Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
138 views
in Technique[技术] by (71.8m points)

python - Openapi-core: Unexpected ServerNotFound while validating the request in Flask

I am running my Flask app (v1.1.1) in local and and the follow path is available: http://localhost:5000/api/v1/bookings/ and i am actually targetting it via Postman and it is working.

I am using flask blueprint to register the endpoint. For completenss here some spippet of the code.

...
bp_booking = Blueprint('booking', 'booking')
app.register_blueprint(bp_booking, url_prefix='/api/v1/bookings')
... 
@bp_booking.route('/', methods=['POST'])
def create():
    user_uuid = g.requestor_uuid
...

I want to add on top swagger validation and so i decided to use openapi-core.

Here a sample of the swagger ( it is valid)

openapi: 3.0.1
info:
  title: Office API
  version: 1.0.0
servers:
- url: https://localhost/v1/api
- url: http://localhost/v1/api
paths:
  /bookings/:
...
     post: 
...

I want so to add the swagger validation and when I code the in my @app.before_request (I put the init for the spec and validator just for the sake of test in the same callback):

@app.before_request
def before_request_func():
    app.logger.info("START: before_request")
    # validate rq...

    from openapi_core.validation.request.validators import RequestValidator
    from openapi_core.contrib.flask import FlaskOpenAPIRequest
    from openapi_core import create_spec


    spec_dict = spec_dict_from_file('api/api3.yaml')
    app.logger.info("Build RequestValidator")
    validator = RequestValidator(create_spec(spec_dict))

    app.logger.info("Build FlaskOpenAPIRequest")
    openapi_request = FlaskOpenAPIRequest(request)

    app.logger.info("Validate")
    result = validator._validate_parameters(openapi_request)
    app.logger.info("List errors")
    app.logger.error(result.errors)

    app.logger.info("END: before_request")

I got the following error

INFO:app:START: before_request
INFO:app:Build RequestValidator
INFO:app:Build FlaskOpenAPIRequest
INFO:app:Validate
INFO:app:List errors
ERROR:app:[**ServerNotFound**(url='http://localhost:5000/api/v1/bookings/')]
INFO:app:END: before_request

Why is openapi-core not capable to find the server difitiotion? Is the url (http://localhost:5000/api/v1/bookings/) is the one coming form the request or the one build form the swagger? Does the error means the two does not match?

Some req:

  • Flask==1.1.1
  • openapi-core==0.13.4

Thanks.

question from:https://stackoverflow.com/questions/65860074/openapi-core-unexpected-servernotfound-while-validating-the-request-in-flask

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...