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

Allow "Required: False" on Response headers - Fix #1261 #1293

Merged
merged 7 commits into from
Jul 16, 2021

Conversation

miguelgf
Copy link
Contributor

Fixes #1261

Changes proposed in this pull request:

  • Allows response headers to have required: False without NonConformingResponseHeaders error

@coveralls
Copy link

coveralls commented Sep 11, 2020

Coverage Status

Coverage decreased (-0.08%) to 96.999% when pulling 1247a9e on miguelgf:master into c014299 on zalando:main.

@miguelgf
Copy link
Contributor Author

Hello @jmcs

Any change this gets reviewed and merged? I would like to use in a project and we will need to fork the project to support our required: false on our openapi specs

Regards

@Ruwann Ruwann self-requested a review July 7, 2021 13:41
Copy link
Member

@Ruwann Ruwann left a comment

Choose a reason for hiding this comment

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

Hi @miguelgf , thanks for the PR! We have moved from master to main, could you rebase your PR (there will probably be a conflict) ?

@@ -52,9 +52,10 @@ def validate_response(self, data, status_code, headers, url):

if response_definition and response_definition.get("headers"):
# converting to set is needed to support python 2.7
response_definition_header_keys = set(response_definition.get("headers").keys())
required_header_keys = {k for (k, v) in response_definition.get("headers").items()
if 'required' not in v or v['required']}
Copy link
Member

Choose a reason for hiding this comment

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

you can simplify this by using dict.get():

if v.get('required')
# or to be explicit:
if v.get('required', False)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Great tip, changed

@miguelgf miguelgf changed the base branch from master to main July 7, 2021 16:27
@miguelgf
Copy link
Contributor Author

miguelgf commented Jul 7, 2021

@Ruwann rebased against main 👍

@miguelgf miguelgf requested a review from Ruwann July 7, 2021 16:50
@Ruwann
Copy link
Member

Ruwann commented Jul 8, 2021

It seems that your code was correct, but another test failed because, imo, the test itself is incorrect.

For OpenAPI 3, it is clearly in the spec what the expected behaviour for optional and required headers is, but it is not very clear for Swagger 2. This issue (OAI/OpenAPI-Specification#321) and this response (OAI/OpenAPI-Specification#321 (comment)) seem to indicate that that the response headers in Swagger 2 are optional.

In OpenAPI 3, when the required field is not present in the header, it should be considered false. So you can revert that last change.

If you are up for it, you can adjust the corresponding test and swagger/openapi. But I'm also happy to do it myself in a PR to your branch or in a follow-up PR.

https://github.com/zalando/connexion/blob/c014299435990ff1d4c20596396ca563f7b266b4/tests/api/test_headers.py#L20-L30

https://github.com/zalando/connexion/blob/c014299435990ff1d4c20596396ca563f7b266b4/tests/fixtures/simple/swagger.yaml#L559-L572

https://github.com/zalando/connexion/blob/c014299435990ff1d4c20596396ca563f7b266b4/tests/fixtures/simple/openapi.yaml#L704-L720

@miguelgf
Copy link
Contributor Author

miguelgf commented Jul 8, 2021

@Ruwann feel free to change the code, I won't be able to make any changes until late today. Thanks!

Co-authored-by: Ruwann <ruwan.lambrichts@ml6.eu>
@miguelgf
Copy link
Contributor Author

@Ruwann I've reading what you sent and I see the issue, yes, the test is not testing the proper behaviour, but we can't test it on the Swagger 2, as there is no way to say that a header is required with the Header Object specification (https://swagger.io/specification/v2/#headerObject).

This test can be perform on openapi 3, on swagger 2 this test has no sense (every header is always optional, based on their specification).

We can change the test so it only pass on OpenAPI 3 and we don't pass it on Swagger 2, what do you think?

Swagger2 response headers are optional, and have no 'required'
attribute. OpenAPI3 response headers are optional by default,
but can be specified to be required by setting 'required: true'.
@hjacobs hjacobs added this to the 2.9 milestone Jul 16, 2021
@hjacobs hjacobs merged commit 005d046 into spec-first:main Jul 16, 2021
@miguelgf
Copy link
Contributor Author

Thanks @Ruwann and @hjacobs !!

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

Successfully merging this pull request may close these issues.

Respect "required: false" specification on headers
4 participants