Describe the bug
It should forbid extra_fields
by default. PyDantic_v2 itself ignores extra_fields
per default. So I expect datamodel-code-gen
to explicitly set allow_extra_fields=False
unless --allow-extra-fields
is specified
To Reproduce
Example schema:
{
"type": "object",
"properties": {
"name": {
"type": "string"
},
"age": {
"type": "integer",
"minimum": 0
},
"email": {
"type": "string",
"format": "email"
},
"address": {
"type": "object",
"properties": {
"street": {
"type": "string"
},
"city": {
"type": "string"
},
"zipcode": {
"type": "string",
"pattern": "^[0-9]{5}(?:-[0-9]{4})?$"
}
},
"required": ["street", "city", "zipcode"]
},
"phoneNumbers": {
"type": "array",
"items": {
"type": "string",
"pattern": "^[0-9]{10}$"
}
}
},
"required": ["name", "age", "email", "address"]
}
Used commandline:
$ datamodel-codegen --input test_json.json --output test_json.py --output-model-type pydantic_v2.BaseModel
Expected behavior
I want the following behavior by default
# generated by datamodel-codegen:
# filename: test_json.json
# timestamp: 2024-05-29T06:01:45+00:00
from __future__ import annotations
from typing import List, Optional
from pydantic import BaseModel, ConfigDict, EmailStr, conint, constr
class Address(BaseModel):
model_config = ConfigDict(
extra='forbid',
)
street: str
city: str
zipcode: constr(pattern=r'^[0-9]{5}(?:-[0-9]{4})?$')
class Model(BaseModel):
model_config = ConfigDict(
extra='forbid',
)
name: str
age: conint(ge=0)
email: EmailStr
address: Address
phoneNumbers: Optional[List[constr(pattern=r'^[0-9]{10}$')]] = None
What I got is this:
# generated by datamodel-codegen:
# filename: test_json.json
# timestamp: 2024-05-29T06:01:45+00:00
from __future__ import annotations
from typing import List, Optional
from pydantic import BaseModel, ConfigDict, EmailStr, conint, constr
class Address(BaseModel):
street: str
city: str
zipcode: constr(pattern=r'^[0-9]{5}(?:-[0-9]{4})?$')
class Model(BaseModel):
name: str
age: conint(ge=0)
email: EmailStr
address: Address
phoneNumbers: Optional[List[constr(pattern=r'^[0-9]{10}$')]] = None
Version:
22.04 LTS
3.10.12
current master
Additional context
Add any other context about the problem here.
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too