jackson-module-jsonSchema
jackson-module-jsonSchema copied to clipboard
SchemaGenerator generates additional properties wrongly.
I have a class like below:
public class MyClass{
@JsonProperty("property_a")
private A a;
@JsonProperty("property_b")
@JsonPropertyDescription("my favourite property")
private B b;
@JsonProperty("property_c")
private C c;
/**
* propagate some internals of A
*/
public List<String> getSomethingAboutA() {
if (this.a != null) {
return this.a.getSomething();
}
return null;
}
}
This will go on to generate the following json schema
{
"type": "object",
"id": "urn:jsonschema:pathToMyClass.MyClass",
"properties": {
"property_a": {
"type": "object",
},
"property_b": {
"type": "object",
"description": "my favourite property",
},
"property_c": {
"type": "object",
},
"somethingAboutA": {
"type": "array",
"items": {
"type": "string"
}
}
}
For brevity I have excluded the details of A, B and C in both java classes and json schema generated.
Bug:
somethingAboutA is not actually a property and should not be generated.
Is there any workaround for this ? Or maybe I am missing some other configuration ?
My observation is that this is only happening when a method starts with get,
so may be, it is being interpreted as an attribute.
I am using the latest version of this library.
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-jsonSchema</artifactId>
<version>2.13.0-rc2</version>
</dependency>
This is the schema generation code:
ObjectMapper jacksonObjectMapper = new ObjectMapper();
JsonSchemaGenerator schemaGen = new JsonSchemaGenerator(jacksonObjectMapper);
String schemaString = "test";
try {
JsonSchema schema = schemaGen.generateSchema(MyClass.class);
schemaString = jacksonObjectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(schema);
} catch (JsonMappingException ex) {
System.out.println(ex.toString());
} catch (JsonProcessingException ex) {
System.out.println(ex.toString());
}
System.out.println(schemaString);
Found one workaround for this.
Both JsonIgnore on the method getSomethingAboutA and JsonIgnoreProperties on the class like @JsonIgnoreProperties({"somethingAboutA"}) work.
But, I still feel these are workarounds, and somethingAboutA should not have been picked in the first place.
This seems to work as expected: getSomethingAboutA() is a valid getter and Jackson does consider there to be a property. This is by design; by default public getters/setters with Bean-compatible name and signature are auto-detected.
Similarly public fields are detected.
You can, however, change configurations so that only explicitly annotated accessors are considered.
For classes that is via @JsonAutoDetect, although there is ObjectMapper configuration setting for changing baseline as well.
What you want basically is to set detection level for getters (and probably setters, fields too) to Visibility.NONE, in which case only explicitly annotated properties are detected.
@cowtowncoder This works. Thanks 👍
Glad it works @rohnigam!
For me jsonschemagenerator is not creating any instance. Giving me some exceptions. I am using Jackson 2.14.2 version
Can anyone help?
It Says CLASSNOTFOUND Exception
@rishabhvarshney2 Do not tag on unrelated things into existing issues. Instead file a new issue with enough details to explain the issue you see -- preferably using a recent version.