-
-
Couldn't load subscription status.
- Fork 780
Validate required fields based on readOnly & writeOnly #1943
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
base: main
Are you sure you want to change the base?
Conversation
|
This regresses #942. Arguably the same should apply for (To fix #942 here, all one needs to do is make |
|
I cannot agree with @sampoh0523 here. I believe the intent of |
|
@RobbeSneyders @Ruwann would you please approve a workflow run on this PR? |
connexion/json_schema.py
Outdated
|
|
||
| for prop in required: | ||
| if prop not in instance: | ||
| properties = schema.get('properties') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the prevailing style in this code is to use double quotes (not single quotes) around strings, please consider matching that.
The OpenAPI specification argues otherwise, see e.g. https://swagger.io/specification/:
Note also the wording on the specification for version 3.0.3: https://spec.openapis.org/oas/v3.0.3.html
This is RFC 2119 wording, which explicitly allows readOnly fields to be sent as part of a request (only telling that it should not be done). If it was forbidden, it would be |
|
@sampoh0523 thanks for the quick reply. The problem stated in issue #1907 is that Connexion rejects a request that is MISSING a field if the schema marks that field as both Parsing the difference between SHOULD NOT and MUST NOT gets kind of subtle here. :/ |
|
Yes, that is correct. #942 has further discussion on it (including quoted commentary by one of the JSON Schema designers), but (my point as well is that) |
connexion/json_schema.py
Outdated
|
|
||
|
|
||
| def validate_readOnly(validator, wo, instance, schema): | ||
| yield ValidationError("Property is read-only") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sampoh0523 has convinced me that there is no need for this function, the argument is that Connexion should tolerate a readOnly field in requests without complaint.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current implementation of validate_required checks that some kind of function is specified for validating readOnly fields, since that is used to determine whether such fields can be skipped from the required validation. Deleting the function would therefore require some additional changes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh good point. Why is it important for the validate_required function to check for presence of a validate_readOnly function? Asked differently, why is just checking for the readOnly subschema property not sufficient?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is because the same validate_required is used for both requests and responses, and it recognizes which is which by checking the validators used (if there is a readOnly validator, it is a request; if there is a writeOnly validator, it is a response).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please let me amend my first comment here. @sampoh0523 has convinced me that this function should be a no-op. It has to exist and be registered, so the validation function can detect it is validating a request, but this function should never reject an instance.
|
I think this PR should add the following new test cases:
|
|
hi @whoseoyster do you have time to update your PR? When I run tox on this PR I see these test failures: The |
|
Just for the record, here's a summary of the difference from V2 to V3. In V2, one JSON schema test checks that a request with a |
|
@whoseoyster please consider this revision to your PR. It changes the |
|
|
||
|
|
||
| def validate_writeOnly(validator, wo, instance, schema): | ||
| yield ValidationError("Property is write-only") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Extending the scope here slightly, for writeOnly the OpenAPI spec section https://spec.openapis.org/oas/v3.0.3.html#fixed-fields-19 says "MAY be sent as part of a request but SHOULD NOT be sent as part of the response." Well, this test case checks that Connexion rejects/fails a response that contains a writeOnly field. I don't much like it, but to conform to the spec, apparently Connexion should allow a response with such a field. The V2 and V3 branches agree on this point, for whatever that is worth. @sampoh0523 would you please comment?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I did not see this before.
I do not have strong opinions about writeOnly, because the code using Connexion has control over its responses, unlike with readOnly where it has no control over the requests it receives.
|
@whoseoyster I sure was hoping to hear from you. If you cannot find time to update this PR, I can submit a new PR with the essential changes. Please comment. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do not recommend merge of the current version due to the behavior of Connexion that deviates from the JSON schema spec on required+readOnly fields in requests.
|
@chrisinmtown added your changes here |
Fixes #1907.
Changes proposed in this pull request:
requiredandreadOnlyvalidators to theDraft4ResponseValidatorandDraft4RequestValidatorobjectsLet me know if anything else needs adjustments.