-
-
Notifications
You must be signed in to change notification settings - Fork 396
feat: add new option --parent-scoped-naming to avoid name collisions #2373
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
feat: add new option --parent-scoped-naming to avoid name collisions #2373
Conversation
CodSpeed Performance ReportMerging #2373 will not alter performanceComparing Summary
|
dc1ae09
to
b81bf20
Compare
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #2373 +/- ##
=======================================
Coverage 98.17% 98.18%
=======================================
Files 65 65
Lines 7945 7968 +23
Branches 800 804 +4
=======================================
+ Hits 7800 7823 +23
Misses 110 110
Partials 35 35
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
@koxudaxi This is a very useful flag to have. Could this be merged? 🙌 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you resolve the merge conflicts please?
I think I am having the same issue as @gjcarneiro with a model having many Spec or Status fields defined in an openapi spec, which is super common in kubernetes-based resources Example: spec, swagger ui But unfortunately, using the cmd = [
"datamodel-codegen",
"--url",
spec["url"],
"--output-model-type",
"pydantic_v2.BaseModel",
"--use-annotated",
"--parent-scoped-naming",
"--enum-field-as-literal",
"all",
"--output",
str(output_dir),
] I wonder if I hold it wrong? I thought this would result on class names to be prepended with the ancestor info, but not lead to the file structure changes. Here is what I meant by saying that the file tree gets exploded. When I generate the model using this new flag I get this: as opposed to a much flatter structure before |
I'm sorry if this option does not work correctly for your case. I did not test this with any api specs that contain dots. In my use cases, I always only have a single flat python module, without any dirs. You can check around this area what it is trying to do. It simply concatenates the path of parent of the current model to the current model's path. Perhaps it needs to be slightly tweaked when there is a |
When you have object or enum definitions defined inline in operations, rather than referenced, you get the name defined globally in python. If there are multiple versions with the same name, the name collision get solved by appending a number.
In some OpenAPI spec I am trying to use, which I didn't define, I am getting something like 12 "Status" enums, because of all the status fields.
This MR adds an option --parent-scoped-naming so that the name of the generated python class is prefixed by the name of the parent class from which it originated.
It kind of solves an issue similar to what is see in #2364