cebe / php-openapi

Read and write OpenAPI yaml/json files and make the content accessible in PHP objects.
MIT License
466 stars 88 forks source link

Unresolved reference while instantiating Encoding #68

Closed karsa-mistmere closed 4 years ago

karsa-mistmere commented 4 years ago

If there's a $ref within a multipart/form-data requestBody, the Encoding will be instantiated with a Reference object instead of a Schema object as expected resulting in a TypeErrorException.

To reproduce, just parse the following OpenAPI schema:

openapi: "3.0.0"
info:
  version: 1.0.0
  title: Multipart demo
paths:
  /pets:
    post:
      summary: Create a pet
      operationId: createPets
      tags:
        - pets
      requestBody:
        content:
          multipart/form-data:
            schema:
              type: object
              properties:
                pet:
                  $ref: '#/components/schemas/Pet'
                petImage:
                  type: string
                  format: binary
            encoding:
              pet:
                contentType: application/json
              petImage:
                contentType: image/*
          application/json:
            schema:
              $ref: '#/components/schemas/Pet'
        description: The pet to be created
        required: true
      responses:
        '201':
          description: Null response

components:
  schemas:
    Pet:
      type: object
      properties:
        id:
          type: integer
          format: int64
        name:
          type: string

Since this parameter is only ever used to guess the default encoding type of a property if and when it is not explicitly defined, I propose to skip the schema parameter altogether if during instantiation it is still an unresolved reference (otherwise we would have to pre-resolve references during instantiation leading to all other kinds of mishaps and a very substantial rewrite of the whole project).

https://github.com/cebe/php-openapi/blob/aafedb56ed8b860ce83b51b81bc1c902b77d8ecb/src/spec/MediaType.php#L56-L62

cebe commented 4 years ago

Thanks for the fix! Could you add a test case to verify it?

karsa-mistmere commented 4 years ago

Sure, I've commited it.

cebe commented 4 years ago

Thank you!