Skip to content

OpenAPI integer discriminator is generated as string type #3062

@kkinugasa

Description

@kkinugasa

Describe the bug
When using OpenAPI discriminator mapping with non-string values (for example integer enum values), generated models treat the discriminator field as a string.
As a result, the generated type for the discriminator does not match the OpenAPI schema type and can break runtime validation and type checking.

To Reproduce

Example schema:

{
  "openapi": "3.0.3",
  "info": {
    "title": "Minimal Integer Discriminator",
    "version": "1.0.0"
  },
  "paths": {},
  "components": {
    "schemas": {
      "Base": {
        "oneOf": [
          { "$ref": "#/components/schemas/Foo" },
          { "$ref": "#/components/schemas/Bar" }
        ],
        "discriminator": {
          "propertyName": "kind",
          "mapping": {
            "1": "#/components/schemas/Foo",
            "2": "#/components/schemas/Bar"
          }
        }
      },
      "Foo": {
        "type": "object",
        "properties": {
          "kind": {
            "type": "integer",
            "enum": [1]
          }
        },
        "required": ["kind"]
      },
      "Bar": {
        "type": "object",
        "properties": {
          "kind": {
            "type": "integer",
            "enum": [2]
          }
        },
        "required": ["kind"]
      }
    }
  }
}

Used commandline:

$ datamodel-codegen --input example.json --input-file-type openapi --output model.py

Output:

from __future__ import annotations

from enum import IntEnum
from typing import Literal

from pydantic import BaseModel, Field, RootModel


class Kind(IntEnum):
    integer_1 = 1


class Foo(BaseModel):
    kind: Literal['1']


class Kind1(IntEnum):
    integer_2 = 2


class Bar(BaseModel):
    kind: Literal['2']


class Base(RootModel[Foo | Bar]):
    root: Foo | Bar = Field(..., discriminator='kind')

Expected behavior
The discriminator field type should follow the schema type (integer), for example using integer literal values for each subtype (such as 1 and 2), instead of string-based discriminator typing.

Version:

  • OS: macOS
  • Python version: 3.13.11
  • datamodel-code-generator version: 0.55.1.dev6+g7e1a5c751

Additional context
OpenAPI 3.1 Discriminator Object states:

Mapping keys MUST be string values, but tooling MAY convert response values to strings for comparison.

I understand this as: discriminator mapping keys to be string values, but the discriminator properties itself does not have to be string values.

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