OpenAPITools / OpenAPITools/openapi-generator

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

Open
#13,714 3 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Bug Report Checklist
  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with DefaultCodegen.java, particularly getAllOfDescendants, createDiscriminator, and fromModel, using the provided schema and generation command to reproduce the recursion. Trace the Type, CreationDetails, UpdateDetails, and ResponseDetails references. Done means the Go generation no longer ends in a stack overflow for this cycle and regression coverage verifies the behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
go, java, openapi
Domain
api, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.