Skip to content

Conversation

@RobbeSneyders
Copy link
Member

This PR moves the JSON response body validation to middleware as another step towards #1525.

I moved away from having the body validators implement the complete ASGI interface and calling the next app as mentioned in #1588, as this is not possible for the response validators. We only know the response content type once the response has started, so we can't wrap the validator around the next app.

Instead, the validator now provides a wrapped receive / send callable depending on if it validates either request or response bodies. I updated the request body validator to match this.

@RobbeSneyders RobbeSneyders added this to the Connexion 3.0 milestone Sep 18, 2022
await self._send(self._messages.pop(0))


class TextResonseBodyValidator(JSONResponseBodyValidator):
Copy link
Member

Choose a reason for hiding this comment

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

Why does the validator for content type text/plain subclass from the JSON validator?

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member Author

Choose a reason for hiding this comment

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

The response body in the spec is defined as a json schema, so we can use jsonschema to validate text/plain responses as well. We can even use json.loads to cast non-string values (eg. json.loads("2.0") = 2.0), but json.loads fails for text strings (eg. json.loads("text")), so we should catch any decode errors.

VALIDATOR_MAP = {
"parameter": ParameterValidator,
"body": {"application/json": JSONRequestBodyValidator},
"response": {
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member Author

Choose a reason for hiding this comment

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

Good catch. Question is what kind of behavior we want here. I do think we want to update at the top level, as it would otherwise be impossible to remove validators. That just means the user needs to include all validators they want to use in their custom validator_map.

Base automatically changed from feature/routed-base-classes to main September 26, 2022 18:34
@coveralls
Copy link

coveralls commented Sep 26, 2022

Pull Request Test Coverage Report for Build 3177325520

  • 159 of 171 (92.98%) changed or added relevant lines in 6 files are covered.
  • 17 unchanged lines in 5 files lost coverage.
  • Overall coverage decreased (-0.8%) to 94.424%

Changes Missing Coverage Covered Lines Changed/Added Lines %
connexion/utils.py 15 16 93.75%
connexion/middleware/request_validation.py 19 21 90.48%
connexion/validators.py 61 64 95.31%
connexion/middleware/response_validation.py 61 67 91.04%
Files with Coverage Reduction New Missed Lines %
connexion/apis/flask_api.py 1 91.55%
connexion/utils.py 1 96.75%
connexion/operations/abstract.py 2 82.97%
connexion/apis/abstract.py 6 86.9%
connexion/jsonifier.py 7 76.19%
Totals Coverage Status
Change from base Build 3130164113: -0.8%
Covered Lines: 2862
Relevant Lines: 3031

💛 - Coveralls

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.

5 participants