Skip to content
Draft
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/894.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Generate protocols where optional attributes with a default value renders attributes that are required, i.e. not nullable.
2 changes: 1 addition & 1 deletion infrahub_sdk/protocols_generator/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def _jinja2_filter_syncify(value: str | list, sync: bool = False) -> str | list:
def _jinja2_filter_render_attribute(value: AttributeSchemaAPI) -> str:
attribute_kind: str = ATTRIBUTE_KIND_MAP[value.kind]

if value.optional:
if value.optional and value.default_value is None:
attribute_kind += "Optional"

return f"{value.name}: {attribute_kind}"
Expand Down
53 changes: 52 additions & 1 deletion tests/unit/sdk/test_protocols_generator.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from dataclasses import dataclass
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, Any

import pytest

from infrahub_sdk import InfrahubClient
from infrahub_sdk.protocols_generator.generator import CodeGenerator
from infrahub_sdk.schema import AttributeSchemaAPI

if TYPE_CHECKING:
from pytest_httpx import HTTPXMock
Expand Down Expand Up @@ -36,6 +37,56 @@ class SyncifyTestCase:
]


@dataclass
class RenderAttributeTestCase:
name: str
optional: bool
default_value: Any
expected: str


RENDER_ATTRIBUTE_TEST_CASES = [
RenderAttributeTestCase(
name="required-no-default",
optional=False,
default_value=None,
expected="enabled: Boolean",
),
RenderAttributeTestCase(
name="required-with-default",
optional=False,
default_value=True,
expected="enabled: Boolean",
),
RenderAttributeTestCase(
name="optional-no-default",
optional=True,
default_value=None,
expected="enabled: BooleanOptional",
),
RenderAttributeTestCase(
name="optional-with-default",
optional=True,
default_value=True,
expected="enabled: Boolean",
),
]


@pytest.mark.parametrize(
"test_case",
[pytest.param(tc, id=tc.name) for tc in RENDER_ATTRIBUTE_TEST_CASES],
)
async def test_filter_render_attribute(test_case: RenderAttributeTestCase) -> None:
attr = AttributeSchemaAPI(
name="enabled",
kind="Boolean",
optional=test_case.optional,
default_value=test_case.default_value,
)
assert CodeGenerator._jinja2_filter_render_attribute(attr) == test_case.expected


@pytest.mark.parametrize(
"test_case",
[pytest.param(tc, id=tc.name) for tc in SYNCIFY_TEST_CASES],
Expand Down