mike-oakley / openapi-pydantic

Pydantic OpenAPI schema implementation
Other
66 stars 10 forks source link

Handling lists / TypeAdapter #33

Closed gberaudo closed 5 months ago

gberaudo commented 6 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 6 months ago

You could try to use root models 1.

mike-oakley commented 5 months 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