datamodel-code-generator icon indicating copy to clipboard operation
datamodel-code-generator copied to clipboard

Fix none related issues

Open AntoineD opened this issue 3 years ago • 1 comments
trafficstars

For a schema with a property that can be null and has a null default value, the pydantic model is not correctly generated. For instance:

{
  "properties": {
    "name": {
      "type": ["null", "string"],
      "default": "null"
    },
  },
  "type": "object"
}

is converted to

class Model(BaseModel):
    name: Optional[Optional[str]] = 'null'

instead of

class Model(BaseModel):
    name: Optional[str] = None

This PR addresses the default value conversion and the duplicated Optional.

AntoineD avatar Sep 02 '22 13:09 AntoineD

Codecov Report

Base: 100.00% // Head: 99.92% // Decreases project coverage by -0.07% :warning:

Coverage data is based on head (2bcc5b1) compared to base (e9b6edf). Patch coverage: 99.90% of modified lines in pull request are covered.

Additional details and impacted files
@@             Coverage Diff             @@
##            master     #840      +/-   ##
===========================================
- Coverage   100.00%   99.92%   -0.08%     
===========================================
  Files           11       21      +10     
  Lines         1020     2603    +1583     
  Branches       201      616     +415     
===========================================
+ Hits          1020     2601    +1581     
- Misses           0        1       +1     
- Partials         0        1       +1     
Flag Coverage Δ
unittests 99.84% <99.80%> (?)

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

Impacted Files Coverage Δ
datamodel_code_generator/model/__init__.py 100.00% <ø> (ø)
datamodel_code_generator/types.py 99.01% <98.78%> (ø)
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/base.py 100.00% <100.00%> (ø)
datamodel_code_generator/model/enum.py 100.00% <100.00%> (ø)
...atamodel_code_generator/model/pydantic/__init__.py 100.00% <100.00%> (ø)
... and 18 more

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

:umbrella: View full report at Codecov.
:loudspeaker: Do you have feedback about the report comment? Let us know in this issue.

codecov[bot] avatar Sep 02 '22 13:09 codecov[bot]

Hi @koxudaxi, it looks like you've rebased this PR, the failing CI jobs are due to codecov. Do you need me to do something?

AntoineD avatar Oct 05 '22 16:10 AntoineD

@AntoineD Thank you for creating the PR. I re-run the failed CI.

Btw, How do we set "null" string value as default? Also, Can we use "default": null for default?

koxudaxi avatar Oct 06 '22 15:10 koxudaxi

@koxudaxi

Btw, How do we set "null" string value as default?

I do not know.

Also, Can we use "default": null for default?

I would say yes in a json file.

AntoineD avatar Oct 13 '22 08:10 AntoineD

@AntoineD

Btw, How do we set "null" string value as default? I do not know.

We should not break current behavior. This is not a bug.

koxudaxi avatar Dec 22 '22 15:12 koxudaxi

no update

koxudaxi avatar Feb 16 '23 10:02 koxudaxi