Skip to content

Serialize Base objects in MappedObject - #389

Merged
zliang-akamai merged 3 commits into
linode:devfrom
zliang-akamai:zhiwei/expand-base-in-mappedobject
Apr 9, 2024
Merged

Serialize Base objects in MappedObject#389
zliang-akamai merged 3 commits into
linode:devfrom
zliang-akamai:zhiwei/expand-base-in-mappedobject

Conversation

@zliang-akamai

Copy link
Copy Markdown
Member

📝 Description

Convert nested Base objects in a MappedObject to dict in the dict property of MappedObject.

This can prevent exception of TypeError: Object of type SomeBaseType is not JSON serializable when passing the dict property of a MappedObject into the json library for serializing.

✔️ How to Test

Automated Testing

make testunit

Manual Testing

import os
from linode_api4 import LinodeClient

client = LinodeClient(os.getenv("LINODE_TOKEN"))

instance, _ = client.linode.instance_create(
    ltype="g6-nanode-1",
    region="us-mia",
    image="linode/ubuntu22.04",
    label="my-ubuntu-linode"
)

print(instance.ips.dict)

Expected Result

Before (dev branch)
{
    "ipv4": {
        "public": [IPAddress: 172.233.181.127],
        "private": [],
        "shared": [],
        "reserved": [],
        "vpc": [],
    },
    "ipv6": {
        "slaac": IPAddress: 2a01:7e04::f03c:94ff:fe72:159a,
        "link_local": IPAddress: fe80::f03c:94ff:fe72:159a,
        "ranges": [],
    },
}
After
{
    "ipv4": {
        "public": [
            {
                "address": "172.233.181.52",
                "gateway": "172.233.181.1",
                "subnet_mask": "255.255.255.0",
                "prefix": 24,
                "type": "ipv4",
                "public": True,
                "rdns": "172-233-181-52.ip.linodeusercontent.com",
                "linode_id": 56675621,
                "region": "us-mia",
                "vpc_nat_1_1": None,
            }
        ],
        "private": [],
        "shared": [],
        "reserved": [],
        "vpc": [],
    },
    "ipv6": {
        "slaac": {
            "address": "2a01:7e04::f03c:94ff:fe72:158f",
            "gateway": "fe80::1",
            "subnet_mask": "ffff:ffff:ffff:ffff::",
            "prefix": 64,
            "type": "ipv6",
            "rdns": None,
            "linode_id": 56675621,
            "region": "us-mia",
            "public": True,
        },
        "link_local": {
            "address": "fe80::f03c:94ff:fe72:158f",
            "gateway": "fe80::1",
            "subnet_mask": "ffff:ffff:ffff:ffff::",
            "prefix": 64,
            "type": "ipv6",
            "rdns": None,
            "linode_id": 56675621,
            "region": "us-mia",
            "public": False,
        },
        "ranges": [],
    },
}

@zliang-akamai
zliang-akamai requested a review from a team as a code owner April 1, 2024 21:33
@zliang-akamai
zliang-akamai requested review from jriddle-linode, lgarber-akamai and yec-akamai and removed request for a team April 1, 2024 21:33
@zliang-akamai zliang-akamai added enhancement issues that request a enhancement breaking-change for breaking changes in the changelog. labels Apr 1, 2024

@lgarber-akamai lgarber-akamai left a comment

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.

@zliang-akamai This looks great, thanks for fixing this! Could you add a corresponding unit test?

@zliang-akamai
zliang-akamai force-pushed the zhiwei/expand-base-in-mappedobject branch from e36c55b to 4db25dc Compare April 5, 2024 18:30

@lgarber-akamai lgarber-akamai left a comment

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.

LGTM, nice work!

@yec-akamai yec-akamai left a comment

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.

Nice work! Works well on my end

@jriddle-linode jriddle-linode left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM

@zliang-akamai
zliang-akamai merged commit 37fbc5e into linode:dev Apr 9, 2024
@zliang-akamai
zliang-akamai deleted the zhiwei/expand-base-in-mappedobject branch April 9, 2024 17:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

breaking-change for breaking changes in the changelog. enhancement issues that request a enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants