anupam anupam - 8 days ago 6
JSON Question

Generation of snakecase format for definitions in swagger.json in Dropwizard

I am using dropwizard framework for our project in which I am using @JsonSnakeCase annotation for converting the format from CamelCase to snake_case. But when I try to generate swagger.json by using swagger annotations, it generates the definitions in CamelCase. Is there any configuration in swagger which I need to set, to change the format of attributes from CamelCase to snake_case ?

My model class :

@Data
@JsonSnakeCase

public class ServiceRequest {
@NotNull
private Long categoryId;

@NotNull @NotEmpty
private String serviceName;

@NotNull @NotEmpty
private String serviceDesc;

private boolean isRootService;
private Long parentServiceId;

private String imageLink;
private String imageDescription;
private String videoLink;
private String serviceHelpText;


}

This is how the swagger.json is generated

"ServiceRequest": {
"type": "object",
"required": [
"categoryId",
"serviceDesc",
"serviceName"
],
"properties": {
"categoryId": {
"type": "integer",
"format": "int64"
},
"serviceName": {
"type": "string"
},
"serviceDesc": {
"type": "string"
},
"parentServiceId": {
"type": "integer",
"format": "int64"
},
"imageLink": {
"type": "string"
},
"imageDescription": {
"type": "string"
},
"videoLink": {
"type": "string"
},
"serviceHelpText": {
"type": "string"
},
"rootService": {
"type": "boolean",
"default": false
}
}
},

Answer

The default ObjectMapper with Swagger does not take this into consideration. You can specify your own mapper to be used by swagger for model conversions.

ObjectMapper converter = new ObjectMapper();
converter.setPropertyNamingStrategy(
    PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES);
ModelConverters.getInstance().addConverter(new ModelResolver(converter));

FYI ModelConverter is a singleton of Swagger core. So this might affect all the places where it is being used.

Comments