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

jsonschema validation #936

Merged
merged 7 commits into from
Jul 31, 2021
Merged

jsonschema validation #936

merged 7 commits into from
Jul 31, 2021

Conversation

patrickkwang
Copy link
Contributor

Addresses #930.

Changes proposed in this pull request:

  • use jsonschema for validation instead of openapi_spec_validator

connexion/spec.py Outdated Show resolved Hide resolved
Copy link
Collaborator

@rafaelcaricio rafaelcaricio left a comment

Choose a reason for hiding this comment

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

This PR relies on internet connection. Please change that.

@patrickkwang
Copy link
Contributor Author

Many of the tests seem to fail because the OpenAPI specs used for testing are actually invalid.
For example: https://travis-ci.org/zalando/connexion/jobs/530330378#L787
If "in: path", then you must have "required: true":
https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#parameterRequired
Perhaps openapi-spec-validator was not handling this correctly.

@dtkav
Copy link
Collaborator

dtkav commented Nov 10, 2019

Hey @patrickkwang - I'm excited for these changes - thanks for taking them on!

@hjacobs
Copy link
Contributor

hjacobs commented Dec 3, 2019

Could you make the tests pass?

@patrickkwang
Copy link
Contributor Author

No, the tests will fail because many of them are based on the invalid simple schema: #936 (comment)

The tests are corrected in #1010, so this PR will be dependent on that one.

@dtkav dtkav mentioned this pull request Dec 13, 2019
@dtkav
Copy link
Collaborator

dtkav commented Dec 13, 2019

It looks like the jsonschema version doesn't check the case where a parameter default is the wrong type, and this results in two failing tests.
This is something that we can check at runtime, but it is somewhat unfortunate. Overall, it's probably worth the simplicity of this change, but we'll have to remove those tests.
I'll see if folks upstream have any thoughts on this.

@patrickkwang
Copy link
Contributor Author

The JSON Schema actually doesn't enforce that default values validate against the associated schema:
https://json-schema.org/understanding-json-schema/reference/generic.html#annotations
The OpenAPI specification is stricter:
https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#schema-object
So jsonschema is doing what it ought to. Someone could fork it to enforce that extra OpenAPI requirement.

@dtkav
Copy link
Collaborator

dtkav commented Dec 13, 2019

Here's the relevant section from the spec.

Unlike JSON Schema, the value MUST conform to the defined type for the Schema Object defined at the same level. For example, if type is string, then default can be "foo" but cannot be 1.

This PR uses jsonschema to validate the openapi spec.
I think the jsonschema spec provided fails to validate that defaults are correct type properly.

I think this is a bug in their openapi spec validation as implemented with jsonschema.

(the language here is very confusing - ack!)

@dtkav
Copy link
Collaborator

dtkav commented Dec 13, 2019

Also of note, this doesn't completely alleviate the need for openapi-spec-validator yet. We are still using it for remote '$ref' resolution for yaml files.

@patrickkwang
Copy link
Contributor Author

It turns out it's not too hard to extend jsonschema to enforce that all the default values validate against the associated (sub)schemas. The last two tests pass now! This instance_validator business feels janky, though; I intend to ask @Julian if there's a cleaner way to handle references when validating the defaults.

@dtkav
Copy link
Collaborator

dtkav commented Dec 20, 2019

nice! I tried to ask in the swagger irc if there's a way to do it in jsonschema, but didn't hear back.

Copy link
Member

@RobbeSneyders RobbeSneyders left a comment

Choose a reason for hiding this comment

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

Hi @patrickkwang, I hope you're still interested in getting this merged!
The changes look good, I left one comment.
We also switched to the main branch, so would be great if you can retarget and rebase / merge.

@@ -207,17 +244,21 @@ def base_path(self, base_path):

@classmethod
def _validate_spec(cls, spec):
Copy link
Member

Choose a reason for hiding this comment

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

This method is now the same in both classes, so we should be able to lift this to the base Specification class.

@coveralls
Copy link

coveralls commented Jul 29, 2021

Coverage Status

Coverage increased (+0.01%) to 97.034% when pulling 736ba53 on patrickkwang:master into 2229831 on zalando:main.

Copy link
Member

@RobbeSneyders RobbeSneyders left a comment

Choose a reason for hiding this comment

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

Thanks! LGTM.

@RobbeSneyders RobbeSneyders merged commit e3e851d into spec-first:main Jul 31, 2021
Comment on lines +207 to +208
schema_string = pkg_resources.resource_string('connexion', 'resources/schemas/v2.0/schema.json')
openapi_schema = json.loads(schema_string.decode('utf-8'))
Copy link
Contributor

Choose a reason for hiding this comment

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

How about del schema_string after loading openapi_schema, we waste quite some memory here...

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.

8 participants