Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open API Overwrites Models In Schema #1228

Closed
tomhillgreyridge opened this issue Apr 15, 2021 · 2 comments
Closed

Open API Overwrites Models In Schema #1228

tomhillgreyridge opened this issue Apr 15, 2021 · 2 comments

Comments

@tomhillgreyridge
Copy link
Contributor

Describe the feature
I've put this as a feature request rather than a bug as it appears to be documented behaviour in the underlying Swagger implementation. Basically, if you create two classes with the same name, but in a different package, you only end up with one underlying model in the schema which means your API documentation is now wrong!

In other words, if I have an API for Thing1 and an API for Thing2 and I create a package structure like

thing1
  dto
    CreationDTO
    UpdateDTO
thing2
  dto
    CreationDTO
    UpdateDTO

Then any API documentation for thing2 will refer to the DTOs for Thing1 which isn't good...

Additional context
This is down to the fact that the swagger model generator, by default, just uses the class name - not the fully qualified name. This can be quite a pain if you want to have models called UpdateRequest or UpdateDTO or similar and keep the names consistent. Obviously, you could rename to Thing1UpdateRequest and Thing2UpdateRequest but there is are some alternative options discussed here

ministryofjustice/prison-api#273
springfox/springfox#182

and this thread (for .NET) suggests a solution by overriding the schemaID generation function in Swagger / OpenAPI

https://stackoverflow.com/questions/46071513/swagger-error-conflicting-schemaids-duplicate-schemaids-detected-for-types-a-a

@tomhillgreyridge
Copy link
Contributor Author

I've had a look into this by the way. There is an existing OpenAPI annotation which can be used to get round this behaviour, which is called APIModel

https://docs.swagger.io/swagger-core/v1.5.0/apidocs/io/swagger/annotations/ApiModel.html

In there, you can provide a different value for the name which will be used in the OpenAPI schema but you have to manually check your codeback before you know whether you have any naming conflicts.

I'm currently looking into writing a custom TypeNameResolver. Implementing it in the OpenAPI plugin was easy (provide an option to set it in OpenAPIOptions, pass it to the JacksonModelConverterFactory and then on to the JacksonModelConverter) but I'm trying to work out how often it gets called so I can work out if there is a way of raising an error on duplicate model names

@anibyl
Copy link

anibyl commented Mar 21, 2022

This helped me:

TypeNameResolver.std.setUseFqn(true);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants