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 """