Describe the bug
$ref doesn't work when used with other fields.
To Reproduce
Example schema:
{
"definitions": {
"ipv4-addr": {
"description": "IPv4 address",
"markdownDescription": "IPv4 address",
"type": "string",
"pattern": "^(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(%[\\p{N}\\p{L}]+)?$"
},
"ipv6-addr": {
"description": "IPv6 address",
"markdownDescription": "IPv6 address",
"type": "string",
"pattern": "^((:|[0-9a-fA-F]{0,4}):)([0-9a-fA-F]{0,4}:){0,5}((([0-9a-fA-F]{0,4}:)?(:|[0-9a-fA-F]{0,4}))|(((25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])\\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])))(%[\\p{N}\\p{L}]+)?$"
}
},
"type": "object",
"properties": {
"ipv4-gw": {
"$ref": "#/definitions/ipv4-addr"
},
"ipv6-gw": {
"description": "IPv6 gateway address that will be set on a bridge used for the management network. Will be set to the first available IP address by default",
"markdownDescription": "IPv6 gateway address that will be set on a bridge used for the management network. Will be set to the first available IP address by default",
"$ref": "#/definitions/ipv6-addr"
}
},
"additionalProperties": false
}
Used commandline:
$ uvx --from datamodel-code-generator datamodel-codegen --input schema.json --input-file-type jsonschema --output models.py --output-model-type pydantic_v2.BaseModel --use-annotated --formatters ruff-format ruff-check
Expected behavior
ipv4-gw here has no other fields than $ref, which got converted into ipv4_gw: Annotated[Ipv4Addr | None, Field(alias="ipv4-gw")] = None.
ipv6-gw, on the other hand, got converted into
ipv6_gw: Annotated[
str | None,
Field(
alias="ipv6-gw",
description="IPv6 gateway address that will be set on a bridge used for the management network. Will be set to the first available IP address by default",
),
] = None
when it should've generated Ipv6Addr | None or at least preserve the pattern.
Version:
- OS: Linux
- Python version: 3.13
- datamodel-code-generator version: v0.42.2 - v0.53.0
Additional context
This bug was introduced in v0.42.2; v0.42.1 works fine. In addition, when run without --use-annotated it generates constr with pattern.
Describe the bug
$ref doesn't work when used with other fields.
To Reproduce
Example schema:
{ "definitions": { "ipv4-addr": { "description": "IPv4 address", "markdownDescription": "IPv4 address", "type": "string", "pattern": "^(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(%[\\p{N}\\p{L}]+)?$" }, "ipv6-addr": { "description": "IPv6 address", "markdownDescription": "IPv6 address", "type": "string", "pattern": "^((:|[0-9a-fA-F]{0,4}):)([0-9a-fA-F]{0,4}:){0,5}((([0-9a-fA-F]{0,4}:)?(:|[0-9a-fA-F]{0,4}))|(((25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])\\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])))(%[\\p{N}\\p{L}]+)?$" } }, "type": "object", "properties": { "ipv4-gw": { "$ref": "#/definitions/ipv4-addr" }, "ipv6-gw": { "description": "IPv6 gateway address that will be set on a bridge used for the management network. Will be set to the first available IP address by default", "markdownDescription": "IPv6 gateway address that will be set on a bridge used for the management network. Will be set to the first available IP address by default", "$ref": "#/definitions/ipv6-addr" } }, "additionalProperties": false }Used commandline:
Expected behavior
ipv4-gwhere has no other fields than$ref, which got converted intoipv4_gw: Annotated[Ipv4Addr | None, Field(alias="ipv4-gw")] = None.ipv6-gw, on the other hand, got converted intowhen it should've generated
Ipv6Addr | Noneor at least preserve the pattern.Version:
Additional context
This bug was introduced in v0.42.2; v0.42.1 works fine. In addition, when run without
--use-annotatedit generates constr with pattern.