Harshita
Harshita

Reputation: 57

Running ApiSpec with Flask and Swagger UI

I am trying to render my API's in Swagger. I am using ApiSpec library to generate the Open Api Spec, which then I trying to add into my Swagger UI. I am trying to use MethodView available in Flask with the following code below.

from flask.views import MethodView
from flask import Blueprint, after_this_request, make_response

test_data = Blueprint('test', __name__, url_prefix='/testdata')

class TestDataApi(MethodView):

def get():
    """Get all TestData.
    ---
    description: Get a random data
    security:
        - ApiKeyAuth: []
    responses:
        200:
        description: Return all the TestData
        content:
            application/json:
            schema: TestDataSchema
        headers:
            - $ref: '#/components/headers/X-Total-Items'
            - $ref: '#/components/headers/X-Total-Pages'
    """
    data = TestData.query.all()
    response_data = test_schema.dump(data)
    resp = make_response(json.dumps(response_data), 200)
    return resp

This is my app.py where I am trying to register swagger and corresponding views:

    api = Blueprint('api', __name__, url_prefix="/api/v0")
    
    spec =  APISpec(
           title='Test Backend',
           version='v1',
            openapi_version='3.0.2',
            plugins=[MarshmallowPlugin(), FlaskPlugin()],
        )
    
    api.register_blueprint(test_data)
    app.register_blueprint(api)

    test_view = TestDataApi.as_view('test_api')
    app.add_url_rule('/api/v0/testdata/', view_func=test_view, methods=['GET',])

    spec.components.schema("TestData", schema=TestDataSchema)
    spec.path(view=test_view, operations=dict(get={}))

    SWAGGER_URL = '/api/v0/docs'
    API_URL = 'swagger.json' 

    swaggerui_blueprint = get_swaggerui_blueprint(
    SWAGGER_URL,
    API_URL,
    config={
        'app_name': "Backend"
    })
    app.register_blueprint(swaggerui_blueprint)

But I keep hitting this below error and I am not able to deduce how to fix it.

File "/home/hh/Projects/testdata/src/goodbytz_app.py", line 29, in <module>
    app = create_app()
  File "/home/hh/Projects/testdata/src/app.py", line 100, in create_app
    spec.path(view=additives_view, operations=dict(get={}))
  File "/home/hh/Projects/testdata/test_poc/lib/python3.10/site-packages/apispec/core.py", line 516, in path
    plugin.operation_helper(path=path, operations=operations, **kwargs)
  File "/home/hh/Projects/testdata/test_poc/lib/python3.10/site-packages/apispec/ext/marshmallow/__init__.py", line 218, in operation_helper
    self.resolver.resolve_operations(operations)
  File "/home/hh/Projects/testdata/test_poc/lib/python3.10/site-packages/apispec/ext/marshmallow/schema_resolver.py", line 34, in resolve_operations
    self.resolve_response(response)
  File "/home/hh/Projects/testdata/test_poc/lib/python3.10/site-packages/apispec/ext/marshmallow/schema_resolver.py", line 183, in resolve_response
    if "headers" in response:
TypeError: argument of type 'NoneType' is not iterable

Upvotes: 0

Views: 1489

Answers (1)

danangjoyoo
danangjoyoo

Reputation: 360

can try this one, more simple and its automatically generate openapi/swagger https://pypi.org/project/flask-toolkits/

Upvotes: 0

Related Questions