Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I have a bunch of entity that returned in controllers in many forms , list , map , multimap , etc...

I have a solution where you define type mappings (entity -> dto, dto -> entity) where the entity is an actual JPA entity, and a DTO is representing the JSON form of the entity with a bunch of swagger annotation and stuff.

I create 2 jackson module from these mappings which is basically a lot of StdDelegatingDeserializer and StdDelegatingSerializer . This modules then added to jackson , and they do their job just fine.

When a controller returns an entity type, the actual JSON is created from the DTO, and the same when you want to receive an entity in the controller.

In this application also have a swagger with swagger UI set up, and in the swagger configuration, I add all this mapping as alternative type mapping. Most of the models in the swagger docs are good, but in some cases the docs show the structure of the entity instead of the DTO .

I store the mapping in a list of the following class

@Data
@NoArgsConstructor
@AllArgsConstructor
public class ModelMapperKey<E, D> {
    @NonNull
    private Class<E> entityType;
    @NonNull
    private Class<D> dtoType;

From this list, I create an AlternateTypeRuleConvention bean and swagger handles it automatically

    @Bean
    public AlternateTypeRuleConvention alternateTypeRuleConvention() {
        return new AlternateTypeRuleConvention() {
            @Override
            public int getOrder() {
                return Ordered.HIGHEST_PRECEDENCE;
            @Override
            public List<AlternateTypeRule> rules() {
                return modelMapperService.getMapperKeys()
                        .stream()
                        .map(key -> newRule(resolver.resolve(key.getEntityType()), resolver.resolve(key.getDtoType())))
                        .collect(Collectors.toList());

As I mentioned it's work just fine and show the actual DTO in the swagger docs, expect some case don't. Currently, 11 times out of 60 the entity being shown instead of the DTO model. I checked many times, the alternative type rules are created for those DTO-s and added to context. When you execute the request in swagger UI I get the correct json response.

I tried to play with rule precedence

newRule(resolver.resolve(key.getEntityType()), resolver.resolve(key.getDtoType()), Ordered.HIGHEST_PRECEDENCE)

Also tried 0 and Ordered.LOWEST_PRECEDENCE but non of them work. I have no idea what can cause this phenomenon.

I would like to see your generic approach on github. For what I understood you have a filter that mapps entity to entityDTO and vice-versa on each controller and somehow that filter is not being applied. Is that correct? – Ricardo Dec 3, 2019 at 15:22 No, the "filter" which is the deserialization and serialization context of the jackson works fine. The actual problem is the swagger, in some case ignore the configured alternative type mapping – Syngularity Dec 4, 2019 at 6:27

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.