Skip to content

use key not title for class name #652

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
erdnax123 opened this issue Aug 11, 2022 · 4 comments
Open

use key not title for class name #652

erdnax123 opened this issue Aug 11, 2022 · 4 comments
Labels
🐞bug Something isn't working

Comments

@erdnax123
Copy link

in components -> schemas
Screenshot from 2022-08-10 22-53-44
here i have erros:

Unable to parse schema /components/schemas/HRVSample

Unable to parse this part of your OpenAPI document: : Attempted to generate duplicate models with name "Sample"

why used the title for class names and not the key. The key is always unique, but the title is not necessarily unique.

@erdnax123 erdnax123 added the 🐞bug Something isn't working label Aug 11, 2022
@dbanty
Copy link
Collaborator

dbanty commented Aug 11, 2022

Classes also are generated for schemas declared inline in endpoints, so the key within components doesn’t necessarily guarantee uniqueness.

We could probably make the source of class names configurable though. I wonder how the openapitools generator selects class names.

@erdnax123
Copy link
Author

openapi uses the keys from components->samples, inline models I don't know, because I don't have in my current project

in an other project (vue3 frontend) I use https://github.com/RicoSuter/NSwag this generates inline models with the same name like Option (first occurrence of model Option), Option1 (second occurrence of model Option), Option2, ...

@davidandrewrice
Copy link

Hi there @dbanty - this bug recently caused a problem for my team and I, so we've implemented a fix using the method @erdnax123 described (counting recurrences of duplicate model names, and using the numbering to disambiguate). Wanted to check if you'd be open to a contribution with the fix?

@dbanty
Copy link
Collaborator

dbanty commented Mar 8, 2025

The main problem I have with the enumeration solution is that two completely equivalent OpenAPI docs could generate different code. Something like re-ordering an endpoint or extracting a model into a reference could cause two models to swap places, or even change the numbering of several models all at once.

This makes the generated code quite fragile, especially for consumers not using a type-checker. If it ends up being the only way to generate code, then I suppose we'll just need to slap a big warning on the option which indicates what can happen.

Probably a better solution is to fall back through a few naming strategies until we find the most-user-friendly-while-unambiguous one. For example, falling back to the ref model key from the title in the case where there's a duplicate. This would cause one-time breakage when the first duplicate is encountered, but hopefully not an ongoing risk to consumers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐞bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants