jsonschema2pojo icon indicating copy to clipboard operation
jsonschema2pojo copied to clipboard

Get Stackoverflow error if schema contains trees/cycle dependencies for children

Open LeatherDeerAU opened this issue 3 years ago • 3 comments
trafficstars

Is it possible to have a field inside an array object pointing to an external array? Such a scheme fails with StackOverflowError.

{
  "type": "array",
  "items": {
    "type": "object",
    "properties": {
      "folders": {
        "$ref": "#"
      }
    }
  }
}

But this schema locally generates classes:

{
  "type": "object",
  "additionalProperties": false,
  "properties": {
    "folders": {
      "type": "array",
      "items": {
        "$ref": "#/definitions/folder"
      }
    }
  },
  "definitions": {
    "folder": {
      "type": "object",
      "properties": {
        "folders": {
          "type": "array",
          "items": {
            "$ref": "#/definitions/folder"
          }
        }
      }
    }
  }
}

LeatherDeerAU avatar Feb 09 '22 14:02 LeatherDeerAU

Could you write a gist and compile an example model?

eirnym avatar Feb 09 '22 15:02 eirnym

I want to generate the following class:

@Generated("jsonschema2pojo")
public class MyFolder {

    @JsonProperty("folders")
    private List<Folder> folders = new ArrayList<Folder>();
}

@Generated("jsonschema2pojo")
public class Folder {
    @JsonProperty("folders")
    private List<Folder> folders = new ArrayList<Folder>();
}

The second scheme from the first message successfully does this. The first scheme throws an error. It seems to me that this is the same scheme, written in different ways

LeatherDeerAU avatar Feb 10 '22 14:02 LeatherDeerAU

Yes, looks like there's a bug here in how the # reference is resolved. The first schema should just generate:

@Generated("jsonschema2pojo")
public class Folder {
    @JsonProperty("folders")
    private List<Folder> folders = new ArrayList<Folder>();
}

We have a variety of these self references in our tests, but something about this example is different. You're right it should work. We should add a test for this and fix.

joelittlejohn avatar Feb 11 '22 19:02 joelittlejohn