Skip to content

Commit

Permalink
- Move nullable logic to openapi parser
Browse files Browse the repository at this point in the history
- Add unittest
  • Loading branch information
koxudaxi committed Nov 4, 2023
1 parent a4dd6f3 commit 92040e9
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 11 deletions.
6 changes: 0 additions & 6 deletions datamodel_code_generator/parser/jsonschema.py
Original file line number Diff line number Diff line change
Expand Up @@ -590,12 +590,6 @@ def _get_data_type(type_: str, format__: str) -> DataType:
**obj.dict() if not self.field_constraints else {},
)

if obj.nullable and self.strict_nullable:
if isinstance(obj.type, list) and 'null' not in obj.type:
obj.type.append('null')
elif isinstance(obj.type, str) and obj.type != 'null':
obj.type = [obj.type, 'null']

if isinstance(obj.type, list):
return self.data_type(
data_types=[
Expand Down
14 changes: 14 additions & 0 deletions datamodel_code_generator/parser/openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,20 @@ def get_ref_model(self, ref: str) -> Dict[str, Any]:
ref_body = self.raw_obj
return get_model_by_path(ref_body, ref_path.split('/')[1:])

def get_data_type(self, obj: JsonSchemaObject) -> DataType:
if obj.nullable and self.strict_nullable and isinstance(obj.type, str):
obj.type = [obj.type, 'null']

return super().get_data_type(obj)

def parse_one_of(
self, name: str, obj: JsonSchemaObject, path: List[str]
) -> List[DataType]:
data_types = super().parse_one_of(name, obj, path)
if obj.nullable and self.strict_nullable:
data_types.append(DataType(type='None'))
return data_types

def resolve_object(
self, obj: Union[ReferenceObject, BaseModelT], object_type: Type[BaseModelT]
) -> BaseModelT:
Expand Down
7 changes: 6 additions & 1 deletion tests/data/expected/main/main_openapi_nullable/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from __future__ import annotations

from typing import List, Optional
from typing import List, Optional, Union

from pydantic import AnyUrl, BaseModel, Field

Expand Down Expand Up @@ -76,3 +76,8 @@ class Tag(BaseModel):

class Notes(BaseModel):
comments: List[str] = Field(default_factory=list)


class Options(BaseModel):
comments: List[str]
oneOfComments: List[Union[str, float]]
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from __future__ import annotations

from typing import List, Optional
from typing import List, Optional, Union

from pydantic import AnyUrl, BaseModel, Field

Expand Down Expand Up @@ -76,3 +76,8 @@ class Tag(BaseModel):

class Notes(BaseModel):
comments: List[str] = Field(default_factory=list)


class Options(BaseModel):
comments: List[Optional[str]]
oneOfComments: List[Optional[Union[str, float]]]
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,8 @@ class Tag(BaseModel):

class Notes(BaseModel):
comments: List[str] = Field(default_factory=list)


class Options(BaseModel):
comments: List[str | None]
oneOfComments: List[str | float | None]
7 changes: 6 additions & 1 deletion tests/data/expected/main/main_typed_dict_nullable/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from __future__ import annotations

from typing import List, NotRequired, Optional, TypedDict
from typing import List, NotRequired, Optional, TypedDict, Union


class Cursors(TypedDict):
Expand Down Expand Up @@ -60,3 +60,8 @@ class EmailItem(TypedDict):

class Notes(TypedDict):
comments: NotRequired[List[str]]


class Options(TypedDict):
comments: List[str]
oneOfComments: List[Union[str, float]]
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from __future__ import annotations

from typing import List, NotRequired, Optional, TypedDict
from typing import List, NotRequired, Optional, TypedDict, Union


class Cursors(TypedDict):
Expand Down Expand Up @@ -60,3 +60,8 @@ class EmailItem(TypedDict):

class Notes(TypedDict):
comments: NotRequired[List[str]]


class Options(TypedDict):
comments: List[Optional[str]]
oneOfComments: List[Optional[Union[str, float]]]
7 changes: 6 additions & 1 deletion tests/data/expected/main/main_use_default_kwarg/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from __future__ import annotations

from typing import List, Optional
from typing import List, Optional, Union

from pydantic import AnyUrl, BaseModel, Field

Expand Down Expand Up @@ -76,3 +76,8 @@ class Tag(BaseModel):

class Notes(BaseModel):
comments: List[str] = Field(default_factory=list)


class Options(BaseModel):
comments: List[str]
oneOfComments: List[Union[str, float]]
18 changes: 18 additions & 0 deletions tests/data/openapi/nullable.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,21 @@ components:
type: string
default_factory: list
nullable: false
options:
type: object
properties:
comments:
type: array
items:
type: string
nullable: true
oneOfComments:
type: array
items:
oneOf:
- type: string
- type: number
nullable: true
required:
- comments
- oneOfComments

0 comments on commit 92040e9

Please sign in to comment.