mike-oakley / openapi-pydantic

Pydantic OpenAPI schema implementation
Other
48 stars 7 forks source link

Example Fails with Type NotSubscritable Error on Pydantic 1.x #20

Closed noce2 closed 1 year ago

noce2 commented 1 year ago

Problem

Pydantic example fails with "not subscriptable" TypeError

Stacktrace

Traceback (most recent call last):
  File "./scripts/expose-market-registries.py", line 4, in <module>
    from openapi_pydantic.util import PydanticSchema, construct_open_api_with_schema_class
  File "/Users/nsikanessien/miniconda3/envs/shared-models/lib/python3.8/site-packages/openapi_pydantic/util.py", line 113, in <module>
    def _validate_schemas(schema_definitions: dict[str, Any]) -> dict[str, Schema]:
TypeError: 'type' object is not subscriptable

Example Code

from pydantic import BaseModel, Field

from openapi_pydantic import OpenAPI
from openapi_pydantic.util import PydanticSchema, construct_open_api_with_schema_class

def construct_base_open_api() -> OpenAPI:
    # Note: for Pydantic 1.x, replace `model_validate` with `parse_obj`
    return OpenAPI.parse_obj(
        {
            "info": {"title": "My own API", "version": "v0.0.1"},
            "paths": {
                "/ping": {
                    "post": {
                        "requestBody": {
                            "content": {
                                "application/json": {
                                    "schema": PydanticSchema(schema_class=PingRequest)
                                }
                            }
                        },
                        "responses": {
                            "200": {
                                "description": "pong",
                                "content": {
                                    "application/json": {
                                        "schema": PydanticSchema(
                                            schema_class=PingResponse
                                        )
                                    }
                                },
                            }
                        },
                    }
                }
            },
        }
    )

class PingRequest(BaseModel):
    """Ping Request"""

    req_foo: str = Field(description="foo value of the request")
    req_bar: str = Field(description="bar value of the request")

class PingResponse(BaseModel):
    """Ping response"""

    resp_foo: str = Field(description="foo value of the response")
    resp_bar: str = Field(description="bar value of the response")

open_api = construct_base_open_api()
open_api = construct_open_api_with_schema_class(open_api)

# print the result openapi.json
# Note: for Pydantic 1.x, replace `model_dump_json` with `json`
print(open_api.json(by_alias=True, exclude_none=True, indent=2))

Environment

Python: 3.8.16 Pydantic: 1.10 Openapi-pydantic: 0.3.1

mike-oakley commented 1 year ago

Ah I see the problem - will push a fix 👍🏼 thanks for reporting

mike-oakley commented 1 year ago

Fix published in 0.3.2 👍🏼