-
Notifications
You must be signed in to change notification settings - Fork 137
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
Add option to permit validation when schema and data are empty #417
Add option to permit validation when schema and data are empty #417
Conversation
3d68f0d
to
90fb86f
Compare
raise InvalidResponse, "Invalid response.\n\n#{errors}" | ||
end | ||
rescue => e | ||
raise InvalidResponse, "Invalid response.\n\nschema is undefined" if /undefined method .all_of. for nil/ =~ e.message |
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.
ruby 3.2.2
NoMethodError: undefined method `all_of' for nil:NilClass
ruby 3.3.0
NoMethodError: undefined method `all_of' for nil
ruby head
NoMethodError: undefined method 'all_of' for nil
@chibicco Sorry for the delay. Do you still need this? |
90fb86f
to
d1488a8
Compare
@brandur Yes, I need this pull requests. |
Greetings. I have just noticed the following correction. If my PR is not appropriate, I would appreciate it if you could close it. |
@chibicco Let's hold on that a bit longer. We have someone offering to help with maintenance so we may yet be able to rescue the project. |
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.
@chibicco I left a few comments. Could you check?
@@ -46,6 +47,7 @@ def initialize(options, schema, schema_type) | |||
@optimistic_json = options.fetch(:optimistic_json, false) | |||
@parse_response_by_content_type = options.fetch(:parse_response_by_content_type, true) | |||
@parameter_overwite_by_rails_rule = options.fetch(:parameter_overwite_by_rails_rule, true) | |||
@permit_blank_structures = options.fetch(:permit_blank_structures, false) |
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.
nits) As for the name of the parameter, how about allow_blank_structures
to match the name of the other parameter if you want to allow it?
committee/lib/committee/schema_validator/option.rb
Lines 42 to 43 in d1488a8
@allow_form_params = options.fetch(:allow_form_params, true) | |
@allow_query_params = options.fetch(:allow_query_params, true) |
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.
Thank you.
I have renamed permit_blank_structures to allow_blank_structures.
ref: 6de7583
@@ -46,6 +47,7 @@ def initialize(options, schema, schema_type) | |||
@optimistic_json = options.fetch(:optimistic_json, false) | |||
@parse_response_by_content_type = options.fetch(:parse_response_by_content_type, true) | |||
@parameter_overwite_by_rails_rule = options.fetch(:parameter_overwite_by_rails_rule, true) | |||
@permit_blank_structures = options.fetch(:permit_blank_structures, false) |
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.
Could you add a description of this parameter to the README?
https://github.com/interagent/committee?tab=readme-ov-file#configuration-options
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 have added a note to the README : )
[memo]
In OpenAPI 3, the following becomes nil, so the validation of empty response succeeds.
Therefore, this option is expected to be valid only for Hyper-schema and OpenAPI 2.
"responses": { | ||
"200": { | ||
"description": "empty schema" |
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.
imo) 204 No Content I think the response body is empty as a specification. So why not make the test data follow the common use case?
"responses": { | |
"200": { | |
"description": "empty schema" | |
"responses": { | |
"204": { | |
"description": "The resource was deleted successfully." |
Refs: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/204#compatibility_notes
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.
In my recent experiences, I've noticed the following scenarios:
-
status_code=200 && empty response
=> This PR addresses this case. While it's technically correct to use status_code=204, there are instances where APIs are operated with 200, and ideally, I'd like to accommodate this case as well. -
status_code=204 && empty response
=> While it's being skipped here for 204, it seemed like a better approach to modify it to skip within Committee::SchemaValidator::HyperSchema::ResponseValidator. If it's not an inconvenience, I'm considering addressing this in a separate PR. Could you please share your thoughts on this?
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.
Thank you for your detailed explanation!
I understood the intent and thought it make sense. So I agree with your opinion.
memo) I noticed in writing the following comment that 204 responses and so on expect to describe a response without a body. In other words, I think the next major update will change this parameters default value. |
Thank you for your review! |
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.
@chibicco Thank you for your response! I think it looks good 😀
Finally, could you add a description of this change in CHANGELOG.md?
@ydah I have updated the CHANGELOG.md :) |
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.
Looks good! Thank you for your work.
cc/ @brandur
@brandur |
I see, I understand ! |
@chibicco Thank you for waiting. Since v5.3.0 has been released, so your patch has been released! Thank you for your great work! |
I have confirmed the v5.3.0 release : ) |
Background
In our project, there are scenarios where the API returns an empty response body with status code 200.
However, the current OpenAPI 2 schema validation does not allow an empty schema for the response body, causing validation errors in these cases.
Specifically, in cases such as the schema and controller example below, I expect the Committee::Middleware::ResponseValidation check to pass successfully.
Changes
To address this issue, I have added an option to allow an empty schema for the response body in OpenAPI 2.
This change enables the validation to pass when both the schema and response body are nil, accommodating the specific use case in our project.