datamodel-code-generator
datamodel-code-generator copied to clipboard
`required` fields declared in an `allof` are missing in the generated code
Describe the bug
required fields declared in an allof are missing in the generated python code.
If this is not a bug, please let me know if I'm doing something wrong.
To Reproduce
Example schema:
ProjectedPet:
type: object
properties:
id:
type: integer
format: int64
name:
type: string
Pet:
allOf:
- $ref: "#/components/schemas/ProjectedPet"
- type: object
required:
- id
- name
- opts
properties:
tag:
type: string
opts:
type: array
items:
type: string
Used commandline:
datamodel-codegen --input api.yaml --output model.py --input-file-type openapi --use-annotated --collapse-root-models --use-standard-collections --capitalize-enum-members --target-python-version 3.10 --output-model-type pydantic_v2.BaseModel
Actual behavior
class ProjectedPet(BaseModel):
id: Optional[int] = None
name: Optional[str] = None
class Pet(ProjectedPet):
tag: Optional[str] = None
opts: list[str]
Expected behavior
class ProjectedPet(BaseModel):
id: Optional[int] = None
name: Optional[str] = None
class Pet(ProjectedPet):
id: int
name: str
tag: Optional[str] = None
opts: list[str]
Version:
- OS: MacOS 14.6.1
- Python version: 3.11
- datamodel-code-generator version: 0.25.9
I think I ran into a similar problem with a JSON Draft 07 schema. Unfortunately I can't share it here, but basically it's structured as follows:
{
"$schema": "http://json-schema.org/draft-07/schema",
...
"definitions": {},
"properties": {
"example": {
"type": "object",
...
"required": [
...
],
"properties": {,
"example_child": {
"type": "array",
"title": "...",
"since": "...",
"items": {
"type": "object",
...
"required": [
...
],
"properties": {
...
},
"allOf": [
{
"if": {
"properties": {
"example3": {
"properties": {
"isDefault": {
"const": true
}
}
}
}
},
"then": {
"properties": {
"example4": {
"required": [
...
],
"properties": {
...
},
"not": {
"anyOf": ...
}
}
}
}
}
]
}
}
}
}
}
}
Generated model with: datamodel-codegen --input schema.json --input-file-type jsonschema --output model.py --output-model-type pydantic_v2.BaseModel
As far as I can see, all properties under properties.allOf are not represented in the model.
If a usable example would be helpful, I can provide one.
I'm having a similar issue
type: object
properties:
current_settings:
allOf:
- $ref: "#/$defs/settings"
- required:
- x
- y
- z
modified_settings:
$ref: "#/$defs/settings"
required:
- current_settings
- modified_settings
$defs:
settings:
type: object
properties:
x:
type: integer
y:
type: integer
z:
type: integer
Expected:
class ModifiedSettings(BaseModel):
x: Optional[int] = None
y: Optional[int] = None
z: Optional[int] = None
class CurrentSettings(BaseModel):
x: int
y: int
z: int
class Foo(BaseModel):
current_settings: CurrentSettings
modified_settings: ModifiedSettings
Actual:
class Settings(BaseModel):
x: Optional[int] = None
y: Optional[int] = None
z: Optional[int] = None
class Foo(SparkModel):
current_settings: Settings
modified_settings: Settings