-
-
Notifications
You must be signed in to change notification settings - Fork 432
Default values for required fields are sometimes rendered and sometimes not #3048
Copy link
Copy link
Open
Labels
bugSomething isn't workingSomething isn't working
Description
Default values for required fields are rendered for lists of objects, nested objects and dictionaries but not for plain fields like strings, ints and enums.
Example schema:
{
"$schema": "http://json-schema.org/draft-07/schema",
"title": "Order",
"type": "object",
"definitions": {
"Status": {
"type": "string",
"enum": ["pending", "processing", "shipped", "delivered"]
},
"Priority": {
"type": "integer",
"enum": [1, 2, 3]
},
"Address": {
"type": "object",
"properties": {
"street": {"type": "string"},
"city": {"type": "string"}
},
"required": ["street", "city"]
}
},
"properties": {
"order_id": {"type": "string"},
"status": {
"$ref": "#/definitions/Status",
"default": "pending"
},
"priority": {
"allOf": [{"$ref": "#/definitions/Priority"}],
"default": 2
},
"quantity": {
"type": "integer",
"default": 1
},
"note": {
"type": "string",
"default": "your note here"
},
"tags": {
"type": "array",
"items": {"type": "string"},
"default": ["new"]
},
"metadata": {
"type": "object",
"additionalProperties": {"type": "string"},
"default": {"source": "web"}
},
"shipping_address": {
"allOf": [{"$ref": "#/definitions/Address"}],
"default": {"street": "123 Main St", "city": "Springfield"}
},
"all_addresses": {
"type": "array",
"items": {"$ref": "#/definitions/Address"},
"default": [{"street": "1 First St", "city": "Shelbyville"}]
},
"addresses": {
"type": "object",
"additionalProperties": {"$ref": "#/definitions/Address"},
"default": {"home": {"street": "10 Oak Ave", "city": "Ogdenville"}}
}
},
"required": ["order_id", "status", "priority", "quantity", "note", "tags", "metadata", "shipping_address", "all_addresses", "addresses"]
}
Used commandline:
datamodel-codegen --input schema.json --input-file-type jsonschema --output-model-type pydantic_v2.BaseModel --output model.py
Generated models:
# generated by datamodel-codegen:
# filename: pydantic_v2_model_defaults.json
# timestamp: 2019-07-26T00:00:00+00:00
from __future__ import annotations
from enum import Enum, IntEnum
from pydantic import BaseModel, Field
class Status(Enum):
pending = 'pending'
processing = 'processing'
shipped = 'shipped'
delivered = 'delivered'
class Priority(IntEnum):
integer_1 = 1
integer_2 = 2
integer_3 = 3
class Address(BaseModel):
street: str
city: str
class Order(BaseModel):
order_id: str
status: Status
priority: Priority
quantity: int
note: str
tags: list[str]
metadata: dict[str, str]
shipping_address: Address = Field(
default_factory=lambda: Address.model_validate(
{'street': '123 Main St', 'city': 'Springfield'}
)
)
all_addresses: list[Address] = Field(
default_factory=lambda: [
Address.model_validate(v)
for v in [{'street': '1 First St', 'city': 'Shelbyville'}]
]
)
addresses: dict[str, Address] = Field(
default_factory=lambda: {
k: Address.model_validate(v)
for k, v in {'home': {'street': '10 Oak Ave', 'city': 'Ogdenville'}}.items()
}
)
Expected behavior
In the generated models some of the fields have the defaults, some not. I would expect all the fields to have defaults.
Version:
- OS: N/A
- Python version: 3.13
- datamodel-code-generator version: 0.55.0
Additional context
It's probably related to #437
There is a flag --use-default but it works by making fields non-required so this behaviour is not desired.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
bugSomething isn't workingSomething isn't working