问题
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