Skip to content

Description not propagated to enum fields docstring #3003

@aschamberger

Description

@aschamberger

Describe the bug

The description fields of an JSON Schema enum declaration are not used for the docstrings. When enums are defined via anyOf+const (as opposed to a simple enum array), the per-member description fields from the schema are not propagated to the generated enum member fields.

To Reproduce

Example schema:

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$defs": {
    "JobExecutionMode": {
      "anyOf": [
        {
          "const": 0,
          "title": "SimulationMode",
          "description": "Machine running in simulation mode with no workpiece involved."
        },
        {
          "const": 1,
          "title": "TestMode",
          "description": "Machine running in test mode with a workpiece involved."
        },
        {
          "const": 2,
          "title": "ProductionMode",
          "description": "Machine running in production mode."
        }
      ],
      "description": "https://reference.opcfoundation.org/Machinery/Jobs/v100/docs/9.1"
    }
}

Output

class JobExecutionMode(IntEnum):
    """
    https://reference.opcfoundation.org/Machinery/Jobs/v100/docs/9.1
    """

    SIMULATION_MODE = 0
    TEST_MODE = 1
    PRODUCTION_MODE = 2

Used code:

    config = JSONSchemaParserConfig(
        target_python_version=PythonVersion.PY_314,
        use_union_operator=True,
        use_standard_collections=True,
        formatters=[Formatter.RUFF_FORMAT, Formatter.RUFF_CHECK],
        custom_template_dir=pathlib.Path(__file__).parent / "template",
        data_model_type=data_model_types.data_model,
        data_model_root_type=data_model_types.root_model,
        data_model_field_type=data_model_types.field_model,
        data_type_manager_type=data_model_types.data_type_manager,
        dump_resolve_reference_action=data_model_types.dump_resolve_reference_action,
        field_constraints=True,
        keyword_only=True,
        use_subclass_enum=True,
        capitalise_enum_members=True,
        snake_case_field=True,
        use_field_description=True,
        use_schema_description=True,
        use_title_as_name=True,
        additional_imports=imports,
        base_class=base_class,
        extra_template_data=defaultdict(dict, extra_template_data),
    )
    parser = JsonSchemaParser(schema_file_path.read_text(), config=config)
    result = parser.parse()
    out = dataclass_path / (
        schema_file_path.name.replace("".join(schema_file_path.suffixes), ".py")
    )

Expected behavior
A clear and concise description of what you expected to happen.

class JobExecutionMode(IntEnum):
    """
    https://reference.opcfoundation.org/Machinery/Jobs/v100/docs/9.1
    """

    SIMULATION_MODE = 0
    """Machine running in simulation mode with no workpiece involved."""
    TEST_MODE = 1
    """Machine running in test mode with a workpiece involved."""
    PRODUCTION_MODE = 2
    """Machine running in production mode."""

Version:

  • OS: Ubuntu 24.04
  • Python version: 3.14
  • datamodel-code-generator version: 0.54.0

Additional context
Add any other context about the problem here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions