Skip to content
This repository has been archived by the owner on Aug 19, 2023. It is now read-only.

DataclassesPlugin, OpenAPI 3.0.2: invalid specification #126

Open
ZdenekM opened this issue Dec 11, 2019 · 1 comment
Open

DataclassesPlugin, OpenAPI 3.0.2: invalid specification #126

ZdenekM opened this issue Dec 11, 2019 · 1 comment

Comments

@ZdenekM
Copy link

ZdenekM commented Dec 11, 2019

The following minimal script:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from apispec import APISpec  # type: ignore
from apispec_webframeworks.flask import FlaskPlugin  # type: ignore
from flask import Flask

PORT = 5007

# Create an APISpec
spec = APISpec(
    title="Test service",
    version="0.1",
    openapi_version="3.0.2",
    plugins=[FlaskPlugin()],
)

app = Flask(__name__)


@app.route("/<string:project_id>", methods=['GET'])
def test_path(project_id: str):
    """Publish project
            ---
            get:
              description: Get something
              parameters:
                - in: path
                  name: project_id
                  schema:
                    type: string
                  required: true
                  description: unique ID
              responses:
                200:
                  description: Ok
            """


with app.test_request_context():
    spec.path(view=test_path)


def main():

    print(spec.to_yaml())
    app.run(host='0.0.0.0', port=PORT)


if __name__ == '__main__':
    main()

prints out valid OpenAPI specification:

info:
  title: Test service
  version: '0.1'
openapi: 3.0.2
paths:
  /{project_id}:
    get:
      description: Get something
      parameters:
      - description: unique ID
        in: path
        name: project_id
        required: true
        schema:
          type: string
      responses:
        '200':
          description: Ok

Once DataclassesPlugin is added:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from apispec import APISpec  # type: ignore
from apispec_webframeworks.flask import FlaskPlugin  # type: ignore
from flask import Flask
from dataclasses_jsonschema.apispec import DataclassesPlugin

PORT = 5007

# Create an APISpec
spec = APISpec(
    title="Test service",
    version="0.1",
    openapi_version="3.0.2",
    plugins=[FlaskPlugin(), DataclassesPlugin()],
)

app = Flask(__name__)


@app.route("/<string:project_id>", methods=['GET'])
def test_path(project_id: str):
    """Publish project
            ---
            get:
              description: Get something
              parameters:
                - in: path
                  name: project_id
                  schema:
                    type: string
                  required: true
                  description: unique ID
              responses:
                200:
                  description: Ok
            """


with app.test_request_context():
    spec.path(view=test_path)


def main():

    print(spec.to_yaml())
    app.run(host='0.0.0.0', port=PORT)


if __name__ == '__main__':
    main()

...produced schema is not valid:

info:
  title: Test service
  version: '0.1'
openapi: 3.0.2
paths:
  /{project_id}:
    get:
      description: Get something
      parameters:
      - description: unique ID
        in: path
        name: project_id
        required: true
        schema:
          $ref: '#/components/schemas/{''type'': ''string''}'
      responses:
        '200':
          description: Ok
@henryh9n
Copy link

@s-knibbs We are using this lib and facing the issue atm. Want me to fix this? Will you be able to make a release?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants