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

Inconsistent validation of multipart body defined with $ref #495

Closed
setaman opened this issue Dec 21, 2020 · 4 comments
Closed

Inconsistent validation of multipart body defined with $ref #495

setaman opened this issue Dec 21, 2020 · 4 comments
Assignees
Labels
bug Something isn't working

Comments

@setaman
Copy link

setaman commented Dec 21, 2020

Hello, I am trying to validate a multipart/form-data request body. The following spec is given:

...
requestBody:
        $ref: "#/components/requestBodies/Image"
...

components:
  requestBodies:
    Image:
      required: true
      content:
        multipart/form-data:
          schema:
            type: object
            required:
              - image
            properties:
              image:
                type: string
                format: binary
          encoding:
            image:
              contentType: image/png, image/jpeg

As you can see, I point to the body definition with $ref. When a request is made to the endpoint, the validator reports an error and says that the required image field is not present. The spec is valid, I have also validated it with other tools.

I spent several hours trying to figure out what the reason could be. Then I removed $ref and wrote down the body definition directly in the route. And voila, it works like expected!

Please note that $ref works wonderful with application/json body. This problem appears in my case only with multipart.
During my research I also saw that you had commented out $ref in this example as well. So, any thoughts on this?

@cdimascio
Copy link
Owner

@setaman this is a bug. the use of requestBodies is causing the problem. For now, you could extract the schema object out e.g

/sample_4:
  post:
    description: upload an image
    requestBody:
      content:
        multipart/form-data:
          schema:
            $ref: '#/components/schemas/Image'
    responses:
      200:
        description: upload success
components:
  schemas: 
    Image:
      type: object
      required:
        - image
      properties:
        image:
          description: the image
          type: string
          format: binary

@cdimascio cdimascio added the bug Something isn't working label Dec 21, 2020
@cdimascio cdimascio self-assigned this Dec 21, 2020
@cdimascio
Copy link
Owner

@setaman fixed in v4.9.5

@setaman
Copy link
Author

setaman commented Dec 22, 2020

@cdimascio thanks, awesome, life saver on the last working day before holiday. But what happened to the README? Documentation disappeared.

@cdimascio
Copy link
Owner

@setaman np. thanks letting me know about the README. its back

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

No branches or pull requests

2 participants