Describe the bug
When a Literal
field is generated from discriminator.mapping
in an OpenAPI spec (to Pydantic v2), a default value is not generated even with --use-one-literal-as-default
flag.
To Reproduce
Example schema:
openapi: "3.0.0"
info:
version: "0.0.1"
title: "Demo OpenAPI spec"
paths: {}
components:
schemas:
ResponseEvent:
discriminator:
propertyName: "type"
mapping:
start: "#/components/schemas/StartEvent"
end: "#/components/schemas/EndEvent"
oneOf:
- $ref: "#/components/schemas/StartEvent"
- $ref: "#/components/schemas/EndEvent"
StartEvent:
type: "object"
properties:
type:
type: "string"
required:
- type
EndEvent:
type: "object"
properties:
type:
type: "string"
required:
- type
Used commandline:
$ datamodel-codegen --input openapi.yml --input-file-type openapi --output model.py --output-model-type pydantic_v2.BaseModel --use-one-literal-as-default
Output:
# generated by datamodel-codegen:
# filename: openapi.yml
# timestamp: 2024-06-12T14:20:48+00:00
from __future__ import annotations
from typing import Union
from pydantic import BaseModel, Field, RootModel
from typing_extensions import Literal
class StartEvent(BaseModel):
type: Literal['start']
class EndEvent(BaseModel):
type: Literal['end']
class ResponseEvent(RootModel[Union[StartEvent, EndEvent]]):
root: Union[StartEvent, EndEvent] = Field(..., discriminator='type')
Expected behavior
The type
property of StartEvent
and EndEvent
should have default values because they are "single literal"s.
Version:
Additional context
Add any other context about the problem here.
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too