jsonschema-generator icon indicating copy to clipboard operation
jsonschema-generator copied to clipboard

`Option.ALLOF_CLEANUP_AT_THE_END` doesn't reduce `allOf` when the class has `@JsonSubTypes`

Open tearfur opened this issue 4 months ago • 2 comments

I think I found a case where Option.ALLOF_CLEANUP_AT_THE_END doesn't reduce an allOf when it should.

This is reproducible on 995a71eaf7a9a05cc2e335f8a7821b4a9019fa1b.

I expected a schema like this:

{
  "$schema" : "https://json-schema.org/draft/2020-12/schema",
  "type" : "object",
  "properties" : {
    "tag" : {
      "type" : "string",
      "const" : "tag1"
    }
  },
  "required" : [ "tag" ]
}

But I got this instead:

{
  "$schema" : "https://json-schema.org/draft/2020-12/schema",
  "allOf" : [ {
    "type" : "object",
    "properties" : {
      "tag" : {
        "type" : "string"
      }
    }
  }, {
    "type" : "object",
    "properties" : {
      "tag" : {
        "const" : "tag1"
      }
    },
    "required" : [ "tag" ]
  } ]
}

Code:

SchemaGeneratorConfigBuilder configBuilder = new SchemaGeneratorConfigBuilder(SchemaVersion.DRAFT_2020_12, OptionPreset.PLAIN_JSON)
        .with(new JacksonModule());
SchemaGeneratorConfig config = configBuilder.build();
System.out.println(new SchemaGenerator(config).generateSchema(Tagged.class).toPrettyString());
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXISTING_PROPERTY, property = "tag", visible = true)
@JsonSubTypes({
        @JsonSubTypes.Type(value = Tagged1.class, name = "tag1")
})
static abstract class Tagged {
    private final String tag;

    Tagged(String tag) {
        this.tag = tag;
    }

    public String getTag() {
        return this.tag;
    }
}

static class Tagged1 extends Tagged {
    public Tagged1() {
        super("tag1");
    }
}

tearfur avatar Aug 26 '25 06:08 tearfur

Hello, i am facing the same problem Have you found any workaround ?

eldinh avatar Sep 28 '25 17:09 eldinh

I had to use JsonTypeInfo.As.PROPERTY instead of JsonTypeInfo.As.EXISTING_PROPERTY.

tearfur avatar Sep 30 '25 11:09 tearfur