hangc
hangc

Reputation: 5473

Get Json schema of a Type of pydantic model

I am using pydantic in my project and am using its jsonSchema functions.

I have defined some models using class MyModel(BaseModel) and can get the schema of the model using MyModel.schema().

How can I obtain the json schema when the model is used together with typing.Type? For example the following:

Example of what I would like to obtain:

MyModelList = typing.List[MyModel]
MyModelListSchema = get_schema(MyModelList)

Upvotes: 0

Views: 1248

Answers (1)

hangc
hangc

Reputation: 5473

This is now doable using pydantic and __root__ attribute.

class MyModelList(BaseModel):
    __root__: typing.List[MyModel]

MyModelListSchema = MyModelList.schema()

Upvotes: 2

Related Questions