Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/908.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Skip mandatory field validation during object loading when `object_profile` is specified.
4 changes: 2 additions & 2 deletions infrahub_sdk/pytest_plugin/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"PythonTransform": "get_python_transform",
}

ITEMS_MAPPING = {
ITEMS_MAPPING: dict[str, type[InfrahubItem]] = {
"check-smoke": InfrahubCheckSmokeItem,
"check-unit-process": InfrahubCheckUnitProcessItem,
"check-integration": InfrahubCheckIntegrationItem,
Expand Down Expand Up @@ -71,7 +71,7 @@ def collect_group(self, group: InfrahubTestGroup) -> Iterable[pytest.Item]:
resource_config = self.get_resource_config(group)

for test in group.tests:
item_class: type[pytest.Item] = ITEMS_MAPPING[test.spec.kind] # type: ignore[assignment]
item_class = ITEMS_MAPPING[test.spec.kind]
item: InfrahubItem = item_class.from_parent(
name=f"{marker.markname}__{group.resource_name}__{test.name}",
parent=self,
Expand Down
6 changes: 3 additions & 3 deletions infrahub_sdk/spec/object.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,9 +267,9 @@ async def validate_object(
context = context.copy() if context else {}

# First validate if all mandatory fields are present
# Skip mandatory check when an object_template is specified, as the template provides defaults, we expect the API server to just send a
# response with a failure if the template is not valid or doesn't provide all mandatory fields
if "object_template" not in data:
# Skip mandatory check when an object_template or object_profile is specified, as the template/profile provides defaults;
# we expect the API server to return a failure if the template/profile is not valid or doesn't provide all mandatory fields
if "object_template" not in data and "object_profile" not in data:
errors.extend(
ObjectValidationError(position=[*position, element], message=f"{element} is mandatory")
for element in schema.mandatory_input_names
Expand Down
15 changes: 15 additions & 0 deletions tests/unit/sdk/spec/test_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -472,3 +472,18 @@ async def test_validate_object_skips_mandatory_check_with_object_template(

mandatory_errors = [e for e in errors if e.message == "type is mandatory"]
assert mandatory_errors == []


async def test_validate_object_skips_mandatory_check_with_object_profile(
client_with_schema_01: InfrahubClient,
) -> None:
"""When object_profile is present, mandatory field validation should be skipped."""
schema = await client_with_schema_01.schema.get(kind="BuiltinLocation")

data = {"name": "Site1", "object_profile": "Standard Site"}
errors = await InfrahubObjectFileData.validate_object(
client=client_with_schema_01, position=[1], schema=schema, data=data
)

mandatory_errors = [e for e in errors if e.message == "type is mandatory"]
assert mandatory_errors == []