Skip to content

Flask Integation decorator failing #488

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

Closed
rconnorjohnstone opened this issue Jan 25, 2023 · 1 comment · Fixed by #489
Closed

Flask Integation decorator failing #488

rconnorjohnstone opened this issue Jan 25, 2023 · 1 comment · Fixed by #489

Comments

@rconnorjohnstone
Copy link

For a flask app, the openapi decorator is failing to provide a response to the flask decorator. Running the following minimum reproducible example, basically taken directly from the documentation:

application/__init__.py:

from flask import Flask
from openapi_core import Spec
from openapi_core.contrib.flask.decorators import FlaskOpenAPIViewDecorator

app = Flask(__name__)
spec = Spec.from_file_path("openapi_alt.yml")
openapi = FlaskOpenAPIViewDecorator.from_spec(spec)

@app.route('/home')
@openapi
def home():
    pass

openapi_alt.yml:

openapi: 3.1.0
info:
  title: OSS IRI rewrite
  description: This api will specify the different ways to call the IRI tool
  version: 0.1.0
paths:
  /home:
    get:
      responses:
        "200":
          content:
            text/html:
              schema:
                type: string

by running the command:

flask --app application run

then curling:

curl localhost:5000/home

produces the following error:

[2023-01-25 14:54:26,776] ERROR in app: Exception on /home [GET]
Traceback (most recent call last):
  File "/home/connor/.local/lib/python3.10/site-packages/flask/app.py", line 2525, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/connor/.local/lib/python3.10/site-packages/flask/app.py", line 1822, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/connor/.local/lib/python3.10/site-packages/flask/app.py", line 1820, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/connor/.local/lib/python3.10/site-packages/flask/app.py", line 1796, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)
  File "/home/connor/.local/lib/python3.10/site-packages/openapi_core/contrib/flask/decorators.py", line 57, in decorated
    response_result = self.process_response(
  File "/home/connor/.local/lib/python3.10/site-packages/openapi_core/validation/processors.py", line 28, in process_response
    return self.response_validator.validate(spec, request, response)
  File "/home/connor/.local/lib/python3.10/site-packages/openapi_core/validation/response/proxies.py", line 38, in validate
    return validator.validate(spec, request, response, base_url=base_url)
  File "/home/connor/.local/lib/python3.10/site-packages/openapi_core/validation/response/validators.py", line 234, in validate
    operation_response = self._find_operation_response(
  File "/home/connor/.local/lib/python3.10/site-packages/openapi_core/validation/response/validators.py", line 63, in _find_operation_response
    return self._get_operation_response(operation, response)
  File "/home/connor/.local/lib/python3.10/site-packages/openapi_core/validation/response/validators.py", line 71, in _get_operation_response
    return finder.find(str(response.status_code))
  File "/home/connor/.local/lib/python3.10/site-packages/openapi_core/contrib/werkzeug/responses.py", line 16, in status_code
    return self.response._status_code
AttributeError: 'NoneType' object has no attribute '_status_code'

It seems that combining the openapi decorator with the flask decorator (exactly as shown in the docs) causes the flask decorator to only get the output of the routing function, rather than a response object which wraps that output.

@p1c2u
Copy link
Collaborator

p1c2u commented Jan 26, 2023

Hi @rconnorjohnstone

it's not the OpenAPI decorator issue. Your home view function is invalid. Remove the OpenAPI decorator and run your request again and you will get flask error

TypeError: The view function for 'home' did not return a valid response. The function either returned None or ended without a return statement.

But it's a good point that decorator/integration should check request type at the beginning.

EDIT: Next issue can be not handling str, bytes, dict, list and tuple return values from decorated function.

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 a pull request may close this issue.

2 participants