mbknor / mbknor-jackson-jsonSchema

Generate JSON Schema with Polymorphism using Jackson annotations
MIT License
232 stars 75 forks source link

JaxbAnnotationModule and PropertyNamingStrategy conflicts #167

Open theseeker58 opened 2 years ago

theseeker58 commented 2 years ago

I don't know it it's a feature or a bug, but after registering JaxbAnnotationModule to the ObjectMapper mapper.registerModule(new JaxbAnnotationModule()); setting PropertynamingStrategy different from LowerCamelCase mapper.setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE); is definitely ignored. Only default LOWER_CAMEL_CASE works. If I remove JaxbAnnotationModule I can successfully set different PropertyNamingStrategy. Unfortunately I need JaxbAnnotationModule to ignore field annotated with XmlTransient in schema generation, but at the same time I have to comply to the ISO Whitepaper for 20022 UNIFI messages that forces snake_case naming convention. Cheers

theseeker58 commented 2 years ago

Found the solution! after adding JaxbAnnotationModule, if you still want to set propertyNamingStrategy you have to enable the MapperFeature ALLOW_EXPLICIT_PROPERTY_RENAMING. Unfortunately I had to refactor my code since enable(MapperFeature) is deprecated on ObjectMapper. I used a subclass named JsonMapper. Here is the code.

        JsonMapper mapper = JsonMapper.builder()
                                .addModule(new JaxbAnnotationModule())
                                .enable(MapperFeature.ALLOW_EXPLICIT_PROPERTY_RENAMING)
                                .propertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE)
                                .build();

Just to share with other guys who come across this problem. Cheers