Thanks to visit codestin.com
Credit goes to github.com

Skip to content

bug: Fetching peer inheriting from a generic does not fetch attributes #521

@LucasG0

Description

@LucasG0

h3. Component

[| #component]

No response

h3. Infrahub SDK version

[| #infrahub-sdk-version]

1.14

h3. Current Behavior

[| #current-behavior]

Considering the peer of a relationship R of a node N, if R inherits from a generic G and the relationship schema within N points to G (and not to R), then fetching the node N from the sdk wouldn't fill the attributes of the peer corresponding to R. Thus, the resulting peer node is missing attributes while they might not even be optional.

h3. Expected Behavior

[| #expected-behavior]

The related node should have its attributes filled.

h3. Steps to Reproduce

[| #steps-to-reproduce]

class TestFetchPeerInheriting(TestInfrahubApp):
    async def test_fetch_peer_inheriting(
        self, db: InfrahubDatabase, client: InfrahubClient, schemas_person: SchemaRoot, default_branch: Branch
    ) -> None:
        schema = {
            "version": "1.0",
            "generics": [
                {
                    "name": "GenericPerson",
                    "namespace": "Test",
                    "relationships": [
                        {
                            "name": "car",
                            "peer": "TestCarr",
                            "optional": True,
                            "cardinality": "one",
                        },
                    ],
                },
            ],
            "nodes": [
                {
                    "name": "Carr",
                    "namespace": "Test",
                    "default_filter": "name__value",
                    "attributes": [
                        {"name": "name", "kind": "Text", "unique": True},
                    ],
                    "relationships": [
                        {
                            "name": "owner",
                            "peer": "TestGenericPerson",
                            "optional": False,
                            "cardinality": "one",
                        },
                    ],
                },
                {
                    "name": "Personn",
                    "namespace": "Test",
                    "default_filter": "name__value",
                    "inherit_from": ["TestGenericPerson"],
                    "attributes": [
                        {"name": "name", "kind": "Text", "unique": True},
                    ],
                },
            ],
        }

        response = await client.schema.load(schemas=[schema])
        assert len(response.errors) == 0, response.errors

        person = await client.create(kind="TestPersonn", name="JohnDoe")
        await person.save()

        car = await client.create(kind="TestCarr", name="AMercedes", owner=person)
        await car.save()

        # clear store
        client.store._branches = {}

        car = await client.get(kind="TestCarr", name__value="AMercedes", prefetch_relationships=True)

        owner_name = car.owner.peer.name.value
        assert owner_name == "JohnDoe", owner_name <--- Raises an error, owner_name is `None`

h3. Additional Information

[| #additional-information]

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    type/bugSomething isn't working as expected

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions