Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 15, 2025

When creating nodes with object templates, the SDK was including explicit null values for uninitialized optional relationships. The backend interprets these as user overrides, preventing template defaults from being applied.

Root cause: _generate_input_data() unconditionally added None for all optional cardinality-one relationships, regardless of whether the node was new or being updated.

Changes:

  • Core logic (infrahub_sdk/node/node.py): Omit optional relationships for new nodes; include None only for existing nodes to preserve ability to clear values
  • Tests (tests/unit/sdk/test_node.py): Updated expectations for new nodes, added coverage for existing node behavior

Before:

mutation {
  DcimDeviceUpsert(data: {
    name: {value: "CoreRouter1"}
    object_template: {id: "..."}
    platform: null          # ❌ Blocks template default
    device_type: null       # ❌ Blocks template default
    primary_address: null   # ❌ Blocks template default
  })
}

After:

mutation {
  DcimDeviceCreate(data: {
    name: {value: "CoreRouter1"}
    object_template: {id: "..."}
    # ✅ Optional fields omitted, template defaults apply
  })
}

This preserves the existing behavior for updates (PR #515) where None allows explicitly clearing relationship values.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • https://api.github.com/repos/opsmill/infrahub-sdk-python/pulls/515
    • Triggering command: curl -s REDACTED (http block)
  • mock
    • Triggering command: /home/REDACTED/.cache/pypoetry/virtualenvs/infrahub-sdk-aoa6suxx-py3.12/bin/python /home/REDACTED/.cache/pypoetry/virtualenvs/infrahub-sdk-aoa6suxx-py3.12/bin/pytest tests/unit/ -v (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

This section details on the original issue you should resolve

<issue_title>bug: Creating an object with an object template with optional attributes fails to set the optional attributes</issue_title>
<issue_description>### Component

Python SDK

Infrahub SDK version

1.15.1

Current Behavior

Currently if you have a optional relationship of cardinality one and try to use the object templates the SDK will provide an override value to the backend this causes the backend not to apply the value provided by object template.

The UI provides a mutation like so:

mutation {
  DcimDeviceCreate(
    data: {object_template: {id: "18779f71-9564-3911-3bc2-1065c8cd8363"}, name: {value: "123123"}, location: {id: "18779f71-e8aa-55c1-3bc0-1065ebd40353"}}
  ) {
    object {
      id
      display_label
      hfid
      __typename
    }
    __typename
  }
}

However the SDK would provide a graphql mutation which looks like the following:

mutation {
    DcimDeviceUpsert(
        data: {
            status: {
                value: "active"
            }
            name: {
                value: "123123"
            }
            index: {
                value: 1
            }
            mlag_domain: null
            object_template: {
                id: "18779f71-55f1-390f-3bcf-10652e6ca9d3"
            }
            platform: {
                id: "test"
            }
            primary_address: null
            location: {
                id: "18779f72-1586-2058-3bc3-106527497af8"
            }
            device_type: null
        }
    ){
        ok
        object {
            id
        }
    }
}

You can see there are many null fields being passed in. This also happens when using object files to load data.

Expected Behavior

I would expect optional fields to be ignored and not instantiated by the client when creating a node the backend does not require them so no need to include them.

Steps to Reproduce

  1. Load infrahub with the base schema from the Schema library https://github.com/opsmill/schema-library/blob/main/base/dcim.yml#L87
  2. Create some manufactures and device_types
  3. Create a device type using an object file
---
apiVersion: infrahub.app/v1
kind: Object
spec:
  kind: DcimDevice
  data:
    # Paris HQ Building - Cisco Core Routers
    - name: CoreRouter1
      object_template: CISCO-ASR1006-CORE
      location: hq-01
      status: active
      index: 1
    - name: CoreRouter2
      object_template: CISCO-ASR1006-CORE
      location: hq-01
      status: active
      index: 2

Additional Information

No response</issue_description>

Comments on the Issue (you are @copilot in this section)

@minitriga Have to be careful we don't impact this change https://github.com//pull/515

✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

@coderabbitai
Copy link

coderabbitai bot commented Nov 15, 2025

Important

Review skipped

Bot user detected.

To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.


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

- Modified _generate_input_data() to skip adding None for optional relationships on new nodes
- For existing nodes, still include None to allow clearing relationships (preserves PR #515 behavior)
- Updated tests to reflect new behavior
- Added test to verify existing nodes still work correctly

Co-authored-by: minitriga <[email protected]>
@cloudflare-workers-and-pages
Copy link

cloudflare-workers-and-pages bot commented Nov 15, 2025

Deploying infrahub-sdk-python with  Cloudflare Pages  Cloudflare Pages

Latest commit: f0ee3cd
Status: ✅  Deploy successful!
Preview URL: https://555d3371.infrahub-sdk-python.pages.dev
Branch Preview URL: https://copilot-fix-optional-attribu.infrahub-sdk-python.pages.dev

View logs

Copilot AI changed the title [WIP] Fix bug with optional attributes in object templates Fix optional relationships preventing object template defaults from applying Nov 15, 2025
Copilot AI requested a review from minitriga November 15, 2025 00:59
Copilot finished work on behalf of minitriga November 15, 2025 00:59
@minitriga minitriga marked this pull request as ready for review November 15, 2025 01:03
@codecov
Copy link

codecov bot commented Nov 15, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.

@@           Coverage Diff           @@
##           stable     #634   +/-   ##
=======================================
  Coverage   75.48%   75.48%           
=======================================
  Files         113      113           
  Lines        9512     9513    +1     
  Branches     1893     1894    +1     
=======================================
+ Hits         7180     7181    +1     
  Misses       1832     1832           
  Partials      500      500           
Flag Coverage Δ
integration-tests 34.89% <66.66%> (-0.01%) ⬇️
python-3.10 48.77% <66.66%> (-0.02%) ⬇️
python-3.11 48.77% <66.66%> (-0.02%) ⬇️
python-3.12 48.75% <66.66%> (+<0.01%) ⬆️
python-3.13 48.73% <66.66%> (-0.04%) ⬇️
python-3.9 47.45% <33.33%> (-0.03%) ⬇️
python-filler-3.12 24.29% <33.33%> (-0.01%) ⬇️

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

Files with missing lines Coverage Δ
infrahub_sdk/node/node.py 76.77% <100.00%> (+0.02%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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: Creating an object with an object template with optional attributes fails to set the optional attributes

2 participants