Skip to content

Commit 41fb350

Browse files
committed
tests/schema - nullable additionalProperties
add test for implicit/explicit nullable & oneOf
1 parent c4ae9be commit 41fb350

2 files changed

Lines changed: 49 additions & 35 deletions

File tree

tests/fixtures/schema-additionalProperties-nullable-allof.yaml

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,42 +5,60 @@ info:
55

66
components:
77
schemas:
8-
LinkOccasion:
8+
A:
99
type: object
10+
nullable: false
11+
additionalProperties: false
1012
properties:
11-
unitTestId:
12-
type: integer
13-
required:
14-
- unitTestId
15-
RelationsModel:
16-
type: object
17-
properties:
18-
unitPartNumber:
13+
a:
1914
type: string
20-
linkOccasions:
21-
type: array
22-
items:
23-
$ref: "#/components/schemas/LinkOccasion"
2415
required:
25-
- unitPartNumber
26-
- linkOccasions
16+
- a
2717

2818
paths:
29-
/relations:
19+
/implicit-nullable-object-allOf:
3020
get:
31-
operationId: getRelations
21+
operationId: implicit-allOf
3222
responses:
3323
"200":
3424
description: ok
3525
content:
3626
application/json:
3727
schema:
38-
title: base
3928
type: object
4029
additionalProperties:
41-
title: addi
42-
# type: object
43-
# additionalProperties: false
30+
# type: object - not set
4431
nullable: true
4532
allOf:
46-
- $ref: "#/components/schemas/RelationsModel"
33+
- $ref: "#/components/schemas/A"
34+
35+
/explicit-nullable-object-allOf:
36+
get:
37+
operationId: explicit-allOf
38+
responses:
39+
"200":
40+
description: ok
41+
content:
42+
application/json:
43+
schema:
44+
type: object
45+
additionalProperties:
46+
type: object
47+
nullable: true
48+
allOf:
49+
- $ref: "#/components/schemas/A"
50+
51+
/explicit-object-oneOf-nullable:
52+
get:
53+
operationId: explicit-oneOf
54+
responses:
55+
"200":
56+
description: ok
57+
content:
58+
application/json:
59+
schema:
60+
type: object
61+
additionalProperties:
62+
oneOf:
63+
- $ref: "#/components/schemas/A"
64+
- nullable: true

tests/schema_test.py

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -340,21 +340,17 @@ def test_schema_additionalProperties_nullable_allof(with_schema_additionalProper
340340
"""
341341
api = OpenAPI("/", with_schema_additionalProperties_nullable_allof)
342342

343-
schema = api._["getRelations"].return_value()
344-
value_type = schema.additionalProperties.get_type()
343+
for op in ["implicit-allOf", "explicit-allOf", "explicit-oneOf"]:
344+
schema = api._[op].return_value()
345+
value_type = schema.additionalProperties.get_type()
345346

346-
assert value_type.__pydantic_complete__ is True
347+
assert value_type.__pydantic_complete__ is True
347348

348-
# non-null value
349-
obj = value_type.model_validate({"unitPartNumber": "ABC", "linkOccasions": [{"unitTestId": 1}]})
350-
assert obj.root.unitPartNumber == "ABC"
351-
assert obj.root.linkOccasions[0].unitTestId == 1
352-
353-
# map model accepts null values for the map entries
354-
map_type = schema.get_type()
355-
result = map_type.model_validate({"key1": {"unitPartNumber": "ABC", "linkOccasions": []}, "key2": None})
356-
assert result.root["key2"] is None
357-
assert result.root["key1"].root.unitPartNumber == "ABC"
349+
# map model accepts null values for the map entries
350+
map_type = schema.get_type()
351+
result = map_type.model_validate({"obj": {"a": "ABC"}, "none": None})
352+
assert result.root["none"] is None
353+
assert result.root["obj"].root.a == "ABC"
358354

359355

360356
def test_schema_with_patternProperties(with_schema_patternProperties):

0 commit comments

Comments
 (0)