question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Duplicate model names don't throw but are overwritten

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:11
  • Comments:12 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
betterment-ashcommented, Aug 13, 2019

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! 🤞

3reactions
ccosentinocommented, Jan 10, 2019

+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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Duplicate Object and working with Duplicate without changing ...
In order to change one instance without changing the other you need to clone the actual values of this instance and not the...
Read more >
How to Clone Objects with Mutable field in Java? Example ...
When we override the clone() method inside the Programmer class, we need to explicitly take care of this List, otherwise, both original and...
Read more >
Things to Know About Duplicate Rules - Salesforce Help
Review considerations for using and customizing duplicate rules.
Read more >
Object references and copying - The Modern JavaScript Tutorial
When an object variable is copied, the reference is copied, but the ... If the copied property name already exists, it gets overwritten:....
Read more >
File.Copy Method (System.IO) - Microsoft Learn
Overwriting a file of the same name is not allowed. ... Message); } } // Delete source files that were copied. foreach (string...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found