Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unresolved reference while instantiating Encoding #68

Merged
merged 2 commits into from
Apr 7, 2020
Merged

Unresolved reference while instantiating Encoding #68

merged 2 commits into from
Apr 7, 2020

Conversation

karsa-mistmere
Copy link
Contributor

@karsa-mistmere karsa-mistmere commented Apr 1, 2020

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).

// Don't pass the schema if it's still an unresolved reference.
if (this->schema->properties[$property] instanceof Reference) {
$encoding[$property] = new Encoding($encodingData);
}
else {
$encoding[$property] = new Encoding($encodingData, $this->schema->properties[$property] ?? null);
}

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`.
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).
@cebe cebe added this to the 1.4.1 milestone Apr 1, 2020
@cebe cebe added the bug Something isn't working label Apr 1, 2020
@cebe cebe self-assigned this Apr 1, 2020
@cebe
Copy link
Owner

cebe commented Apr 1, 2020

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

Test case for unresolved references while instantiating Encoding
@karsa-mistmere
Copy link
Contributor Author

Sure, I've commited it.

@cebe cebe merged commit 9572321 into cebe:master Apr 7, 2020
@cebe
Copy link
Owner

cebe commented Apr 7, 2020

Thank you!

@karsa-mistmere karsa-mistmere deleted the patch-1 branch April 29, 2020 16:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants