Title: resource.update doesn't pass by_alias, mangling keyword-named fields · Issue #210 · crossplane/function-sdk-python · GitHub
Open Graph Title: resource.update doesn't pass by_alias, mangling keyword-named fields · Issue #210 · crossplane/function-sdk-python
X Title: resource.update doesn't pass by_alias, mangling keyword-named fields · Issue #210 · crossplane/function-sdk-python
Description: What happened? resource.update and resource.update_status serialize Pydantic models with model_dump(exclude_unset=True) (exclude_defaults=True before #208), neither of which passes by_alias=True. So a model field that carries a Pydantic ...
Open Graph Description: What happened? resource.update and resource.update_status serialize Pydantic models with model_dump(exclude_unset=True) (exclude_defaults=True before #208), neither of which passes by_alias=True. S...
X Description: What happened? resource.update and resource.update_status serialize Pydantic models with model_dump(exclude_unset=True) (exclude_defaults=True before #208), neither of which passes by_alias=True. S...
Opengraph URL: https://github.com/crossplane/function-sdk-python/issues/210
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"resource.update doesn't pass by_alias, mangling keyword-named fields","articleBody":"### What happened?\n\n`resource.update` and `resource.update_status` serialize Pydantic models with `model_dump(exclude_unset=True)` (`exclude_defaults=True` before #208), neither of which passes `by_alias=True`. So a model field that carries a Pydantic alias is serialized under its **Python attribute name** rather than its alias, and the alias is the resource's real wire/JSON name.\n\nThis bites fields whose KRM name is a Python keyword. `datamodel-code-generator` (which `crossplane xpkg generate` / the CLI uses to generate models) can't name a field `bool`, `int`, `from`, `continue`, or `schema`, so it emits a `bool_` attribute aliased to `bool`, `int_` aliased to `int`, and so on. When a function composes a resource that sets such a field, `resource.update` writes the Python name into the desired resource:\n\n```python\ndata = source.model_dump(exclude_unset=True, warnings=False)\n# -\u003e {\"bool_\": True}, but the resource's field is \"bool\"\n```\n\nThe composed resource then carries `bool_: true` instead of `bool: true`, which doesn't match the resource's schema. The API server rejects it (or silently drops the unknown field), and the field the function set never takes effect.\n\nThis surfaced with Kubernetes Dynamic Resource Allocation, whose device attribute value is a one-of over `string`, `version`, `bool`, and `int`. `bool` and `int` generate aliased fields, so any function composing a DRA `ResourceClaim` / device attribute through a generated model hits this.\n\n### How can we reproduce it?\n\nGenerate a model for a CRD with a field named `bool` (or `int`/`from`/`continue`/`schema`), then compose it:\n\n```python\n# Generated model: bool_ aliased to \"bool\".\nattrs = Attributes(**{\"bool\": True})\n\nattrs.model_dump(exclude_unset=True)\n# {'bool_': True} \u003c- wrong key in the composed resource\n\nattrs.model_dump(exclude_unset=True, by_alias=True)\n# {'bool': True} \u003c- correct wire name\n```\n\n`resource.update` uses the first form, so the desired resource ends up with `bool_`.\n\n### How could the SDK help solve it?\n\nPass `by_alias=True` alongside `exclude_unset=True` in both `resource.update` and `resource.update_status`.\n\nIt's a safe, general fix. `datamodel-code-generator` only aliases fields whose names collide with Python keywords; every ordinary field — including camelCase ones like `clusterName` — is generated with the wire name as the attribute name and no alias, so `by_alias=True` is a no-op for them and corrects only the keyword cases. It's also orthogonal to the `exclude_unset` change in #208: one decides which fields are emitted, the other how they're named.\n\n### What environment did it happen in?\n\n- crossplane/function-sdk-python: `main` (and the `exclude_unset` branch from #208)\n- Models generated by `datamodel-code-generator` via the Crossplane CLI, which emits keyword-collision field aliases (see crossplane/cli#63, crossplane/cli#64)\n","author":{"url":"https://github.com/negz","@type":"Person","name":"negz"},"datePublished":"2026-06-08T20:09:03.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":0},"url":"https://github.com/210/function-sdk-python/issues/210"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:888ea569-92f3-ff34-7a56-4d2da10ead21 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A174:359CE8:128AC2:19470A:6A5FB859 |
| html-safe-nonce | a680bfe729db780836d16752bbbe610322874c9f81981bc0f30a070098f51948 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBMTc0OjM1OUNFODoxMjhBQzI6MTk0NzBBOjZBNUZCODU5IiwidmlzaXRvcl9pZCI6IjkwMzg0OTI2MzMwNTc0NDE4ODEiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 225d28222e362d218ca2d72282c48fa1efdbbc556ebbbd36d83dd497e090af14 |
| hovercard-subject-tag | issue:4616261865 |
| github-keyboard-shortcuts | repository,issues,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/_view_fragments/issues/show/crossplane/function-sdk-python/210/issue_layout |
| twitter:image | https://opengraph.githubassets.com/ea7dd8d33cc9c13e1e1f2f98e7db26f58d25da4042964d2fe2dd87c9dcccb776/crossplane/function-sdk-python/issues/210 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/ea7dd8d33cc9c13e1e1f2f98e7db26f58d25da4042964d2fe2dd87c9dcccb776/crossplane/function-sdk-python/issues/210 |
| og:image:alt | What happened? resource.update and resource.update_status serialize Pydantic models with model_dump(exclude_unset=True) (exclude_defaults=True before #208), neither of which passes by_alias=True. S... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | negz |
| hostname | github.com |
| expected-hostname | github.com |
| None | e0cfc367faf9c4c774d2a903897aa214c786247c4f45ffcee48cda2b819fb0c8 |
| turbo-cache-control | no-preview |
| 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 |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 94569c859bc88d6bf7de7e0ce3f2a11cd529a131 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width