Skip to content

Commit be1b6d3

Browse files
test: Add unit test for allOf required override
Add test_allof_required_override to verify that properties inherited from a base schema via allOf can be correctly marked as required in the derived schema's required list.
1 parent c6e8448 commit be1b6d3

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

tests/test_parser/test_properties/test_model_property.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,44 @@ def test_duplicate_properties(self, model_property_factory, string_property_fact
543543

544544
assert result.optional_props == [prop], "There should only be one copy of duplicate properties"
545545

546+
def test_allof_required_override(self, model_property_factory, string_property_factory, config):
547+
"""Test that required field can be overridden in allOf schemas"""
548+
# Simulates:
549+
# FooBase:
550+
# type: object
551+
# properties:
552+
# bar: {type: string}
553+
# baz: {type: string}
554+
# FooCreate:
555+
# allOf:
556+
# - $ref: '#/components/schemas/FooBase'
557+
# - type: object
558+
# required: [bar]
559+
bar_prop = string_property_factory(name="bar", required=False)
560+
baz_prop = string_property_factory(name="baz", required=False)
561+
562+
data = oai.Schema.model_construct(
563+
allOf=[
564+
oai.Reference.model_construct(ref="#/FooBase"),
565+
oai.Schema.model_construct(type="object", required=["bar"]),
566+
]
567+
)
568+
schemas = Schemas(
569+
classes_by_reference={
570+
"/FooBase": model_property_factory(
571+
required_properties=[], optional_properties=[bar_prop, baz_prop]
572+
),
573+
}
574+
)
575+
576+
result = _process_properties(data=data, schemas=schemas, class_name="FooCreate", config=config, roots={"root"})
577+
578+
# bar should now be required, baz should remain optional
579+
assert len(result.required_props) == 1
580+
assert result.required_props[0].name == "bar"
581+
assert len(result.optional_props) == 1
582+
assert result.optional_props[0].name == "baz"
583+
546584
@pytest.mark.parametrize("first_required", [True, False])
547585
@pytest.mark.parametrize("second_required", [True, False])
548586
def test_mixed_requirements(

0 commit comments

Comments
 (0)