Skip to content

Commit

Permalink
Improve strict-nullable (#330)
Browse files Browse the repository at this point in the history
  • Loading branch information
koxudaxi authored Feb 9, 2021
1 parent a016a08 commit 1c3a842
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 19 deletions.
30 changes: 14 additions & 16 deletions datamodel_code_generator/parser/jsonschema.py
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,9 @@ def parse_object_fields(
required=required,
alias=alias,
constraints=constraints,
nullable=field.nullable if self.strict_nullable else None,
nullable=field.nullable
if self.strict_nullable and (field.has_default or required)
else None,
)
)
return fields
Expand Down Expand Up @@ -664,15 +666,16 @@ def parse_array_fields(
item_obj_data_types.append(self.get_data_type(item))
if self.force_optional_for_required_fields:
required: bool = False
nullable: Optional[bool] = None
else:
required = not (
obj.has_default and self.apply_default_values_for_required_fields
)
if self.strict_nullable:
required = not (
obj.has_default and self.apply_default_values_for_required_fields
)
nullable = obj.nullable if obj.has_default or required else True
else:
required = not obj.nullable and not (
obj.has_default and self.apply_default_values_for_required_fields
)
required = not obj.nullable and required
nullable = None
return self.data_model_field_type(
data_type=self.data_type(data_types=item_obj_data_types, is_list=True,),
example=obj.example,
Expand All @@ -682,7 +685,7 @@ def parse_array_fields(
title=obj.title,
required=required,
constraints=obj.dict(),
nullable=obj.nullable if self.strict_nullable else None,
nullable=nullable,
)

def parse_array(
Expand Down Expand Up @@ -721,14 +724,9 @@ def parse_root_type(
if self.force_optional_for_required_fields:
required: bool = False
else:
if self.strict_nullable:
required = not (
obj.has_default and self.apply_default_values_for_required_fields
)
else:
required = not obj.nullable and not (
obj.has_default and self.apply_default_values_for_required_fields
)
required = not obj.nullable and not (
obj.has_default and self.apply_default_values_for_required_fields
)
reference = self.model_resolver.add(path, name, loaded=True)
self.set_title(name, obj)
self.set_additional_properties(name, additional_properties or obj)
Expand Down
14 changes: 14 additions & 0 deletions tests/data/expected/main/main_openapi_nullable/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class Cursors(BaseModel):
prev: str
next: Optional[str] = 'last'
index: float
tag: Optional[str] = None


class TopLevel(BaseModel):
Expand Down Expand Up @@ -50,6 +51,7 @@ class EmailItem(BaseModel):
author: str
address: str = Field(..., description='email address')
description: Optional[str] = 'empty'
tag: Optional[str] = None


class Email(BaseModel):
Expand All @@ -58,3 +60,15 @@ class Email(BaseModel):

class Id(BaseModel):
__root__: int


class Description(BaseModel):
__root__: Optional[str] = 'example'


class Name(BaseModel):
__root__: Optional[str] = None


class Tag(BaseModel):
__root__: str
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class Cursors(BaseModel):
prev: Optional[str] = Field(...)
next: str = 'last'
index: float
tag: Optional[str] = None


class TopLevel(BaseModel):
Expand All @@ -28,8 +29,10 @@ class User(BaseModel):


class Api(BaseModel):
apiKey: str = Field(None, description='To be used as a dataset parameter value')
apiVersionNumber: str = Field(
apiKey: Optional[str] = Field(
None, description='To be used as a dataset parameter value'
)
apiVersionNumber: Optional[str] = Field(
None, description='To be used as a version parameter value'
)
apiUrl: Optional[AnyUrl] = Field(
Expand All @@ -48,6 +51,7 @@ class EmailItem(BaseModel):
author: str
address: str = Field(..., description='email address')
description: str = 'empty'
tag: Optional[str] = None


class Email(BaseModel):
Expand All @@ -56,3 +60,15 @@ class Email(BaseModel):

class Id(BaseModel):
__root__: int


class Description(BaseModel):
__root__: Optional[str] = 'example'


class Name(BaseModel):
__root__: Optional[str] = None


class Tag(BaseModel):
__root__: str
15 changes: 14 additions & 1 deletion tests/data/openapi/nullable.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ components:
default: last
index:
type: number
tag:
type: string
required:
- prev
- index
Expand Down Expand Up @@ -74,9 +76,20 @@ components:
description:
type: string
default: empty
tag:
type: string
required:
- author
- address
id:
type: integer
default: 1
default: 1
description:
type: string
nullable: true
default: example
name:
type: string
nullable: true
tag:
type: string

0 comments on commit 1c3a842

Please sign in to comment.