Add description to json schema subtypes

▼魔方 西西 提交于 2020-01-15 10:38:30

问题


I am generating json schema from my java classes with jackson, and I add the description field to all my properties like this :

@NotNull
@JsonProperty (required = true)
@JsonPropertyDescription ("My description to add in schema")
private double value;

This work fine, and the field description is set into the json schema.

Now I have json subtypes described as below :

@JsonTypeInfo (use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXTERNAL_PROPERTY, property = "type")
@JsonSubTypes ({
    @JsonSubTypes.Type (value = TriangularWaveformConfigurationEntity.class, name = "TRIANGULAR"),
    @JsonSubTypes.Type (value = SquareWaveformConfigurationEntity.class, name = "SQUARE")
})
public abstract class WaveformConfigurationEntity implements ConfigurationEntity

And the result :

"waveformConfiguration" : 
{
"oneOf" : [ {
            "$ref" : "#/definitions/TriangularWaveformConfigurationEntity"
          }, {
            "$ref" : "#/definitions/SquareWaveformConfigurationEntity"
          }
          "description" : "Configuration of the waveform used to generate values"
        }

With the ref :

"TriangularWaveformConfigurationEntity" : {
      "type" : "object",
      "additionalProperties" : false,
      "properties" : {
        "type" : {
          "type" : "string",
          "enum" : [ "TRIANGULAR" ],
          "default" : "TRIANGULAR"
        },

My problem is that the enum value has no description, and I need this field like this :

"TriangularWaveformConfigurationEntity" : {
      "type" : "object",
      "additionalProperties" : false,
      "properties" : {
        "type" : {
          "type" : "string",
          "enum" : [ "TRIANGULAR" ],
          "default" : "TRIANGULAR",
          "description" : "my description"
        },

Note the schema are generated with the code :

ObjectMapper MAPPER = ConfigurationObjectMapper.createObjectMapper();
JsonSchemaGenerator jsonSchemaGenerator = new JsonSchemaGenerator(MAPPER);
JsonNode jsonSchema = jsonSchemaGenerator.generateJsonSchema(InputGeneratorConfiguration.class);

System.out.println(MAPPER.writeValueAsString(jsonSchema));

And the mapper

ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setVisibility(objectMapper.getSerializationConfig()
                                       .getDefaultVisibilityChecker()
                                       .withCreatorVisibility(JsonAutoDetect.Visibility.NONE)
                                       .withGetterVisibility(JsonAutoDetect.Visibility.NONE)
                                       .withIsGetterVisibility(JsonAutoDetect.Visibility.NONE)
                                       .withSetterVisibility(JsonAutoDetect.Visibility.NONE)
                                       .withFieldVisibility(JsonAutoDetect.Visibility.ANY));

objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
return objectMapper;

来源:https://stackoverflow.com/questions/58594445/add-description-to-json-schema-subtypes

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