How can I define the sequence of properties in JSON Schema?

我的未来我决定 提交于 2019-12-10 19:12:01

问题


I have following java Object:

ProcessBean.java

import java.util.List;
import com.fasterxml.jackson.annotation.JsonRootName;

@JsonRootName(value = "process")
public class ProcessBean{
    private Integer id;
    private String name;
    private String description;
    private String user;
    private String executePermissions;
    private String createdDtm;
    private String updatedDtm;
    private Process tpProcess;
    private List<ProcessParamBean> processParameters;

    /* --------Getters and Setters ----------*/
}

I need to find the JSON schema for this object which is used to display these fields on UI. The order of fields in UI is determined by the order of properties in JSON schema generated.

I have generated the schema using following code:

DataSpec.java

import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.module.jsonSchema.JsonSchema;
import com.fasterxml.jackson.module.jsonSchema.factories.SchemaFactoryWrapper;

public class DataSpec {
    public static <T> String getDataSpec(Class<T> clazz) {
        ObjectMapper mapper = new ObjectMapper();
        mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
        SchemaFactoryWrapper visitor = new SchemaFactoryWrapper();
        MetauiVisitorContext obj = new MetauiVisitorContext();
        visitor.setVisitorContext(obj);
        try {
            mapper.acceptJsonFormatVisitor(clazz, visitor);
            JsonSchema schema = visitor.finalSchema();
            return mapper.writeValueAsString(schema);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            return null;
        }
    }
}

MetauiVisitorContext.java

import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.module.jsonSchema.factories.VisitorContext;

public class MetauiVisitorContext extends VisitorContext{
    @Override
    public String getSeenSchemaUri(JavaType aSeenSchema) {
        return null;
    }
}

The resultant schema looks like below:

{
  "type": "object",
  "id": "urn:jsonschema:com:restservices:api:jsonbeans:ProcessBean",
  "properties": {
    "updatedDtm": {
      "type": "string"
    },
    "createdDtm": {
      "type": "string"
    },
    "name": {
      "type": "string"
    },
    "tpProcess": {
      "type": "object",
      "id": "urn:jsonschema:com:restservices:api:jsonbeans:Process",
      "properties": {
        "name": {
          "type": "string"
        },
        "id": {
          "type": "integer"
        }
      }
    },
    "description": {
      "type": "string"
    },
    "id": {
      "type": "integer"
    },
    "processParameters": {
      "type": "array",
      "items": {
        "type": "object",
        "id": "urn:jsonschema:com:restservices:api:jsonbeans:ProcessParamBean",
        "properties": {
          "updatedDtm": {
            "type": "string"
          },
          "defaultValue": {
            "type": "string"
          },
          "createdDtm": {
            "type": "string"
          },
          "masterParam": {
            "type": "object",
            "id": "urn:jsonschema:com:restservices:api:jsonbeans:MasterParamBean",
            "properties": {
              "updatedDtm": {
                "type": "string"
              },
              "createdDtm": {
                "type": "string"
              },
              "name": {
                "type": "string"
              },
              "description": {
                "type": "string"
              },
              "id": {
                "type": "integer"
              }
            }
          },
          "name": {
            "type": "string"
          },
          "description": {
            "type": "string"
          },
          "processParamId": {
            "type": "integer"
          },
          "id": {
             "type": "integer"
          },
          "userPrompted": {
             "type": "boolean"
          },
          "tags": {
            "type": "string"
          }
        }
      }
    },
    "executePermissions": {
      "type": "string"
    },
    "user": {
      "type": "string"
    }
  }
}

As it can be seen that the order of properties in JSON schema does not match the order of fields defined in Java object which is necessary in my case.

So how can I determine the sequence of the properties?


回答1:


There is not an intrinsic order of properties in a JSON schema object, as it happens with a regular JSON or javascript object. Indeed, it does not change the validation semantics. The following schemas validate exactly the same collection of objects:

Schema 1:

{
    "properties" : {
        "prop1" : {
            "type" : "string"
        },
        "prop2" : {
            "type" : "number"
        }
    }
}

Schema 2:

{
    "properties" : {
        "prop2" : {
            "type" : "number"
        },
        "prop1" : {
            "type" : "string"
        }
    }
}

If you want to preserve some ordering you need to do it within and array. You could achieve this by using an array of objects instead of an object in the items clause. An example:

{
    "type" : "array" :
    "items" : [{
            "properties" : {
                "prop2" : {
                    "type" : "number"
                }
            }
        }, {
            "properties" : {
                "prop1" : {
                    "type" : "string"
                }
            }
        }
    ]
}

This way you get an ordered definition of UI items. Unfortunately, it forces you to nest each single property in an object.



来源:https://stackoverflow.com/questions/31691247/how-can-i-define-the-sequence-of-properties-in-json-schema

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!