Skip to content

Schema reference in allOf block results in different class name from schema reference not in allOf block #2900

@declaresub

Description

@declaresub

Describe the bug
A same schema reference used in an allOf block results in a different class name from a schema reference not in an allOf block.

To Reproduce

Example schema:

openapi: 3.0.0
components:
  schemas:
    ACHClass:
      type: string
      title: ACHClass
      enum:
      - ccd
    Foo:
      type: object
      properties:
        ach_class:
          $ref: '#/components/schemas/ACHClass'
    Bar:
      type: object
      properties:
        ach_class:
          allOf:
          - $ref: '#/components/schemas/ACHClass'

Used commandline:

$ datamodel-codegen --input test_schema.yaml --input-file-type openapi --output-model-type pydantic_v2.BaseModel --target-python-version 3.14 --enum-field-as-literal all

Expected behavior
the output of the above command is

# generated by datamodel-codegen:
#   filename:  test_schema.yaml
#   timestamp: 2026-01-02T15:59:18+00:00

from typing import Literal

from pydantic import BaseModel, Field, RootModel


class ACHClass(RootModel[Literal['ccd']]):
    root: Literal['ccd'] = Field(..., title='ACHClass')


class Foo(BaseModel):
    ach_class: ACHClass | None = None


class AchClass(RootModel[ACHClass]):
    root: ACHClass


class Bar(BaseModel):
    ach_class: AchClass | None = None

I did not expect a subclass AchClass.

Version:

  • OS: Mac OS 15.7.1
  • Python version: 3.14.2
  • datamodel-code-generator version: 0.51.0

Additional context
Add any other context about the problem here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions