Skip to content

Commit d4b3506

Browse files
authored
Merge pull request #114 from haarchri/test
Pydantic, exclude_defaults=True re-add `apiVersion` and `kind`
2 parents 9c44291 + 70e86c0 commit d4b3506

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

crossplane/function/resource.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,15 @@ def update(r: fnv1.Resource, source: dict | structpb.Struct | pydantic.BaseModel
3838
"""
3939
match source:
4040
case pydantic.BaseModel():
41-
r.resource.update(source.model_dump(exclude_defaults=True, warnings=False))
41+
data = source.model_dump(exclude_defaults=True, warnings=False)
42+
# In Pydantic, exclude_defaults=True in model_dump excludes fields
43+
# that have their value equal to the default. If a field like
44+
# apiVersion is set to its default value 's3.aws.upbound.io/v1beta2'
45+
# (and not explicitly provided during initialization), it will be
46+
# excluded from the serialized output.
47+
data['apiVersion'] = source.apiVersion
48+
data['kind'] = source.kind
49+
r.resource.update(data)
4250
case structpb.Struct():
4351
# TODO(negz): Use struct_to_dict and update to match other semantics?
4452
r.resource.MergeFrom(source)

tests/test_resource.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,11 @@ class TestCase:
9090
),
9191
want=fnv1.Resource(
9292
resource=resource.dict_to_struct(
93-
{"spec": {"forProvider": {"region": "us-west-2"}}}
93+
{
94+
"apiVersion": "s3.aws.upbound.io/v1beta1",
95+
"kind": "Bucket",
96+
"spec": {"forProvider": {"region": "us-west-2"}},
97+
}
9498
),
9599
),
96100
),

tests/testdata/models/io/upbound/aws/s3/v1beta2.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -759,11 +759,11 @@ class Status(BaseModel):
759759

760760

761761
class Bucket(BaseModel):
762-
apiVersion: Optional[str] = None
762+
apiVersion: Optional[str] = 's3.aws.upbound.io/v1beta2'
763763
"""
764764
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
765765
"""
766-
kind: Optional[str] = None
766+
kind: Optional[str] = 'Bucket'
767767
"""
768768
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
769769
"""

0 commit comments

Comments
 (0)