Skip to content

Render optional attributes with default values as mandatory#909

Merged
ogenstad merged 1 commit intostablefrom
pog-mandatory-default-attributes-IHS-216
Apr 8, 2026
Merged

Render optional attributes with default values as mandatory#909
ogenstad merged 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 so attributes that have default values are treated as required (not nullable) in generated schemas.
  • Tests

    • Added unit tests covering combinations of optional flags and default values to ensure correct rendered attribute types.

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Mar 31, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 8726bc6c-db3b-4670-9816-aa06011d4829

📥 Commits

Reviewing files that changed from the base of the PR and between 02eb81e and 76df042.

📒 Files selected for processing (3)
  • changelog/894.fixed.md
  • infrahub_sdk/protocols_generator/generator.py
  • tests/unit/sdk/test_protocols_generator.py
✅ Files skipped from review due to trivial changes (3)
  • changelog/894.fixed.md
  • infrahub_sdk/protocols_generator/generator.py
  • tests/unit/sdk/test_protocols_generator.py

Walkthrough

Updates the protocols generator to stop marking attributes with a default value as Optional. The Jinja2 rendering filter now appends "Optional" only when value.optional is true and value.default_value is None. Adds changelog/894.fixed.md describing the fix and a parametrized unit test (test_filter_render_attribute) covering combinations of optional and default_value to validate the corrected rendering.

🚥 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 summarizes the main change: rendering optional attributes with default values as mandatory in generated protocols.
Description check ✅ Passed The description covers key sections including Why, What changed, and Impact & rollout, though it lacks How to test and Suggested review order.
Linked Issues check ✅ Passed The PR successfully addresses issue #894 by implementing the Jinja2 template fix to render attributes as non-optional when they have default values.
Out of Scope Changes check ✅ Passed All changes are directly related to fixing the protocol generation logic for attributes with default values; no out-of-scope modifications detected.

✏️ 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.68%   +7.77%     
==========================================
  Files         119      119              
  Lines       10336    10336              
  Branches     1551     1551              
==========================================
+ Hits         7536     8340     +804     
+ Misses       2287     1473     -814     
- Partials      513      523      +10     
Flag Coverage Δ
integration-tests 41.72% <0.00%> (?)
python-3.10 51.82% <100.00%> (-0.02%) ⬇️
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

Comment thread changelog/894.fixed.md Outdated
@@ -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.

Comment thread changelog/894.fixed.md Outdated
@@ -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"

@ogenstad ogenstad force-pushed the pog-mandatory-default-attributes-IHS-216 branch from 02eb81e to 76df042 Compare April 8, 2026 11:53
@cloudflare-workers-and-pages
Copy link
Copy Markdown

cloudflare-workers-and-pages bot commented Apr 8, 2026

Deploying infrahub-sdk-python with  Cloudflare Pages  Cloudflare Pages

Latest commit: 76df042
Status: ✅  Deploy successful!
Preview URL: https://1fc86862.infrahub-sdk-python.pages.dev
Branch Preview URL: https://pog-mandatory-default-attrib.infrahub-sdk-python.pages.dev

View logs

@ogenstad ogenstad merged commit bc84821 into stable Apr 8, 2026
21 checks passed
@ogenstad ogenstad deleted the pog-mandatory-default-attributes-IHS-216 branch April 8, 2026 13:53
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