Skip to content

JSON Schema & Pydantic's default factory. #1026

@koxudaxi

Description

@koxudaxi

Discussed in #882

Originally posted by zcherry17 October 24, 2022
Unless this is already a feature, I cannot find anything in the code base that supports this.
I am trying to make a schema that generates the following:

class Foo(BaseModel):
    my_list: list[int] = Field(default_factory=list)

Firstly, is there a reason why default factory is not supported? If there is not any particular reason, then a default factory should be generated for arrays in some meaningful way. Perhaps if the following schema was defined, then you would get the appropriate generation.

{
    "$id": "/foo",
    "$schema": "https://json-schema.org/draft/2020-12/schema",
    "title": "Foo",
    "type": "object",
    "properties": {
        "my_list": {
            "type": "array",
            "items": {"type": "integer"}
            "default": { "default_factory": "list" }
            < -- or -- >
            "default_factory": "list" < -- as I have seen that extra keys are supported in the code generator -->
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions