datamodel-code-generator
datamodel-code-generator copied to clipboard
Applying $ref defaults in JSON Schema
Is your feature request related to a problem? Please describe.
If a $ref definition contains a default, this is not being passed to the property using the $ref.
E.g.,
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Person",
"type": "object",
"definitions": {
"teamType": {
"description": "Person team",
"type": "string",
"enum": [
"Department",
"Division",
"BusinessUnit",
"Organization"
],
"default": "Department"
}
},
"properties": {
"firstName": {
"type": "string",
"description": "The person's first name."
},
"team": {
"$ref": "#/definitions/teamType"
},
"anotherTeam": {
"$ref": "#/definitions/teamType",
"default": "Department"
}
}
}
Converts to
class TeamType(Enum):
Department = 'Department
Division = 'Division'
BusinessUnit = 'BusinessUnit'
Organization = 'Organization'
class Person(BaseModel):
firstName: Optional[str] = Field(None, description="The person's first name.")
team: Optional[TeamType] = None
anotherTeam: Optional[TeamType] = 'Department'
Describe the solution you'd like
In the example above, team should also have Department as default as specified in the $ref
Describe alternatives you've considered Keep as-is
Additional context Add any other context or screenshots about the feature request here.
We'd be willing to give this a go. Would love to hear your thoughts though first, and any pointers that might help in the implementation.
My first thought would be to add this on add_ref, passing a new field to Reference about the original default, and validating that before using field.default.
Might not be the best place though. Also, not sure if reading the JSON Schema from the path and parsing the definitions is the right approach.
Thanks!