flask-swagger-ui icon indicating copy to clipboard operation
flask-swagger-ui copied to clipboard

How to solve failed to load API definition?

Open vvksahoo opened this issue 4 years ago • 5 comments

I am trying to configure the swagger.json but every time it is giving the below error.

Fetch errorFailed to fetch C:\Project\test\swagger.json
Fetch errorPossible cross-origin (CORS) issue? The URL origin (file://) does not match the page (http://127.0.0.1:5000). Check the server returns the correct 'Access-Control-Allow-*' headers.

When i clicked on "Invalid" button {"schemaValidationMessages":[{"level":"error","message":"Can't read from file c:/Project\test\\swagger.json"}]} this error is coming .

SWAGGER_URL = '/swagger'
API_URL = 'C:\Project\test\swagger.json'
SWAGGERUI_BLUEPRINT = get_swaggerui_blueprint(
    SWAGGER_URL,
    API_URL,
    config={
        'app_name': "Seans-Python-Flask-REST-Boilerplate"
    }
)
app.register_blueprint(SWAGGERUI_BLUEPRINT, url_prefix=SWAGGER_URL)

Let me know what i am doing wrong.

vvksahoo avatar Mar 05 '20 14:03 vvksahoo

Hi,

the API_URL should be a endpoint (resource) at your server (e.g. /docs/swagger.json), not a file on the system. In other words you'll need to manually serve this file somehow at a given resource.

sveint avatar Mar 06 '20 12:03 sveint

Would you happen to have an example of this? I am also running into this problem and unsure how to resolve it.

Jonyorker avatar Mar 25 '20 14:03 Jonyorker

You could do something like this modified example, if using flask to serve the file:

import os
from flask import Flask, send_from_directory
from flask_swagger_ui import get_swaggerui_blueprint

app = Flask(__name__)


SWAGGER_URL = "/api/docs"
API_URL = "/docs/swagger.json"

swaggerui_blueprint = get_swaggerui_blueprint(
    SWAGGER_URL,
    API_URL,
    config={"app_name": "Test application"}
)
app.register_blueprint(swaggerui_blueprint, url_prefix=SWAGGER_URL)


@app.route("/docs/swagger.json")
def specs():
    return send_from_directory(os.getcwd(), "swagger.json")


app.run()

This assumes that swagger.json is in current working directory, so adjust accordingly.

sveint avatar Mar 25 '20 15:03 sveint

ok, that seems to help a bit.

I've still got a blank swagger.json though, how do I generate the documentation?

Jonyorker avatar Mar 25 '20 18:03 Jonyorker

https://swagger.io/docs/specification/basic-structure/

jeremyrlee avatar Jul 28 '20 17:07 jeremyrlee