Skip to content

Commit c4ae9be

Browse files
committed
schema - nullable additionalProperties
nullable additionalProperties have to be RootModels
1 parent 50fcf27 commit c4ae9be

3 files changed

Lines changed: 18 additions & 6 deletions

File tree

src/aiopenapi3/model.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -618,9 +618,10 @@ def createAnnotation(
618618

619619
@staticmethod
620620
def types(schema: "SchemaType") -> typing.Generator[str, None, None]:
621+
nullable = getattr(schema, "nullable", False)
621622
if isinstance(schema.type, str):
622623
yield schema.type
623-
if getattr(schema, "nullable", False):
624+
if nullable:
624625
yield "null"
625626
else:
626627
typesfilter: set[str] = set()
@@ -631,6 +632,9 @@ def types(schema: "SchemaType") -> typing.Generator[str, None, None]:
631632
values = set(SCHEMA_TYPES)
632633
typesfilter = set()
633634

635+
if nullable:
636+
typesfilter.add("null")
637+
634638
if (const := getattr(schema, "const", None)) is not None:
635639
typesfilter.add(cast(str, TYPES_SCHEMA_MAP.get(type(const))))
636640

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ components:
1010
properties:
1111
unitTestId:
1212
type: integer
13-
13+
required:
14+
- unitTestId
1415
RelationsModel:
1516
type: object
1617
properties:
@@ -20,6 +21,9 @@ components:
2021
type: array
2122
items:
2223
$ref: "#/components/schemas/LinkOccasion"
24+
required:
25+
- unitPartNumber
26+
- linkOccasions
2327

2428
paths:
2529
/relations:
@@ -31,8 +35,12 @@ paths:
3135
content:
3236
application/json:
3337
schema:
38+
title: base
3439
type: object
3540
additionalProperties:
41+
title: addi
42+
# type: object
43+
# additionalProperties: false
3644
nullable: true
3745
allOf:
3846
- $ref: "#/components/schemas/RelationsModel"

tests/schema_test.py

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

343-
schema = api.paths["/relations"].get.responses["200"].content["application/json"].schema_
343+
schema = api._["getRelations"].return_value()
344344
value_type = schema.additionalProperties.get_type()
345345

346346
assert value_type.__pydantic_complete__ is True
347347

348348
# non-null value
349349
obj = value_type.model_validate({"unitPartNumber": "ABC", "linkOccasions": [{"unitTestId": 1}]})
350-
assert obj.unitPartNumber == "ABC"
351-
assert obj.linkOccasions[0].unitTestId == 1
350+
assert obj.root.unitPartNumber == "ABC"
351+
assert obj.root.linkOccasions[0].unitTestId == 1
352352

353353
# map model accepts null values for the map entries
354354
map_type = schema.get_type()
355355
result = map_type.model_validate({"key1": {"unitPartNumber": "ABC", "linkOccasions": []}, "key2": None})
356356
assert result.root["key2"] is None
357-
assert result.root["key1"].unitPartNumber == "ABC"
357+
assert result.root["key1"].root.unitPartNumber == "ABC"
358358

359359

360360
def test_schema_with_patternProperties(with_schema_patternProperties):

0 commit comments

Comments
 (0)