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

Improve JSON req error on disallowed empty body #1761

Merged

Conversation

uranusjr
Copy link
Contributor

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 🙂

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.
@RobbeSneyders
Copy link
Member

Thanks @uranusjr!

I see your patch contains a test as well. Could you add it to this PR? test_json_validation.py is probably the most logical place to put it.

The pipelines are failing due to mypy in pre-commit. Could you have a look? You can easily set up pre-commit locally by running

pip install poetry
poetry install pre-commit
pre-commit install

@RobbeSneyders RobbeSneyders merged commit 9c02fdf into spec-first:main Nov 1, 2023
8 checks passed
@uranusjr uranusjr deleted the improve-none-body-validation-error branch November 6, 2023 09:54
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.

Should None is not of type 'object' be friendlier?
2 participants