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

Not getting any response if response validation fails. #1896

Closed
gaurav-triverus opened this issue Mar 8, 2024 · 0 comments
Closed

Not getting any response if response validation fails. #1896

gaurav-triverus opened this issue Mar 8, 2024 · 0 comments

Comments

@gaurav-triverus
Copy link

gaurav-triverus commented Mar 8, 2024

Description

when application has a validation error in response, it doen't send response. In the below example my response data from application code doesn't match with the schema defined in openAPI. I can see it goes in the JSONResponseBodyValidator and raises NonConformingResponseBody in the application but doesn't send back the response.

Expected behaviour

Response validation propagates the exception up

Actual behaviour

The app hangs forever and never recieves the response.

Steps to reproduce

This is how I am configuring my application

def create_app():
    options = SwaggerUIOptions(swagger_ui_path="/docs")
    validator_map = validation.get_custom_validator_map()
    app = connexion.FlaskApp(
        __name__, specification_dir=get_project_root_dir(), 
        swagger_ui_options=options,
    )
    app.add_api(
        'openapi.yaml',
        swagger_ui_options=options,
        strict_validation=True,
        validate_responses=True,
    )
    flask_app = app.app
    @flask_app.before_request
    def push_db():
        print('test app --> in before request ')

    @flask_app.teardown_request
    def close_db(exception=None):
        print('test app--> in teardown request')

    @flask_app.after_request
    def access_log_end(response):
        print('test app--> in after request', response.data)
        return response
    
    app.run(host = "0.0.0.0",
            port=5000,
            )
    # app.run(debug=True, host="0.0.0.0")

OpenApi has the successful response schema. The schema has a field data with type array or objct. In my response if I am passing None in data. I am getting validation error within the application but never recieves the response back.
OpenAPI -

openapi: 3.0.0
info:
  title: Sample API
  version: 1.0.0
paths:
  /health:
    get:
      security:
        - {}
      # security:
      #   - jwt: []
      summary: hellow world
      description: hellow world
      operationId: handlers.health.health
      responses:
        '200':
          description: Successful response
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/SuccessfulResponse"

components:
  securitySchemes:
    jwt:
      type: http
      scheme: bearer
      bearerFormat: JWT
      x-bearerInfoFunc: application.app.decode_jwt

  schemas:
    SuccessfulResponse:
      type: object
      properties:
        status_code:
          type: integer
        message:
          type: string
        meta:
          $ref: "#/components/schemas/Meta"
        data:
          oneOf:
            - type: array
            - type: object
        warnings:
          type: array
          items:
            type: string
        errors:
          type: array
          items:
            type: string
      required: ["status_code"]
      additionalProperties: false

    Meta:
      type: object
      properties:
        resource:
          type: string
        method:
          type: string
        query:
          type: object
      required: ["resource", "method"]
      additionalProperties: false

below is the application code that sends the response (Please not I am sending None in data which is defined as array or object in openApi_ -

def health():
    print('health check')
    return response_util.success_response(
        message="flask and dbt health check",
        data=None,
        errors=response_error,
        status_code=200,
    ).to_api_response() 

Additional info:

Output of the commands:

  • python --version
    Python 3.9.18
  • pip show connexion | grep "^Version\:"
    Name: connexion
    Version: 3.0.5
    Summary: Connexion - API first applications with OpenAPI/Swagger
    Home-page: https://github.com/spec-first/connexion
    Author: Robbe Sneyders
    Author-email: robbe.sneyders@gmail.com
    License: Apache-2.0
    Location: /usr/local/lib/python3.9/site-packages
    Requires: asgiref, httpx, inflection, Jinja2, jsonschema, python-multipart, PyYAML, requests, starlette, typing-extensions, werkzeug
    Required-by:
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

No branches or pull requests

1 participant