Skip to content

patternProperties key constraints doesn't get preserved with --use-annotated #2988

@juliusliu02

Description

@juliusliu02

Describe the bug
When converting patternProperties for object to pydantic v2 models, the key constraints only show up when not using --use-annotated.

To Reproduce

Example schema:

{
  "type": "object",
  "patternProperties": {
    "^pattern.*$": {
      "type": "string"
    }
  },
  "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

Key should be annotated with pattern.

dict[Annotated[str, Field(pattern="^pattern.*$")], str]

instead of: dict[str, str].

Version:

  • OS: Linux
  • Python version: 3.13
  • datamodel-code-generator version: 0.53.0

Additional context
Without --use-annotated, it does preserve the pattern as dict[constr(pattern=r"^pattern.*$"), str].

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions