【问题标题】:Maven plugin jsonschema2pojo-maven-plugin not generating pojos for all the definitionsMaven 插件 jsonschema2pojo-maven-plugin 没有为所有定义生成 pojos
【发布时间】:2020-06-14 03:57:12
【问题描述】:

我们使用 jsonschema2pojo-maven-plugin 从 json 模式文件生成 java pojos。该插件正在为从架构中引用的定义生成 pojo,但不是为所有定义。

是否有任何选项可以让插件为 schmea 中的所有定义生成 pojo?

下面是使用的pom插件配置和schema定义,这里是插件

  • 仅为产品定义生成 POJO
  • 不为产品子类定义(“proprietaryProduct”和“thirdPartyProduct”)生成 POJO

这就是问题所在,我们还需要为这些子类定义生成 pojos

Maven 插件配置:

    <plugin>
        <groupId>org.jsonschema2pojo</groupId>
        <artifactId>jsonschema2pojo-maven-plugin</artifactId>
        <executions>
            <execution>
                <id>generate-models</id>
                <configuration>
                    <targetPackage>com.xyz.abc</targetPackage>
                    <useCommonsLang3>true</useCommonsLang3>
                </configuration>
                <goals>
                    <goal>generate</goal>
                </goals>
            </execution>
        </executions>
    </plugin>

Json 架构:

{
  "$schema":"http://json-schema.org/draft-04/schema",
  "definitions":{
    "product":{
      "type":"object",
      "properties":{
        "type":{
          "enum":[
            "THIRD_PARTY",
            "PROPRIETARY"
          ],
          "type":"string"
        },
        "price":{
          "type":"string"
        }
      },
      "additionalProperties": false
    },
    "proprietaryProduct": {
      "type": "object",
      "properties": {
        "batchName": {
          "type": "string"
        }
      },
      "extends": "#/definitions/product"
    },
    "thirdPartyProduct": {
      "type": "object",
      "properties": {
        "thirdPartyName": {
          "type": "string"
        }
      },
      "extends": "#/definitions/product"
    }
  },
  "type":"object",
  "properties":{
    "product":{
      "type":"object",
      "$ref":"#/definitions/product"
    }
  },
  "additionalProperties":false
}

【问题讨论】:

  • 您会收到哪些日志消息。你有没有用 -X 详细模式运行 maven 来查看插件在做什么?
  • Maven 日志没有提供太多线索——它只检测产品类定义,而不是子类定义。但是,当我使用任何子类定义(如“$ref”:“#/definitions/proprietaryProduct”)在架构中添加引用时 - 正在创建相应的子类 POJO。

标签: java json maven jsonschema jsonschema2pojo


【解决方案1】:

我设法在一个新的 maven 项目中使用自定义模式规则工厂生成定义的未引用部分:

package my.package;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.sun.codemodel.JClassContainer;
import com.sun.codemodel.JType;
import org.jsonschema2pojo.Schema;
import org.jsonschema2pojo.rules.Rule;
import org.jsonschema2pojo.rules.RuleFactory;
import org.jsonschema2pojo.rules.SchemaRule;

import java.util.Iterator;

public class JsonSchemaRuleFactory extends RuleFactory {

    @Override
    public Rule<JClassContainer, JType> getSchemaRule() {
        return new MySchemaRule(this);
    }

    private class MySchemaRule extends SchemaRule {

        public MySchemaRule(JsonSchemaRuleFactory jsonSchemaRuleFactory) {
            super(jsonSchemaRuleFactory);
        }

        @Override
        public JType apply(String nodeName, JsonNode schemaNode, JsonNode parent, JClassContainer generatableType, Schema schema) {
            final JType apply = super.apply(nodeName, schemaNode, parent, generatableType, schema);

            final JsonNode definitions = schemaNode.get("definitions");
            if (definitions != null && definitions.isObject()) {
                ObjectNode objectNode = (ObjectNode) definitions;
                final Iterator<String> nodeIterator = objectNode.fieldNames();
                while (nodeIterator.hasNext()) {
                    final String name = nodeIterator.next();
                    try {
                        final ObjectNode node = (ObjectNode) objectNode.get(name);
                        final Schema currentSchema = getSchemaStore().create(schema, "#/definitions/" + name, getGenerationConfig().getRefFragmentPathDelimiters());
                        getSchemaRule().apply(name, node, schemaNode, generatableType.getPackage(), currentSchema);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }

            return apply;
        }
    }
}

新的专用 maven 项目:

<project>
    <groupId>my.group</groupId>
    <artifactId>jsonschema-with-all-definitions</artifactId>
    <version>1.0.0</version>
    // ...
    <dependencies>
        <dependency>
            <groupId>org.jsonschema2pojo</groupId>
            <artifactId>jsonschema2pojo-core</artifactId>
            <version>1.0.2</version>
        </dependency>
    </dependencies>
</project>

然后我修改了jsonschema2pojo-maven-plugin的配置,如下图:

                    <plugin>
                        <groupId>org.jsonschema2pojo</groupId>
                        <artifactId>jsonschema2pojo-maven-plugin</artifactId>
                        <version>1.0.2</version>
                        <executions>
                            <execution>
                                <id>generateClassesFromSchema</id>
                                <phase>generate-sources</phase>
                                <goals>
                                    <goal>generate</goal>
                                </goals>

                            </execution>
                        </executions>
                        <configuration>
                            // ...                            
<customRuleFactory>my.package.JsonSchemaRuleFactory</customRuleFactory>
                        </configuration>

                        <dependencies>
                            <dependency>
                                <groupId>my.group</groupId>
                                <artifactId>jsonschema-with-all-definitions</artifactId>
                                <version>1.0.0</version>
                            </dependency>
                        </dependencies>
                    </plugin>

由于 jsonschema 不是我的,我无法编辑/更正它们,我使用了我在 ObjectMapper 中注册的 MixIns 来添加生成的类中缺少的一些注释,例如 @JsonTypeInfo 或其他。

希望对你有帮助!

【讨论】:

    【解决方案2】:

    发布此答案(作为一种解决方法),因为它可能对面临同样挑战的人有所帮助。

    在研究并阅读了 jsonschema2pojo-maven-plugin:generate 文档后,我了解到无法让此插件为模式中未引用的定义生成 POJO。

    作为一种解决方法,我们可以将它们定义为单独的 json-schema 定义,并让插件为其生成 pojo。

    因此,我必须为每个子类创建单独的架构定义(在这种情况下 - #/definitions/proprietaryProduct 需要定义为单独的架构定义)

    【讨论】:

      【解决方案3】:

      发布此答案(作为解决方法),因为它可能对面临同样挑战的人有所帮助。

      在研究并阅读了 jsonschema2pojo-maven-plugin:generate 文档后,我了解到无法让此插件为架构中未引用的定义生成 POJO。

      所以我认为示例中显示的 json 架构定义是错误的,因此插件无法生成 POJO。

      json 架构应遵循http://json-schema.org/draft-04/schema 中定义的规则

      这样的东西应该会更好

      {
      "$schema":"http://json-schema.org/draft-04/schema",
      "description":"Products definition",
      "type":"object,"
      "properties":{
      "product":{
        "type":"object",
        "properties":{
          "type":{
            "enum":[
              "THIRD_PARTY",
              "PROPRIETARY"
            ],
            "type":"string"
          },
          "price":{
            "type":"string"
          }
        },
        "additionalProperties": false
      },
      

      ...等等 }

      我试过了,效果很好

      【讨论】:

        猜你喜欢
        • 2019-12-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-05-31
        • 2016-01-13
        • 2016-11-25
        • 2014-04-19
        • 2016-10-02
        相关资源
        最近更新 更多