swagger-core
swagger-core copied to clipboard
Make `ObjectMapper` used for finding properties configurable
The problem
Swagger creates its own ObjectMapper instance for serializing schemas. In the process of resolving a model, this object mapper is used to find the properties of the target class.
https://github.com/swagger-api/swagger-core/blob/dc8785efc71c243eebf1ad57dd612cc75112d351/modules/swagger-core/src/main/java/io/swagger/v3/core/jackson/ModelResolver.java#L584
Applications always create their own ObjectMapper to serialize their API response objects. This object mapper might have different settings than the one from Swagger. An example is different visibility settings for a PropertyAccessor.
Consider the following example:
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setVisibility(PropertyAccessor.GETTER, JsonAutoDetect.Visibility.NONE);
objectMapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
System.out.println(objectMapper.writeValueAsString(response));
public class Response {
private String value;
// getter and setter for value
public String getSomething() {
return "There should be no property 'something' in the generated schema";
}
}
Restricting serialzation of classes to their fields and ignoring getters is a common thing. When the application serializes the object, only value will appear in its result.
This is different to the generated schema from Swagger, which will also include a property something. This is due to Swagger using its own ObjectMapper and calling findProperties (see above).
"Response": {
"properties": {
"value": {
"type": "string"
},
"something": {
"type": "string"
}
}
}
Expectation
Swagger should allow to specify an ObjectMapper that is used for finding properties of a bean class. Calling findProperties with this ObjectMapper returns only visible properties.