Describe the bug
When I try to generate models from an OpenAPI schema containing discriminators, I get an error: RuntimeError: Discriminator type is not found
.
To Reproduce
Get the OpenAPI spec for the Jira REST API, or the reduced example below. Try to convert it into Pydantic models using datamodel-code-generator
.
Example schema:
{
"components": {
"schemas": {
"CustomContextVariable": {
"additionalProperties": false,
"discriminator": {
"mapping": {
"issue": "#/components/schemas/IssueContextVariable",
"json": "#/components/schemas/JsonContextVariable",
"user": "#/components/schemas/UserContextVariable"
},
"propertyName": "type"
},
"oneOf": [
{
"$ref": "#/components/schemas/UserContextVariable"
},
{
"$ref": "#/components/schemas/IssueContextVariable"
},
{
"$ref": "#/components/schemas/JsonContextVariable"
}
],
"properties": {
"type": {
"description": "Type of custom context variable.",
"type": "string"
}
},
"required": [
"type"
],
"type": "object"
},
"JsonContextVariable": {
"description": "A JSON object with custom content.",
"properties": {
"type": {
"description": "Type of custom context variable.",
"type": "string"
},
"value": {
"description": "A JSON object containing custom content.",
"type": "object"
}
},
"required": [
"type"
],
"type": "object"
},
"IssueContextVariable": {
"description": "An [issue](https://developer.atlassian.com/cloud/jira/platform/jira-expressions-type-reference#issue) specified by ID or key. All the fields of the issue object are available in the Jira expression.",
"properties": {
"id": {
"description": "The issue ID.",
"format": "int64",
"type": "integer"
},
"key": {
"description": "The issue key.",
"type": "string"
},
"type": {
"description": "Type of custom context variable.",
"type": "string"
}
},
"required": [
"type"
],
"type": "object"
},
"UserContextVariable": {
"description": "A [user](https://developer.atlassian.com/cloud/jira/platform/jira-expressions-type-reference#user) specified as an Atlassian account ID.",
"properties": {
"accountId": {
"description": "The account ID of the user.",
"type": "string"
},
"type": {
"description": "Type of custom context variable.",
"type": "string"
}
},
"required": [
"accountId",
"type"
],
"type": "object"
}
}
},
"info": {
"contact": {
"email": "[email protected]"
},
"description": "Jira Cloud platform REST API documentation",
"license": {
"name": "Apache 2.0",
"url": "http://www.apache.org/licenses/LICENSE-2.0.html"
},
"termsOfService": "http://atlassian.com/terms/",
"title": "The Jira Cloud platform REST API",
"version": "1001.0.0-SNAPSHOT-157e8201994ee15df4ad7fc2b41795e7c88fbc36"
},
"openapi": "3.0.1",
"paths": {},
"servers": [
{
"url": "https://your-domain.atlassian.net"
}
],
"tags": []
}
Used commandline:
$ datamodel-codegen --input test-openapi.json --input-file-type openapi --output models.py --output-model-type pydantic_v2.BaseModel
Result:
Traceback (most recent call last):
...
File ".../python3.11/site-packages/datamodel_code_generator/parser/base.py", line 772, in __apply_discriminator_type
raise RuntimeError(
RuntimeError: Discriminator type is not found. test-openapi.json#/components/schemas/CustomContextVariable#-datamodel-code-generator-#-oneOf-#-special-##-datamodel-code-generator-#-oneOfCommon-#-special-#/0
Expected behavior
The command should generate a file models.py
containing pydantic models.
Version:
Additional context
None
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