Skip to content

use-title-as-name sometimes get ignored #2887

@hgl

Description

@hgl

Describe the bug
For types like in the reproduction, titles are ignored

To Reproduce

Example schema:

{
    $schema: "https://json-schema.org/draft/2020-12/schema",
    $defs: {
      Foo: {
        type: "object",
        properties: {
          array: {
            title: "MyName",
            type: "array",
            items: {
              type: "string",
            },
          },
          object: {
            title: "MyObject",
            type: "object",
            additionalProperties: {
              type: "string",
            },
          },
          enum: {
            title: "MyEnum",
            enum: ["foo", "bar"],
          },
          oneOf: {
            title: "MyOneOf",
            oneOf: [
              {
                type: "string",
              },
              {
                type: "number",
              },
            ],
          },
        },
      },
    },
  }

Used commandline:

[tool.datamodel-codegen]
input-file-type = "jsonschema"
output-model-type = "typing.TypedDict"
target-python-version = "3.13"
use-union-operator = true
skip-root-model = true
use-standard-collections = true
use-frozen-field = true
use-title-as-name = true

Expected behavior
I'd expect it to create type aliases like

type MyName = list[str]
type MyObject = dict[str, str]
...

instead, titles are simply ignored

class Foo(TypedDict):
    array: NotRequired[list[str]]
    object: NotRequired[dict[str, str]]
    enum: NotRequired[Literal['foo', 'bar']]
    oneOf: NotRequired[str | float]

Version:

  • OS: [e.g. iOS]
  • Python version:
  • datamodel-code-generator version: [e.g. 22]

Additional context
Add any other context about the problem here.

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