Skip to content

StackOverflowError with object having an array of itself #332

Open

Description

@mathecruz

Actually when using this OAPI specification (on quarkus-openapi-generator-server side):

{
  "openapi": "3.0.3",
  "info": {
    "title": "Recursive Example API",
    "version": "1.0.0"
  },
  "paths": {
    "/example": {
      "get": {
        "summary": "Get Something",
        "responses": {
          "200": {
            "description": "Successful response",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Something"
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "Something": {
        "type": "object",
        "properties": {
          "someGroup": {
            "type": "array",
            "items": {
              "allOf": [
                {
                  "$ref": "#/components/schemas/Something"
                }
              ]
            }
          }
        }
      }
    }
  }
}

We are getting a StackOverflowException.

You can reproduce this one, using my branch https://github.com/mcruzdev/apicurio-codegen/tree/issue-stackoverflow and executing the test testConstraintParameters_allOfSelfRef from an IDE.

Activity

  1. self-assigned this
    on Mar 7, 2025
  2. EricWittmann commented on Apr 8, 2025

    @EricWittmann
    Contributor

    Moving this issue to apicurio-codegen. I've tracked down the issue to the OpenApiAllOfProcessor in that repository. I do not believe there is an issue in apicurio-data-models.

  3. EricWittmann commented on Apr 8, 2025

    @EricWittmann
    Contributor

    Hey @mcruzdev - a stack overflow is obviously not an acceptable outcome. :)

    That said, what do you expect to happen for a recursive type reference in JSON Schema when generating the JAX-RS layer? We could, of course, put in something to detect recursive types and simply fail. I'm not clear on what else we could do...

    wdyt?

  4. mathecruz commented on Apr 8, 2025

    @mathecruz
    ContributorAuthor

    I think we need to generate something like it:

    import com.fasterxml.jackson.annotation.JsonInclude;
    import com.fasterxml.jackson.annotation.JsonProperty;
    
    import java.util.List;
    
    @JsonInclude(JsonInclude.Include.NON_NULL)
    public class Something {
    
        @JsonProperty("someGroup")
        private List<Something> someGroup;
    
        public Something() {
        }
    
        public List<Something> getSomeGroup() {
            return someGroup;
        }
    
        public void setSomeGroup(List<Something> someGroup) {
            this.someGroup = someGroup;
        }
    
        @Override
        public String toString() {
            return "Something{" +
                    "someGroup=" + someGroup +
                    '}';
        }
    }
  5. EricWittmann commented on Apr 10, 2025

    @EricWittmann
    Contributor

    Yeah I suppose that makes sense. We're using jsonschema2pojo to create the java beans from the schema types. It will probably require some experimenting to figure out how to get something like that working.

    I'm just guessing, but perhaps it can already handle simple recursive cycles in the types, but the "AllOf" introduces an additional challenge. Let's expand on your example a bit to get this:

    "Something": {
            "type": "object",
            "properties": {
              "someGroup": {
                "type": "array",
                "items": {
                  "allOf": [
                    {
                      "$ref": "#/components/schemas/Something"
                    },
                    {
                      "$ref": "#/components/schemas/SomethingElse"
                    },
                    {
                        "type": "object",
                        "properties": {
                            "foo": {
                                "type": "string"
                            },
                            "bar": {
                                "type": "string"
                            },
                        }
                    }
                  ]
                }
              }
            }
          }

    In that case, the someGroup property is a list of Things where each Thing has all of the properties of Something plus all the properties of SomethingElse *and the foo and bar properties.

    What we're trying to do right now in the code generator is preprocess the JSON Schema to flatten out the type of someGroup to get rid of the allOf and merge all of the properties into a new object definition. The problem is that Something is self-recursive so the preprocessor gets into a loop.

    We can obviously try to detect the cycle and skip the allOf preprocessing when we find one. That will probably result in jsonschema2pojo failing or producing something not usable.

    I think the first thing to do here may be to create a few tests with different recursive schemas defined. Then see what we get and iteratively work from there.

    wdyt?

  6. mathecruz commented on Apr 23, 2025

    @mathecruz
    ContributorAuthor

    I think the first thing to do here may be to create a few tests with different recursive schemas defined. Then see what we get and iteratively work from there.

    It is really a challenge, and I think too that it is a good start way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions