dataclasses-jsonschema icon indicating copy to clipboard operation
dataclasses-jsonschema copied to clipboard

DataclassesPlugin, OpenAPI 3.0.2: invalid specification

Open ZdenekM opened this issue 4 years ago • 1 comments

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

ZdenekM avatar Dec 11 '19 14:12 ZdenekM

@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?

henryh9n avatar May 10 '20 22:05 henryh9n