How do I force pydantic schema into recursion instead of reference?

Viewed 280

According to Pydantic's documentation,

"Sub-models" with modifications (via the Field class) like a custom title, description or default value, are recursively included instead of referenced.

However, if I execute this code;

def test_me():

    class cls1(BaseModel):

        one: str
        two: str = None

    class cls2(BaseModel):
        three: int = 3
        four: cls1 = Field(None, title="TestMe")

    print()
    pprint(cls2.schema())

I get the following result;

{'definitions': {'cls1': {'properties': {'one': {'title': 'One',
                                                 'type': 'string'},
                                         'two': {'title': 'Two',
                                                 'type': 'string'}},
                          'required': ['one'],
                          'title': 'cls1',
                          'type': 'object'}},
 'properties': {'four': {'allOf': [{'$ref': '#/definitions/cls1'}],
                         'title': 'TestMe'},
                'three': {'default': 3, 'title': 'Three', 'type': 'integer'}},
 'title': 'cls2',
 'type': 'object'}

Note that cls1 is still presented as a definition and referenced. I want the value to be nested, not referenced. According to the documentation, I would expect that to occur when modifying the title of the Field object.

Q: It not via the approach above, How do I force Pydantic to nest a model when creating a json-schema instead of calling by reference?

[Update] I added this question as an issue on github, thinking it might be a bug; https://github.com/samuelcolvin/pydantic/issues/3627

[Update] funnydman was kind enough to point me to an existing issue. If you scroll to the very bottom, you will find a post-processing solution. I'm still left with the confusion that the documentation seems to imply the ability to do what I am asking? https://github.com/samuelcolvin/pydantic/issues/889

0 Answers
Related