Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Since FastAPI 0.119.0, using arbitrary_types_allowed=True with custom types that define their serialization and JSON Schema breaks when generating OpenAPI #14483

@tiangolo

Description

@tiangolo

Privileged issue

  • I'm @tiangolo or he asked me directly to create an issue here.

Issue Content

Using arbitrary_types_allowed=True with custom types that define their serialization and JSON Schema breaks when generating OpenAPI.

Originally reported in: #14184

Extracting the minimal reproducible example from the comments:

from typing import Annotated

import numpy as np
from fastapi import FastAPI
from pydantic import BaseModel, ConfigDict, WithJsonSchema, TypeAdapter

type MyNumpyArray = Annotated[
    np.ndarray, WithJsonSchema(TypeAdapter(list[float]).json_schema(), mode="serialization"),
]


class MyModel(BaseModel):
    model_config = ConfigDict(arbitrary_types_allowed=True)
    custom_field: MyNumpyArray

app = FastAPI()

@app.get("/")
def test() -> MyModel:
    raise NotImplementedError()

print(app.openapi())
# {'openapi': '3.1.0', 'info': {'title': 'FastAPI', 'version': '0.1.0'}, 'paths': {'/': {'get': {'summary': 'Test', 'operationId': 'test__get', 'responses': {'200': {'description': 'Successful Response', 'content': {'application/json': {'schema': {'$ref': '#/components/schemas/MyModel'}}}}}}}}, 'components': {'schemas': {'MyModel': {'properties': {'custom_field': {'$ref': '#/components/schemas/MyNumpyArray'}}, 'type': 'object', 'required': ['custom_field'], 'title': 'MyModel'}, 'MyNumpyArray': {'items': {'type': 'number'}, 'type': 'array'}}}}

Should be solved by: #14482

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions