s-knibbs / dataclasses-jsonschema

JSON schema generation from dataclasses
MIT License
167 stars 38 forks source link

DataclassesPlugin, OpenAPI 3.0.2: invalid specification #126

Open ZdenekM opened 4 years ago

ZdenekM commented 4 years ago

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 commented 4 years ago

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