Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

gjcarneiro
Copy link
Contributor

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

Copy link

codspeed-hq bot commented Apr 5, 2025

CodSpeed Performance Report

Merging #2373 will not alter performance

Comparing gjcarneiro:feature/parent-scoped-naming (46a7da6) with main (b0d9109)

Summary

✅ 31 untouched benchmarks

@gjcarneiro gjcarneiro force-pushed the feature/parent-scoped-naming branch from dc1ae09 to b81bf20 Compare April 5, 2025 15:44
Copy link

codecov bot commented Apr 5, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.18%. Comparing base (117a31d) to head (0654506).

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           
Flag Coverage Δ
unittests 98.18% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@guywaldman
Copy link

@koxudaxi This is a very useful flag to have. Could this be merged? 🙌

Copy link
Collaborator

@gaborbernat gaborbernat left a 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?

@gaborbernat gaborbernat merged commit e771ada into koxudaxi:main Apr 17, 2025
30 checks passed
@hellt
Copy link

hellt commented May 14, 2025

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 --parent-scoped-naming in my case led to a very nested file structure of the generated model with some models even failing to build hitting the file name length limits:

            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:

CleanShot 2025-05-14 at 09 58 13@2x

as opposed to a much flatter structure before

@gjcarneiro gjcarneiro deleted the feature/parent-scoped-naming branch May 14, 2025 08:46
@gjcarneiro
Copy link
Contributor Author

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 "." (dot) in the path.

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

Successfully merging this pull request may close these issues.

4 participants