koxudaxi / datamodel-code-generator

Pydantic model and dataclasses.dataclass generator for easy conversion of JSON, OpenAPI, JSON Schema, and YAML data sources.
https://koxudaxi.github.io/datamodel-code-generator/
MIT License
2.78k stars 305 forks source link

RuntimeError: Discriminator type is not found #1832

Open torazem opened 9 months ago

torazem commented 9 months ago

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": "ecosystem@atlassian.com"
        },
        "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

rlefkowitz commented 1 month ago

@torazem Amazingly, I am trying to do precisely the same thing: convert the Jira openapi spec to pydantic models. Did you have any luck with the conversion? If so, did you find a workaround to make this package work or did you find an alternative? Hope you see this. Thanks!