Title: Serialize models with exclude_unset, not exclude_defaults by negz · Pull Request #208 · crossplane/function-sdk-python · GitHub
Open Graph Title: Serialize models with exclude_unset, not exclude_defaults by negz · Pull Request #208 · crossplane/function-sdk-python
X Title: Serialize models with exclude_unset, not exclude_defaults by negz · Pull Request #208 · crossplane/function-sdk-python
Description: Fixes #207 Fixes #210 resource.update and resource.update_status serialized Pydantic models with model_dump(exclude_defaults=True). This PR makes two related corrections to how those functions serialize models. exclude_defaults → exclude_unset exclude_defaults asks "is this field different from its default?". The correct question for server-side apply is "did the caller set this field?", which exclude_unset answers. exclude_defaults also regressed with newer datamodel-code-generator, which emits object defaults as a raw dict with validate_default=True; the validated instance doesn't compare equal to the declared dict default, so unset fields like spec.providerConfigRef leaked into every composed resource. exclude_unset is immune to how a default is represented (see crossplane/cli#64 (comment)) — a field the caller didn't touch is absent from model_fields_set. Add by_alias=True Neither serialization passed by_alias=True, so a field carrying a Pydantic alias was emitted under its Python attribute name rather than its alias — the resource's real wire name. datamodel-code-generator can't name a field bool, int, from, continue, or schema, so it emits a bool_ attribute aliased to bool, and so on. A function composing such a field wrote bool_: true instead of bool: true, which doesn't match the resource's schema, so the API server rejects it or silently drops it. This surfaced with Kubernetes Dynamic Resource Allocation, whose device attribute value is a one-of over string, version, bool, and int. Passing by_alias=True is a no-op for ordinary fields and corrects only the keyword-collision cases. It's also symmetric with how pydantic deserializes these models: by default an aliased field is populated only by its alias, so reads and writes now both speak wire names. It's orthogonal to the exclude_unset change: one decides which fields are emitted, the other how they're named. I have: Read and followed Crossplane's contribution process. Added or updated unit tests for my change.
Open Graph Description: Fixes #207 Fixes #210 resource.update and resource.update_status serialized Pydantic models with model_dump(exclude_defaults=True). This PR makes two related corrections to how those functions seri...
X Description: Fixes #207 Fixes #210 resource.update and resource.update_status serialized Pydantic models with model_dump(exclude_defaults=True). This PR makes two related corrections to how those functions seri...
Opengraph URL: https://github.com/crossplane/function-sdk-python/pull/208
X: @github
Domain: github.com
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:a7674d9f-d6fa-1260-784d-6eee940c0db6 |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | ACE6:3E74E8:16106D1:1EA088E:6A61C17D |
| html-safe-nonce | d145e06d72036c1726f57abd062a4ec7e05b745f8f078424da851f00beb1fb61 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBQ0U2OjNFNzRFODoxNjEwNkQxOjFFQTA4OEU6NkE2MUMxN0QiLCJ2aXNpdG9yX2lkIjoiNjc4MTIwMDY3NzE4NzIwNzU0OSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | b56ea1616616e571e38bae20f7e496d6b5dfe4039f4a98839ce1063ade796928 |
| hovercard-subject-tag | pull_request:3799550577 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/crossplane/function-sdk-python/pull/208/files |
| twitter:image | https://avatars.githubusercontent.com/u/1049349?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/1049349?s=400&v=4 |
| og:image:alt | Fixes #207 Fixes #210 resource.update and resource.update_status serialized Pydantic models with model_dump(exclude_defaults=True). This PR makes two related corrections to how those functions seri... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | 6f4633bcf01c1ad14b73fd07dd39ac31d61f3d3c2578ee08ec1792b7b351eeb9 |
| turbo-cache-control | no-preview |
| diff-view | unified |
| go-import | github.com/crossplane/function-sdk-python git https://github.com/crossplane/function-sdk-python.git |
| octolytics-dimension-user_id | 45158470 |
| octolytics-dimension-user_login | crossplane |
| octolytics-dimension-repository_id | 721365259 |
| octolytics-dimension-repository_nwo | crossplane/function-sdk-python |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 721365259 |
| octolytics-dimension-repository_network_root_nwo | crossplane/function-sdk-python |
| turbo-body-classes | logged-out env-production page-responsive full-width |
| disable-turbo | true |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | ac296ae7f21856f1f92adbad22f870b6fbb4b907 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width