Improve JSON req error on disallowed empty body #1761
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #1152.
Currently, when a request body is empty, the JSON request validator would parse it into None, which is later passed down to the JSON Schema validator. However, jsonschema's validation error message for this case (when nullable is false) "None is not of type 'object'" is not particularly friendly to either the API user, nor the website developer.
This change adds a specific check before the None value is passed to jsonschema to emit a better error message directly.
I also added some drive-by improvements on function argument typing since _validate in validators don't seem to expect receiving None, but _parse (the result of which is passed to _validate) is totally allowed to return None (or anything really). This does not seem to reflect the logic well.
I’m not exactly sure if this is the best way to do this in Connexion 3.x. We do have a patch in Connexion 2.x to achieve a similar effect so if anyone understands how the two implementations correspond please tell me whether the two do the same thing 🙂