From 70e86c0d30a93a64750d29f0b3f1b04543439eee Mon Sep 17 00:00:00 2001 From: Christopher Haar Date: Mon, 6 Jan 2025 17:10:32 +0100 Subject: [PATCH] fix(defaults): add apiVersion and kind Signed-off-by: Christopher Haar --- crossplane/function/resource.py | 10 +++++++++- tests/test_resource.py | 6 +++++- tests/testdata/models/io/upbound/aws/s3/v1beta2.py | 4 ++-- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/crossplane/function/resource.py b/crossplane/function/resource.py index 322a482..3cc79a9 100644 --- a/crossplane/function/resource.py +++ b/crossplane/function/resource.py @@ -38,7 +38,15 @@ def update(r: fnv1.Resource, source: dict | structpb.Struct | pydantic.BaseModel """ match source: case pydantic.BaseModel(): - r.resource.update(source.model_dump(exclude_defaults=True, warnings=False)) + data = source.model_dump(exclude_defaults=True, warnings=False) + # In Pydantic, exclude_defaults=True in model_dump excludes fields + # that have their value equal to the default. If a field like + # apiVersion is set to its default value 's3.aws.upbound.io/v1beta2' + # (and not explicitly provided during initialization), it will be + # excluded from the serialized output. + data['apiVersion'] = source.apiVersion + data['kind'] = source.kind + r.resource.update(data) case structpb.Struct(): # TODO(negz): Use struct_to_dict and update to match other semantics? r.resource.MergeFrom(source) diff --git a/tests/test_resource.py b/tests/test_resource.py index e9c818f..2f9523b 100644 --- a/tests/test_resource.py +++ b/tests/test_resource.py @@ -90,7 +90,11 @@ class TestResource(unittest.TestCase): ), want=fnv1.Resource( resource=resource.dict_to_struct( - {"spec": {"forProvider": {"region": "us-west-2"}}} + { + "apiVersion": "s3.aws.upbound.io/v1beta1", + "kind": "Bucket", + "spec": {"forProvider": {"region": "us-west-2"}}, + } ), ), ), diff --git a/tests/testdata/models/io/upbound/aws/s3/v1beta2.py b/tests/testdata/models/io/upbound/aws/s3/v1beta2.py index 0a678e0..d6253ff 100644 --- a/tests/testdata/models/io/upbound/aws/s3/v1beta2.py +++ b/tests/testdata/models/io/upbound/aws/s3/v1beta2.py @@ -759,11 +759,11 @@ class Status(BaseModel): class Bucket(BaseModel): - apiVersion: Optional[str] = None + apiVersion: Optional[str] = 's3.aws.upbound.io/v1beta2' """ APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources """ - kind: Optional[str] = None + kind: Optional[str] = 'Bucket' """ Kind is a string value representing the REST resource this object represents. Servers may infer this from the endpoint the client submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds """