Swagger-core: Duplicate model names don't throw but are overwritten

Created on 16 Nov 2017  路  12Comments  路  Source: swagger-api/swagger-core

I'm using version 1.5.10 and experiencing the following issues: I have two classes with the same simple name but in different namespaces. When I generate my swagger file, one of them "wins" and overwrites the first one.

I've found a workaround to use a custom model name via ApiModel. Alternatevly, I could make use of one of the other name resolvers like Jackson's annotation.

But still, I'd like to detect such a name clash automatically and not by accident when the specs don't match the API. What do you think about adding a check here? I can provide a PR if wanted, just wanted to talk about it before.

Alternatively, I'd have to execute the same class scanning as Swagger does in order to find such a clash.

Top Issue!

Most helpful comment

Not sure this is helpful but I figured I'd add what worked for me.

Our use case is a Spring Boot application and we're adding Swagger UI. Our controllers often times have inner classes for requests and responses, but are often generically named Request and Response, which causes conflicts due to how the DefaultTypeNameProvideruses type.getSimpleName():

https://github.com/springfox/springfox/blob/master/springfox-schema/src/main/java/springfox/documentation/schema/DefaultTypeNameProvider.java#L35

Our solution was to just create our own TypeNameProviderPlugin, which is a component on one of our @Configurations

    @Component
    @Order(SwaggerPluginSupport.SWAGGER_PLUGIN_ORDER)
    public static class CustomTypeNameProvider implements TypeNameProviderPlugin {
        @Override
        public String nameFor(Class<?> type) {
            String fullName = type.getName();
            return fullName.substring(fullName.lastIndexOf(".") + 1);
        }

        @Override
        public boolean supports(DocumentationType delimiter) {
            return true;
        }
    }

Found out about plugins via documentation here: https://springfox.github.io/springfox/docs/snapshot/#plugins

Hope this helps someone! 馃

All 12 comments

I am struggling with the same issue.
The ApiModel workaround doesn't work for me because I am using the swagger-maven-plugin with UseJAXBAnnotationProcessor option:

https://github.com/kongchen/swagger-maven-plugin/commit/dbe98e507c756c806431d8c80df06832617f27bd

(objectMapper.registerModule(new JaxbAnnotationModule());)

Any suggestions?

I ran into the same issue, where I have several JAX-RS resources where the models are defined as inner classes and sometimes they conflict. It's going to be a surprise each time because we don't track such conflicts any other way so they just show up as misinformation in Swagger.

+1

+100
This is a huge issue for us. We have hundreds of APIs and add more every month. The fact that swagger silently overwrites models if they have duplicate names has caused a lot of issues; most importantly of which is that we cannot trust the swagger documentation. It would be nice if the scanner would at least log when it finds a duplicate api model.

Keep in mind this is only manifests itself if you have two models with the same value but have different structures. In our case, we had many models with duplicate values, but the structures were the same. However, for many models with the same value, the models were similar but not identical, which caused further confusion since things looks mostly correct.

For those struggling with this, our solution was to write our own annotation scanner and search for apimodel annotations with a duplicate value, then going into the code and manually making them unique. We are working this process into our pre-promote workflow. But it would be better if the tool didn't allow this to happen in the first place.

+1

+1

+100
this issue make the compiled binary file untrustworthy, so in fact ,this is a huge issue...

Not sure this is helpful but I figured I'd add what worked for me.

Our use case is a Spring Boot application and we're adding Swagger UI. Our controllers often times have inner classes for requests and responses, but are often generically named Request and Response, which causes conflicts due to how the DefaultTypeNameProvideruses type.getSimpleName():

https://github.com/springfox/springfox/blob/master/springfox-schema/src/main/java/springfox/documentation/schema/DefaultTypeNameProvider.java#L35

Our solution was to just create our own TypeNameProviderPlugin, which is a component on one of our @Configurations

    @Component
    @Order(SwaggerPluginSupport.SWAGGER_PLUGIN_ORDER)
    public static class CustomTypeNameProvider implements TypeNameProviderPlugin {
        @Override
        public String nameFor(Class<?> type) {
            String fullName = type.getName();
            return fullName.substring(fullName.lastIndexOf(".") + 1);
        }

        @Override
        public boolean supports(DocumentationType delimiter) {
            return true;
        }
    }

Found out about plugins via documentation here: https://springfox.github.io/springfox/docs/snapshot/#plugins

Hope this helps someone! 馃

@betterment-ash Nice workaround! Sadly, it's just usable if you use springfox and not "just" swagger.

Any ETA on when this will be resolved? Do we have any workaround in swagger.core?

Just like any issue, PRs are welcome :)

Custom naming can be obtain providing a custom TypeNameResolver, an example can be found here naming models with fully qualified name, but whatever strategy could be applied, e.g. applying code similar to the springfox example above. Closing ticket please reopen if still experiencing issues

Was this page helpful?
0 / 5 - 0 ratings