Skip to content

Commit f639855

Browse files
Fix nullable propagation from $ref schema targets (#2649)
* Fix nullable reference propagation in JSON Schema and OpenAPI handling * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Fix formatting inconsistencies in test cases for JSON Schema handling * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Refactor get_mock_response function to use underscore for unused kwargs * Refactor get_mock_response function to use underscore for unused kwargs --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 27929ad commit f639855

7 files changed

Lines changed: 283 additions & 140 deletions

File tree

src/datamodel_code_generator/parser/jsonschema.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1038,7 +1038,11 @@ def _get_data_type(type_: str, format__: str) -> DataType:
10381038
def get_ref_data_type(self, ref: str) -> DataType:
10391039
"""Get a data type from a reference string."""
10401040
reference = self.model_resolver.add_ref(ref)
1041-
return self.data_type(reference=reference)
1041+
ref_schema = self._load_ref_schema_object(ref)
1042+
is_optional = (
1043+
ref_schema.type_has_null or ref_schema.type == "null" or (self.strict_nullable and ref_schema.nullable)
1044+
)
1045+
return self.data_type(reference=reference, is_optional=is_optional)
10421046

10431047
def set_additional_properties(self, path: str, obj: JsonSchemaObject) -> None:
10441048
"""Set additional properties flag in extra template data."""
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# generated by datamodel-codegen:
2+
# filename: ref_type_has_null.json
3+
# timestamp: 2019-07-26T00:00:00+00:00
4+
5+
from __future__ import annotations
6+
7+
from pydantic import BaseModel
8+
9+
10+
class NullableString(BaseModel):
11+
__root__: str | None
12+
13+
14+
class NonNullableString(BaseModel):
15+
__root__: str
16+
17+
18+
class NullOnly(BaseModel):
19+
__root__: None
20+
21+
22+
class Model(BaseModel):
23+
nullableRef: NullableString | None
24+
nonNullableRef: NonNullableString
25+
nullOnlyRef: NullOnly | None
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# generated by datamodel-codegen:
2+
# filename: ref_nullable.yaml
3+
# timestamp: 2019-07-26T00:00:00+00:00
4+
5+
from __future__ import annotations
6+
7+
from pydantic import BaseModel
8+
9+
10+
class NullableChild(BaseModel):
11+
name: str | None = None
12+
13+
14+
class NonNullableChild(BaseModel):
15+
name: str | None = None
16+
17+
18+
class Parent(BaseModel):
19+
nullableChild: NullableChild | None
20+
nonNullableChild: NonNullableChild
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-07/schema#",
3+
"definitions": {
4+
"NullableString": {
5+
"type": ["string", "null"]
6+
},
7+
"NonNullableString": {
8+
"type": "string"
9+
},
10+
"NullOnly": {
11+
"type": "null"
12+
}
13+
},
14+
"type": "object",
15+
"required": ["nullableRef", "nonNullableRef", "nullOnlyRef"],
16+
"properties": {
17+
"nullableRef": {
18+
"$ref": "#/definitions/NullableString"
19+
},
20+
"nonNullableRef": {
21+
"$ref": "#/definitions/NonNullableString"
22+
},
23+
"nullOnlyRef": {
24+
"$ref": "#/definitions/NullOnly"
25+
}
26+
}
27+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
openapi: 3.0.0
2+
info:
3+
title: Test API
4+
version: 1.0.0
5+
paths: {}
6+
components:
7+
schemas:
8+
NullableChild:
9+
nullable: true
10+
type: object
11+
properties:
12+
name:
13+
type: string
14+
NonNullableChild:
15+
type: object
16+
properties:
17+
name:
18+
type: string
19+
Parent:
20+
type: object
21+
required:
22+
- nullableChild
23+
- nonNullableChild
24+
properties:
25+
nullableChild:
26+
$ref: "#/components/schemas/NullableChild"
27+
nonNullableChild:
28+
$ref: "#/components/schemas/NonNullableChild"

0 commit comments

Comments
 (0)