【问题标题】:Victools JSON schema generator creates two definitions for the same classVictools JSON 模式生成器为同一个类创建两个定义
【发布时间】:2022-08-16 16:50:53
【问题描述】:

使用来自 victools 的 JSON 模式生成器(这会生成基于 Java 类的 JSON 模式),如果我将两个类与一个共同的超类型相关联,并且使用了 @JsonTypeName,我会得到一个奇怪的结果。

请考虑以下代码:

@JsonTypeName(\"Root\")
public class Root {
    private String rootName;
    ... 
    private List<SuperClass1> superclass1 = new ArrayList<SuperClass1>();
    ...
}

@JsonTypeName(\"SuperClass1\")
@JsonTypeInfo(  use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY, property = \"type\")
@JsonSubTypes({ @JsonSubTypes.Type(value = Sub1.class, name = \"Sub1\"),
@JsonSubTypes.Type(value = Sub2.class, name = \"Sub2\")})
public abstract class SuperClass1 {
    String name;
    int someThingElse;
    
    Root root;

    ...
}
@JsonTypeName(\"Sub1\")
public class Sub1 extends SuperClass1 {
    String sub1;
    ...
    Sub2 sub2;
    ...
}
@JsonTypeName(\"Sub2\")
public class Sub2 extends SuperClass1{
    String sub2;
    ...
}

生成以下 JSON 模式:

{
  \"$schema\" : \"http://json-schema.org/draft-07/schema#\",
  \"definitions\" : {
    \"Sub1\" : {
      \"type\" : \"object\",
      \"properties\" : {
        \"root\" : {
          \"$ref\" : \"#\"
        },
        \"sub1\" : {
          \"type\" : \"string\"
        },
        \"sub2\" : {
          \"$ref\" : \"#/definitions/Sub2-2\"
        }
      }
    },
    \"Sub2-1\" : {
      \"type\" : \"object\",
      \"properties\" : {
        \"root\" : {
          \"$ref\" : \"#\"
        },
        \"sub2\" : {
          \"type\" : \"string\"
        }
      }
    },
    \"Sub2-2\" : {
      \"allOf\" : [ {
        \"$ref\" : \"#/definitions/Sub2-1\"
      }, {
        \"type\" : \"object\",
        \"properties\" : {
          \"type\" : {
            \"const\" : \"json_test.Sub2\"
          }
        },
        \"required\" : [ \"type\" ]
      } ]
    }
  },
  \"type\" : \"object\",
  \"properties\" : {
    \"rootName\" : {
      \"type\" : \"string\"
    },
    \"superclass1\" : {
      \"type\" : \"array\",
      \"items\" : {
        \"anyOf\" : [ {
          \"allOf\" : [ {
            \"$ref\" : \"#/definitions/Sub1\"
          }, {
            \"type\" : \"object\",
            \"properties\" : {
              \"type\" : {
                \"const\" : \"json_test.Sub1\"
              }
            },
            \"required\" : [ \"type\" ]
          } ]
        }, {
          \"$ref\" : \"#/definitions/Sub2-2\"
        } ]
      }
    }
  }
}

如果Sub1 类的属性引用Sub2 类,并且使用@JsonTypeInfo( use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY, property = \"type\"),则会发生这种情况。

有人可以解释一下为什么它会在定义部分生成Sub2-1Sub2-2,而不仅仅是Sub2

谢谢,

——雅普

  • 嗨 Jaap,由于生成器是高度可配置的,如果您可以分享您的配置,将会有所帮助。但我可以笼统地回答你的问题。

标签: java jsonschema


【解决方案1】:

不同之处在于模式生成器的内部工作原理:

  • Sub2-1 代表“原始”类型,如果你愿意的话
  • Sub2-2 代表 Sub2-1 带有杰克逊添加的附加“类型”属性

通常,如果Sub2-1 只被引用一次,它应该被内联到第二个模式中,因此允许将第二个模式标记为Sub2。至少对于似乎按预期工作的Sub1

我建议您在victools GitHub 存储库上提供一个完整的可重现示例作为“问题”,我可以看看这是一个可以轻松修复的错误,或者至少可以通过您的特定设置来解释。

注意:我是那个库的维护者。

【讨论】:

    猜你喜欢
    • 2016-03-22
    • 2020-01-15
    • 2019-03-02
    • 1970-01-01
    • 1970-01-01
    • 2021-12-17
    • 1970-01-01
    • 1970-01-01
    • 2020-04-02
    相关资源
    最近更新 更多