Skip to content

Commit bc84821

Browse files
authored
Merge pull request #909 from opsmill/pog-mandatory-default-attributes-IHS-216
Render optional attributes with default values as mandatory
2 parents 7757d11 + 76df042 commit bc84821

3 files changed

Lines changed: 54 additions & 2 deletions

File tree

changelog/894.fixed.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Generate protocols so that optional attributes with a default value are rendered as required (not nullable).

infrahub_sdk/protocols_generator/generator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ def _jinja2_filter_syncify(value: str | list, sync: bool = False) -> str | list:
117117
def _jinja2_filter_render_attribute(value: AttributeSchemaAPI) -> str:
118118
attribute_kind: str = ATTRIBUTE_KIND_MAP[value.kind]
119119

120-
if value.optional:
120+
if value.optional and value.default_value is None:
121121
attribute_kind += "Optional"
122122

123123
return f"{value.name}: {attribute_kind}"

tests/unit/sdk/test_protocols_generator.py

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
from dataclasses import dataclass
2-
from typing import TYPE_CHECKING
2+
from typing import TYPE_CHECKING, Any
33

44
import pytest
55

66
from infrahub_sdk import InfrahubClient
77
from infrahub_sdk.protocols_generator.generator import CodeGenerator
8+
from infrahub_sdk.schema import AttributeSchemaAPI
89

910
if TYPE_CHECKING:
1011
from pytest_httpx import HTTPXMock
@@ -36,6 +37,56 @@ class SyncifyTestCase:
3637
]
3738

3839

40+
@dataclass
41+
class RenderAttributeTestCase:
42+
name: str
43+
optional: bool
44+
default_value: Any
45+
expected: str
46+
47+
48+
RENDER_ATTRIBUTE_TEST_CASES = [
49+
RenderAttributeTestCase(
50+
name="required-no-default",
51+
optional=False,
52+
default_value=None,
53+
expected="enabled: Boolean",
54+
),
55+
RenderAttributeTestCase(
56+
name="required-with-default",
57+
optional=False,
58+
default_value=True,
59+
expected="enabled: Boolean",
60+
),
61+
RenderAttributeTestCase(
62+
name="optional-no-default",
63+
optional=True,
64+
default_value=None,
65+
expected="enabled: BooleanOptional",
66+
),
67+
RenderAttributeTestCase(
68+
name="optional-with-default",
69+
optional=True,
70+
default_value=True,
71+
expected="enabled: Boolean",
72+
),
73+
]
74+
75+
76+
@pytest.mark.parametrize(
77+
"test_case",
78+
[pytest.param(tc, id=tc.name) for tc in RENDER_ATTRIBUTE_TEST_CASES],
79+
)
80+
async def test_filter_render_attribute(test_case: RenderAttributeTestCase) -> None:
81+
attr = AttributeSchemaAPI(
82+
name="enabled",
83+
kind="Boolean",
84+
optional=test_case.optional,
85+
default_value=test_case.default_value,
86+
)
87+
assert CodeGenerator._jinja2_filter_render_attribute(attr) == test_case.expected
88+
89+
3990
@pytest.mark.parametrize(
4091
"test_case",
4192
[pytest.param(tc, id=tc.name) for tc in SYNCIFY_TEST_CASES],

0 commit comments

Comments
 (0)