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

Fix body validator different error codes for bad Content-Types. #629

Merged
merged 5 commits into from Aug 14, 2018

Conversation

positron96
Copy link
Contributor

Fixes #628 .

Changes proposed in this pull request:

  • Added a test for this bug.
  • Fixed it by checking for non-empty HTTP POST payload by considering request.body, request.form and request.files (only request.body was checked)

@coveralls
Copy link

coveralls commented Jul 18, 2018

Coverage Status

Coverage remained the same at 100.0% when pulling 85f1f7d on positron96:fix_bodyvalidator_formdata_refed into ccd62f8 on zalando:master.

@dtkav
Copy link
Collaborator

dtkav commented Jul 19, 2018

nice, I noticed this too when working OpenAPI3 support.
Since this may break existing clients, it might make sense to stage it for the 2.0 release.
Just my 2c though.

@positron96
Copy link
Contributor Author

@dtkav That's not a big bug and not a big fix actually, it only changes 400 error to 415.

@spec-first spec-first deleted a comment Jul 19, 2018
@spec-first spec-first deleted a comment Jul 19, 2018
@dtkav
Copy link
Collaborator

dtkav commented Jul 20, 2018

my bad @positron96 , I think i am confusing it with a different issue where connexion accepts form data even if the mimetype is wrong.

@positron96
Copy link
Contributor Author

positron96 commented Jul 20, 2018 via email

@dtkav
Copy link
Collaborator

dtkav commented Jul 20, 2018

I dug into this a little bit to refresh my memory. I couldn't remember if it was a problem with my branch, or also on master.
It turns out that form parameters won't be rejected based on the "consumes" part of the operation.
For example, the spec below still accepts the form parameters, even though consumes = ["application/json"].

  /test-formData-param:
    post:
      summary: Test formData parameter
      operationId: fakeapi.hello.test_formdata_param
      consumes:
        - application/json  # connexion assumes application/json if this is blank
      # - application/x-www-form-urlencoded
      parameters:
        - name: formData
          type: string
          in: formData
          required: true
      responses:
        200:
          description: OK

The client still sends the correct content type alongside the form data, but the spec doesn't specify that the endpoint consumes form content types, and connexion isn't strict - it let's the form data through.

It's an oddity of the swagger 2.0 spec that you can specify in: formData separately from the consumes block. This is fixed in the 3.0 spec with the introduction of requestBody. The existing behavior may actually make sense for the 2.0 spec - I'm not sure. If you were to reject formData parameter if the consumes block didn't match, that should/would probably be in a spec validator. What do you think?

You can reproduce this by running:

cd tests
PYTHONPATH=$PWD connexion run fixtures/simple/swagger.yaml -p 9090
# in another terminal...
curl -X POST -F "formData=somedata" http://localhost:9090/v1.0/test-formData-param

Either way, your change looks good to me!

@positron96
Copy link
Contributor Author

@dtkav Yes, I've reproduced this behavior. It seems that Swagger 2.0 vagueness in this point caused it since it's not clear what to do in this case. Probably keeping formData is OK.

Regarding MIME types, #593 is more serious for me, I'll look into it sometime.

@rafaelcaricio rafaelcaricio self-assigned this Jul 21, 2018
@@ -101,7 +101,8 @@ def wrapper(request):
if all_json(self.consumes):
data = request.json

if data is None and len(request.body) > 0 and not self.is_null_value_valid:
empty_body = len(request.body) == 0 and len(request.form) == 0 and len(request.files) == 0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think not(request.body or request.form or request.files) would be more readable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, it definitely will

@jmcs
Copy link
Contributor

jmcs commented Jul 27, 2018

@positron96 I agree with @dtkav this should target the dev-2.0 branch (and be included in the changes in the README).

@dtkav
Copy link
Collaborator

dtkav commented Jul 27, 2018

@jmcs It turns out I was conflating this change with some other behavior (elaborated on above).
FWIW I now think this change is relatively benign.

@positron96
Copy link
Contributor Author

BTW, I've changed the code as requested.

@jmcs
Copy link
Contributor

jmcs commented Aug 14, 2018

👍

@jmcs jmcs merged commit 6675ccc into spec-first:master Aug 14, 2018
@jmcs
Copy link
Contributor

jmcs commented Aug 14, 2018

Thanks for the PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants