koxudaxi / datamodel-code-generator

Pydantic model and dataclasses.dataclass generator for easy conversion of JSON, OpenAPI, JSON Schema, and YAML data sources.
https://koxudaxi.github.io/datamodel-code-generator/
MIT License
2.44k stars 281 forks source link

Fix Missing Imports #2009

Closed luca-knaack-webcom closed 1 day ago

luca-knaack-webcom commented 2 weeks ago

The processed_models array stores Processed objects, which include models and their imports after being handled by the parser/base.py:parse function. However, since models are objects (passed by reference), the Processed objects and their models can be modified after creation. This can sometimes result in model changes without corresponding updates to the model imports, leading to broken Pydantic models (example is given in #2001).

This PR addresses this issue by introducing a post-processing step in the base parser's parsing method, ensuring that all necessary imports are correctly included.

Fixes #2001

codecov[bot] commented 1 week ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Project coverage is 100.00%. Comparing base (9ac0425) to head (9e7eafb).

Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #2009 +/- ## ========================================= Coverage 100.00% 100.00% ========================================= Files 38 38 Lines 4188 4197 +9 Branches 961 967 +6 ========================================= + Hits 4188 4197 +9 ``` | [Flag](https://app.codecov.io/gh/koxudaxi/datamodel-code-generator/pull/2009/flags?src=pr&el=flags&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Koudai+Aono) | Coverage Δ | | |---|---|---| | [unittests](https://app.codecov.io/gh/koxudaxi/datamodel-code-generator/pull/2009/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Koudai+Aono) | `99.66% <100.00%> (+<0.01%)` | :arrow_up: | Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Koudai+Aono#carryforward-flags-in-the-pull-request-comment) to find out more.

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

codspeed-hq[bot] commented 6 days ago

CodSpeed Performance Report

Merging #2009 will not alter performance

Comparing luca-knaack-webcom:feat/2001-missing-imports (9e7eafb) with main (9ac0425)

Summary

✅ 29 untouched benchmarks

koxudaxi commented 4 days ago

@luca-knaack-webcom Thank you for creating the PR again. Could you please fix the test coverage?

https://github.com/koxudaxi/datamodel-code-generator/pull/2009/files

luca-knaack-webcom commented 2 days ago

@koxudaxi Just performed a final code-cleanup. Everything is now covered by tests. While there is one pragma: no cover, this is still covered by those lines being covered 😅