Jackson PropertyNamingStrategy in jersey generated API

1,052 views
Skip to first unread message

Andrew Craft

unread,
Oct 19, 2017, 10:47:41 AM10/19/17
to Swagger

I have a Jersey jax-rs application that i have configured to generate a swagger document for as described in https://github.com/swagger-api/swagger-core/wiki/swagger-core-jersey-2.x-project-setup

We are using jackson to convert our entity and configured it to use SNAKE_CASE property naming strategy. When running swagger its is picking the default naming strategy and putting those names into the swagger document using camel case. I have tried configuring io.swagger.util.Json.mapper() 

        Json.mapper().setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);


but that ObjectMapper appears to be used globally as not only does it fix the propertynames for my rest entity, the properties in the swagger document also get converted eg 'operationId' on my operations gets changed to operation_id, which is then breaking the swagger-codegen which is experting normal camel case.

Any way i can custom the names generated for properties on my entities? Obviously don't want to go through adding annotations on all my entities but configure swagger to default to snake case.

thanks
Andrew

Francesco Tumanischvili

unread,
Oct 19, 2017, 3:28:14 PM10/19/17
to Swagger
Hi Andrew,

You can use a custom converter, possibly the quickest option would be something like:

class MyFactory extends ObjectMapperFactory{
public ObjectMapper createMapper() {
ObjectMapper mapper = ObjectMapperFactory.createJson();
mapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
return mapper;
}
}
class MyConverter extends ModelResolver {
public MyConverter() {
super(new MyFactory().createMapper());
}
}

which applies the customized mapper to the definitions resolver.

Alternatively you can do it similarly in code implementing ModelConverter directly, an example with exactly your case is here: 


In both cases you need to add the custom converter during bootstrap, e.g.:

ModelConverters.getInstance().addConverter(new MyConverter());

you can also add your custom converter via service loader to META-INF/services/io.swagger.converter.ModelConverter

Also possibly you are referring to an old wiki link; Wiki for current 1.5.x version is avaliable at https://github.com/swagger-api/swagger-core/wiki/Swagger-Core-JAX-RS-Project-Setup-1.5.X

while the one for oncoming 2.x version, currently in RC is here:  https://github.com/swagger-api/swagger-core/wiki/Swagger-Core-JAX-RS-Project-Setup-2.0.X
Reply all
Reply to author
Forward
0 new messages