OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.24k stars 6.43k forks source link

[BUG] [GO] Stack Overflow when cycle present in spec #13714

Open AniketK-Crest opened 1 year ago

AniketK-Crest commented 1 year ago

Bug Report Checklist

Description

I have provided a minimal version of the schema below with what I am facing the issue. In a way, it is similar to https://github.com/OpenAPITools/openapi-generator/issues/4904. It works fine if I remove one of the items from AllOf from any attribute or remove the discriminator. However, I do not own the schema so it would not be possible for me to modify it.

Below is the error that I am getting:

Exception in thread "main" java.lang.RuntimeException: Could not process model 'Type'.Please make sure that your schema is correct!
        at org.openapitools.codegen.DefaultGenerator.generateModels(DefaultGenerator.java:518)
        at org.openapitools.codegen.DefaultGenerator.generate(DefaultGenerator.java:912)
        at org.openapitools.codegen.cmd.Generate.execute(Generate.java:465)
        at org.openapitools.codegen.cmd.OpenApiGeneratorCommand.run(OpenApiGeneratorCommand.java:32)
        at org.openapitools.codegen.OpenAPIGenerator.main(OpenAPIGenerator.java:66)
Caused by: java.lang.RuntimeException: Stack overflow hit when looking for Type an infinite loop starting and ending at ResponseDetails was seen
        at org.openapitools.codegen.DefaultCodegen.getAllOfDescendants(DefaultCodegen.java:3351)
        at org.openapitools.codegen.DefaultCodegen.createDiscriminator(DefaultCodegen.java:3420)
        at org.openapitools.codegen.DefaultCodegen.fromModel(DefaultCodegen.java:2906)
        at org.openapitools.codegen.DefaultGenerator.processModels(DefaultGenerator.java:1291)
        at org.openapitools.codegen.DefaultGenerator.generateModels(DefaultGenerator.java:513)
        ... 4 more
openapi-generator version

6.2.0

OpenAPI declaration file content
{
    "openapi": "3.0.0",
    "info": {
        "title": "Recursion Bug",
        "version": "2.0.0"
    },
    "paths": {},
    "components": {
        "schemas": {
            "Type": {
                "type": "object",
                "properties": {
                    "Type": { "type": "string" }
                },
                "discriminator": {
                    "propertyName": "Type",
                    "mapping": { "something": "#/components/schemas/CreationDetails" }
                }
            },
            "CreationDetails": {
                "allOf": [
                    { "$ref": "#/components/schemas/Type" }
                ]
            },
            "UpdateDetails": {
                "allOf": [
                    { "$ref": "#/components/schemas/CreationDetails" }
                ]
            },
            "ResponseDetails": {
                "allOf": [
                    { "$ref": "#/components/schemas/CreationDetails" },
                    { "$ref": "#/components/schemas/UpdateDetails" }
                ]
            }
        }
    }
}
Generation Details
Steps to reproduce
openapi-generator-cli generate -i https://gist.githubusercontent.com/AniketK-Crest/06c5248a2d1f34bc544c9ce758e7d2e3/raw/14e024cba487ccc8a2d682918ebfbd4792e6b96a/recursion-bug.json -g go -o ./Generated/recursion-bug-go-client
Related issues/PRs

https://github.com/OpenAPITools/openapi-generator/issues/4904 https://github.com/OpenAPITools/openapi-generator/pull/4906 https://github.com/OpenAPITools/openapi-generator/issues/5229

Suggest a fix
wing328 commented 1 year ago

have you tested with other generators such as java, csharp-netcore to see if the issue impacts other generators as well?

AniketK-Crest commented 1 year ago

Hi @wing328, I tried with java, python, ruby, csharp-netcore, and go-server. it successfully generates the ruby client and go server but fails with others.

IB-AUTEN-rvillalba commented 1 year ago

Some option to fix this problem in 6.6.0?