Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: Pydantic Generic Models are not handled correctly in "$ref" field #44

Open
jfschneider opened this issue Sep 19, 2024 · 0 comments

Comments

@jfschneider
Copy link

The "$ref" field in the openapi-schema only has a limited set of characters allowed. If I use a Generic Model as the schema_class parameter for "PydanticSchema", the square brackets "[]" won't get removed, which results in an invalid schema.
Pydantic itself removes all invalid characters in "‎GenerateJsonSchema.normalize_name" (https://github.com/pydantic/pydantic/blob/main/pydantic/json_schema.py#L1939 ).

Reproduce:

from typing import Generic, TypeVar

from openapi_pydantic.util import (PydanticSchema,
                                   construct_open_api_with_schema_class)
from openapi_pydantic.v3 import OpenAPI
from pydantic import BaseModel

DataType = TypeVar("SibData")


class ResponseObject(BaseModel, Generic[DataType]):
    msg: str
    data: DataType


class HostResponse(BaseModel):
    fqdn: str
    location: str


if __name__ == "__main__":
    openapi = OpenAPI.model_validate(
        {
            "info": {"title": "Test", "version": "v0.0.1"},
            "paths": {
                "/host": {
                    "get": {
                        "responses": {
                            "200": {
                                "description": "host",
                                "content": {
                                    "application/json": {
                                        "schema": PydanticSchema(
                                            schema_class=ResponseObject[HostResponse]
                                        )
                                    }
                                },
                            }
                        },
                    }
                }
            },
        }
    )
    schema = construct_open_api_with_schema_class(openapi)
    print(schema.model_dump_json(by_alias=True, exclude_none=True, indent=2))

This results in

"schema": {
                  "$ref": "#/components/schemas/ResponseObject[HostResponse]"
                }

where as it should be:

"schema": {
                  "$ref": "#/components/schemas/ResponseObject_HostResponse_"
                }

to work with Pydantic:

  "components": {
    "schemas": {
       "ResponseObject_HostResponse_": 

I have a quick fix ready (basically copied the regex from "‎GenerateJsonSchema.normalize_name" to "_construct_ref_obj", but no test yet (https://github.com/jfschneider/openapi-pydantic/blob/normalize-refname/openapi_pydantic/util.py#L173)

jfschneider added a commit to jfschneider/openapi-pydantic that referenced this issue Nov 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant