springfox: Duplicate class names in different packages get squashed in ControllerDocumentation - modelMap
I haven’t yet made a fix for this yet but our team encountered this issue and I believe it’s fairly easy to fix but wanted to get feedback from you on how to go about this.
Example of the issue given the following controller method:
public void sampleMethod3(com.mangofactory.swagger.spring.sample.duplicate.Pet petOne, com.mangofactory.swagger.spring.sample.Pet petTwo) {
}
Notice that petOne and petTwo are different classes but have the same class name. When the ControllerDocumnetation modelMap is being populated it uses a friendly class name as the key so it would be “Pet” in this instance. With “Pet” being the same key in the model map they will overwrite each other.
Ways I was thinking to go about fixing this:
- Just add in the package name…so friendly names will now be fully qualified (Not so friendly names)
- Add configuration into SwaggerConfiguration that would trigger friendly/fully qualified names
I am really not sure which one to do…friendly names are nice but people might end up not realizing this is happening so it’s almost safer to have fully qualified.
Not sure if you need a reason of why this is important but here is it:
- Service versioning - having multiple version of client model object could cause these collision to happen over time
- Services that return subsets of a canonical model - some data might be expensive or unnecessary to return in this case service A could return petOne (superset) and service B could return petTwo (subset of petOne).
Let me know what you think and if I have time I don’t mind working on this fix as it will benefit our team.
About this issue
- Original URL
- State: closed
- Created 11 years ago
- Comments: 55 (20 by maintainers)
Commits related to this issue
- #182 Fix sort ordering. — committed to springfox/springfox by MaksimOrlov 5 years ago
- #182 Fix checkstyle. — committed to springfox/springfox by MaksimOrlov 5 years ago
- #182 Fix resource listing. — committed to springfox/springfox by MaksimOrlov 5 years ago
- #182 Fix Webflux tests. — committed to springfox/springfox by MaksimOrlov 5 years ago
- PAT-320: Renamed v1 Alert class to AlertV1 to prevent springfox issue wth generating swagger docs. See issue https://github.com/springfox/springfox/issues/182 - where the package is ignored for classe... — committed to ministryofjustice/prison-api by timharrison-moj 5 years ago
- PAT-320: Renamed v1 Alert class to AlertV1 to prevent springfox issue wth generating swagger docs. See issue https://github.com/springfox/springfox/issues/182 - where the package is ignored for classe... — committed to ministryofjustice/prison-api by timharrison-moj 5 years ago
- Merge pull request #3132 from springfox/bugfix/sort-ordering Fix method sort order (#182 ) — committed to springfox/springfox by dilipkrish 4 years ago
- pulling latest master (#1) * Create a FormParameter model object instead of BodyParameter for fields that are part of a multipart form (in=formData). * Adapted behavior of ParameterMapper and adde... — committed to wn-doolittle/springfox by deleted user 4 years ago
@Chinna-SHS The easiest for now is to use the
@ApiModel
attribute and set different values on the two.e.g.
@MaksimOrlov Just tried 3.0.0 and the issue is still not fixed.
Maybe because in my case the difference is not a package name, but an outer class name:
And:
Only one
Location
model is present in the generated docs. So, one ofUser
orCar
has incorrect model forlocation
property.I upgraded to 2.9.2 and am still seeing this issue
I have run into this as well. Any updates on progress around this is a fix still targeted for 2.7.0? Is there anything we can do to help out with a fix?
Also, my classes are generated by JAXB and thus cannot be modified with the ApiModel property specified in the workaround. Was this supposed to be fixed in 2.9.0?
@vignesharunachalam I had the same issue lately. Until a clear solution arises, you can always go around the problem by annotating at least one of your model with
@ApiModel
and use a unique name (e.g. LegacyPet for instance)Thanks for getting back to me so quickly with this. So I completely agree with you on the two points you have outlined. Based on what you are saying and it completely makes sense is that I would rename my objects that are similar to give them a more distinct name.
For the example you outlined above you could have com.xxxxxx.canonical.Pet and com.xxxxxx.summary.PetSummary or com.xxxxxx.canonical.PetSummary. That way the domain objects are distinct and there is no confusion about what Pet domain object is what.
The above annotation becomes valuable when you supporting multiple versions of a given service (happens all the time because consumers might not want to move to the latest…for whatever reason). Imagine this package structure:
I know ideally your canonical model shouldn’t change but in practice this happens I see these things happen:
Service versioning is always an interesting topic. Again I really appreciate the help.