Skip to content
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 additional imports #1422

Merged
merged 2 commits into from
Jul 14, 2023

Conversation

skonik
Copy link
Contributor

@skonik skonik commented Jul 12, 2023

This PR allows to pass additional imports within generate function.

The problem I was facing was that I needed to add custom classes for models generated inside output file.
However, adding new classes to custom template didn't affect output file at all. The reason was how imports are added to rendered data model template. This PR solves such kind of issue.

This allows me to do such things:

  1. Specify some kind of python object inside template(BaseModel in my case)
Screenshot 2023-07-12 at 12 39 41
  1. Call generate function with custom base_class and aditional_imports with pydantic BaseModel
Screenshot 2023-07-12 at 12 24 32
  1. As a result generated code has required imports.
Screenshot 2023-07-12 at 12 24 04

Waiting for feedback regarding the feature and the code itself.

Closes #1421

@codecov
Copy link

codecov bot commented Jul 14, 2023

Codecov Report

Patch coverage: 100.00% and no project coverage change.

Comparison is base (e9b6edf) 100.00% compared to head (6225b8f) 100.00%.

Additional details and impacted files
@@             Coverage Diff             @@
##            master     #1422     +/-   ##
===========================================
  Coverage   100.00%   100.00%             
===========================================
  Files           11        32     +21     
  Lines         1020      3547   +2527     
  Branches       201       833    +632     
===========================================
+ Hits          1020      3547   +2527     
Flag Coverage Δ
unittests 99.63% <99.57%> (?)

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

Impacted Files Coverage Δ
datamodel_code_generator/__init__.py 100.00% <100.00%> (ø)
datamodel_code_generator/__main__.py 100.00% <100.00%> (ø)
datamodel_code_generator/format.py 100.00% <100.00%> (ø)
datamodel_code_generator/http.py 100.00% <100.00%> (ø)
datamodel_code_generator/imports.py 100.00% <100.00%> (ø)
datamodel_code_generator/model/__init__.py 100.00% <100.00%> (ø)
datamodel_code_generator/model/base.py 100.00% <100.00%> (ø)
datamodel_code_generator/model/dataclass.py 100.00% <100.00%> (ø)
datamodel_code_generator/model/enum.py 100.00% <100.00%> (ø)
datamodel_code_generator/model/improts.py 100.00% <100.00%> (ø)
... and 22 more

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

@koxudaxi koxudaxi merged commit ca7dabd into koxudaxi:master Jul 14, 2023
73 checks passed
@koxudaxi
Copy link
Owner

koxudaxi commented Aug 5, 2023

@skonik
Sorry for the delay in thanking you.
Thank you for your contribution!

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.

Ability to add custom imports
2 participants