Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RuntimeError: Discriminator type is not found #1832

Open
torazem opened this issue Feb 2, 2024 · 0 comments
Open

RuntimeError: Discriminator type is not found #1832

torazem opened this issue Feb 2, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@torazem
Copy link

torazem commented Feb 2, 2024

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:

  • OS: Ubuntu
  • Python version: 3.11.4
  • datamodel-code-generator version: 0.25.3

Additional context
None

@koxudaxi koxudaxi added the bug Something isn't working label Mar 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants