mike-oakley / openapi-pydantic

Pydantic OpenAPI schema implementation
Other
48 stars 7 forks source link

Handling lists / TypeAdapter #33

Closed gberaudo closed 1 month ago

gberaudo commented 2 months ago

I want to return a special kind of JSON: a bare list of objects:

["tour1", "tour2", ...]

Pydantic provides the TypeAdapter to directly parse / serialize primitive types, and notably lists.

Unfortunately, using such adapter does not produces a class so it is not possible to use it directly with openapi-pydantic.

# with PDCListTourOrRecordedTrack = TypeAdapter(list[Tour | Recorded])
# we get an error since that does not inherit from `BaseModel`
"schema": PydanticSchema(schema_class=PDCListTourOrRecordedTrack, enum=None),

Is this use case handled by openapi-pydantic?

jcklie commented 1 month ago

You could try to use root models 1.

mike-oakley commented 1 month ago

Hey @gberaudo - yep currently schema_class expects a type which has a base of BaseModel from pydantic so type adapters wouldn't work. As @jcklie suggested, you could try to use a root model for this use case as it should then conform to the expected interface