Skip to content

Render optional attributes with default values as mandatory#909

Open
ogenstad wants to merge 1 commit intostablefrom
pog-mandatory-default-attributes-IHS-216
Open

Render optional attributes with default values as mandatory#909
ogenstad wants to merge 1 commit intostablefrom
pog-mandatory-default-attributes-IHS-216

Conversation

@ogenstad
Copy link
Copy Markdown
Contributor

@ogenstad ogenstad commented Mar 31, 2026

Why

Currently when you create a schema in Infrahub with a node that has a mandatory attribute and that attribute has a default value, then Infrahub will update the schema definition and mark the attribute as optional. The intention is to change this behaviour, until that happens we want to update the protocol definitions that are generated out of such a schema so that the attributes appear to be required (a separate PR will be opened against the Infrahub repo to disallow changing such attributes to a null value.

Closes #894.

What changed

  • Update the Jinja2 template to only mark an attribute as optional in the generated protocols if it both is optional and doesn't have a default value.

Impact & rollout

At some later date we'll want to revert this in order to properly support schemas where the user wants these attributes to actually be optional (and nullable) even though they have a default_value set.

Summary by CodeRabbit

  • Bug Fixes
    • Fixed protocol generation to correctly render optional attributes with default values as optional instead of required.

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Mar 31, 2026

Walkthrough

This pull request fixes a bug in the protocols generator where attributes with default values were incorrectly rendered as Optional in schema protocols. The Jinja2 attribute rendering filter has been updated to only append "Optional" when an attribute is marked optional and has no default value. A changelog entry documenting this fix and parametrized unit tests validating the corrected behavior across different optional and default_value combinations have been added.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main change: updating protocol generation to render optional attributes with default values as mandatory (required) types.
Description check ✅ Passed The description provides clear context for the problem, explains the solution, and references the issue being closed. All critical sections are present and complete.
Linked Issues check ✅ Passed The pull request addresses the requirements in issue #894: updating protocol generation to mark attributes with default_value as non-optional (mandatory) types, fixing the bug where such fields were incorrectly rendered as Optional.
Out of Scope Changes check ✅ Passed All changes are focused and in-scope: the template logic update, comprehensive unit tests covering the fix, and a changelog entry documenting the change.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@codecov
Copy link
Copy Markdown

codecov bot commented Mar 31, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

@@            Coverage Diff             @@
##           stable     #909      +/-   ##
==========================================
+ Coverage   72.91%   80.71%   +7.80%     
==========================================
  Files         119      119              
  Lines       10336    10336              
  Branches     1551     1551              
==========================================
+ Hits         7536     8343     +807     
+ Misses       2287     1469     -818     
- Partials      513      524      +11     
Flag Coverage Δ
integration-tests 41.75% <0.00%> (?)
python-3.10 51.84% <100.00%> (ø)
python-3.11 51.84% <100.00%> (+0.01%) ⬆️
python-3.12 51.84% <100.00%> (+0.01%) ⬆️
python-3.13 51.82% <100.00%> (-0.02%) ⬇️
python-3.14 53.54% <100.00%> (ø)
python-filler-3.12 24.04% <0.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
infrahub_sdk/protocols_generator/generator.py 95.00% <100.00%> (ø)

... and 32 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@ogenstad ogenstad marked this pull request as ready for review April 2, 2026 14:33
@ogenstad ogenstad requested a review from a team as a code owner April 2, 2026 14:33
Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
tests/unit/sdk/test_protocols_generator.py (1)

48-73: Consider adding falsy default-value cases to harden the matrix.

The current cases cover None and True; adding False, 0, and "" would explicitly guard the is None sentinel behavior.

Suggested additions
 RENDER_ATTRIBUTE_TEST_CASES = [
@@
     RenderAttributeTestCase(
         name="optional-with-default",
         optional=True,
         default_value=True,
         expected="enabled: Boolean",
     ),
+    RenderAttributeTestCase(
+        name="optional-with-false-default",
+        optional=True,
+        default_value=False,
+        expected="enabled: Boolean",
+    ),
+    RenderAttributeTestCase(
+        name="optional-with-zero-default",
+        optional=True,
+        default_value=0,
+        expected="enabled: Boolean",
+    ),
+    RenderAttributeTestCase(
+        name="optional-with-empty-string-default",
+        optional=True,
+        default_value="",
+        expected="enabled: Boolean",
+    ),
 ]
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@tests/unit/sdk/test_protocols_generator.py` around lines 48 - 73, The test
matrix RENDER_ATTRIBUTE_TEST_CASES in tests/unit/sdk/test_protocols_generator.py
lacks falsy default_value cases; add additional RenderAttributeTestCase entries
that use default_value=False, default_value=0, and default_value="" (with
appropriate name fields like "optional-false-default", "optional-zero-default",
"optional-emptystr-default") to ensure the rendering logic in the renderer (look
at RenderAttributeTestCase usage and the optional vs default handling) treats
non-None falsy values as explicit defaults and produces the expected types
accordingly; update expected strings to match the behavior verified for those
falsy defaults.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@changelog/894.fixed.md`:
- Line 1: Rewrite the single-line changelog to be clearer and grammatically
correct by stating the behavior precisely; replace the current sentence with
something like: "Generate protocols where optional attributes with a default
value are rendered as required (non-nullable) attributes," ensuring it mentions
"optional attributes with a default value" and that they become "required
(non-nullable)" in generated typing; update the line in changelog/894.fixed.md
accordingly.

---

Nitpick comments:
In `@tests/unit/sdk/test_protocols_generator.py`:
- Around line 48-73: The test matrix RENDER_ATTRIBUTE_TEST_CASES in
tests/unit/sdk/test_protocols_generator.py lacks falsy default_value cases; add
additional RenderAttributeTestCase entries that use default_value=False,
default_value=0, and default_value="" (with appropriate name fields like
"optional-false-default", "optional-zero-default", "optional-emptystr-default")
to ensure the rendering logic in the renderer (look at RenderAttributeTestCase
usage and the optional vs default handling) treats non-None falsy values as
explicit defaults and produces the expected types accordingly; update expected
strings to match the behavior verified for those falsy defaults.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 7a7f378c-ccd0-4dd4-9cd0-dad65f7b9fea

📥 Commits

Reviewing files that changed from the base of the PR and between 17af0a3 and 02eb81e.

📒 Files selected for processing (3)
  • changelog/894.fixed.md
  • infrahub_sdk/protocols_generator/generator.py
  • tests/unit/sdk/test_protocols_generator.py

@@ -0,0 +1 @@
Generate protocols where optional attributes with a default value renders attributes that are required, i.e. not nullable.
Copy link
Copy Markdown

@coderabbitai coderabbitai bot Apr 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Tighten changelog wording for clarity and grammar.

Line 1 is grammatically awkward and a bit ambiguous. Consider a clearer statement focused on generated typing behavior.

Suggested wording
-Generate protocols where optional attributes with a default value renders attributes that are required, i.e. not nullable.
+Generate protocols so attributes with a default value are rendered as required (not nullable).
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
Generate protocols where optional attributes with a default value renders attributes that are required, i.e. not nullable.
Generate protocols so attributes with a default value are rendered as required (not nullable).
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@changelog/894.fixed.md` at line 1, Rewrite the single-line changelog to be
clearer and grammatically correct by stating the behavior precisely; replace the
current sentence with something like: "Generate protocols where optional
attributes with a default value are rendered as required (non-nullable)
attributes," ensuring it mentions "optional attributes with a default value" and
that they become "required (non-nullable)" in generated typing; update the line
in changelog/894.fixed.md accordingly.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this suggestion is easier to understand, but also leaves out the word "optional"

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ajtmccarty, that's a fair point — "optional" is meaningful here since the fix specifically targets attributes marked as optional in the schema that also have a default value. Here's a revised suggestion that preserves it:

-Generate protocols where optional attributes with a default value renders attributes that are required, i.e. not nullable.
+Generate protocols so that optional attributes with a default value are rendered as required (not nullable).

This keeps "optional attributes with a default value" intact while fixing the grammatical awkwardness of the original.

@@ -0,0 +1 @@
Generate protocols where optional attributes with a default value renders attributes that are required, i.e. not nullable.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this suggestion is easier to understand, but also leaves out the word "optional"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: Mandatory fields with default_value set incorrectly have Optional type in schema protocols

2 participants