Serialize models with exclude_unset, not exclude_defaults#208
Open
negz wants to merge 1 commit into
Open
Conversation
resource.update and update_status serialized Pydantic models with model_dump(exclude_defaults=True), which 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. It emits object defaults as a raw dict with validate_default=True instead of a default_factory. The default is validated into a model instance at construction, which doesn't compare equal to the declared dict default, so exclude_defaults fails to exclude it. Unset fields like spec.providerConfigRef then leaked into every composed resource. exclude_unset is immune to how a default is represented: a field the caller didn't touch is absent from model_fields_set. It also keeps fields the caller explicitly set to their default value, which is more correct under server-side apply, where setting a field claims ownership of it. The apiVersion and kind workaround stays. Functions build models with kwargs and rarely pass these, so they're unset and excluded either way. See crossplane#207 for more detail. Signed-off-by: Nic Cope <[email protected]>
Member
Author
|
I just pinned a fairly huge Python function project to use this commit and I'm not seeing anything concerning. As expected my tests need a few updates for cases where the function code explicitly set a field to its defaults. Before this change those fields would be omitted on serialization - now they're emitted. |
This was referenced Jun 4, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #207
resource.updateandresource.update_statusserialized Pydantic models withmodel_dump(exclude_defaults=True), which asks "is this field different from its default?" and includes it if it is. The correct question for server-side apply is "did the caller set this field?", whichexclude_unsetanswers.exclude_defaultsalso regressed with newerdatamodel-code-generator, which emits object defaults as a raw dict withvalidate_default=Trueinstead of adefault_factory. The default is validated into a model instance at construction, which doesn't compare equal to the declared dict default, soexclude_defaultsfails to exclude it. Unset fields likespec.providerConfigRefthen leaked into every composed resource.exclude_unsetis immune to how a default is represented (see crossplane/cli#64 (comment)). A field the caller didn't touch is absent frommodel_fields_set. It also keeps fields the caller explicitly set to their default value, which is more correct under server-side apply, where setting a field claims ownership of it.The tests cover both
datamodel-code-generatorstyles: the existing S3Bucketmodel (olddefault_factoryobject defaults) and a new IAMAccountAliasmodel (newervalidate_default=Trueobject defaults). TheAccountAliascase is the one that fails underexclude_defaults.I have: