Pydantic, exclude_defaults=True re-add apiVersion and kind#114
Pydantic, exclude_defaults=True re-add apiVersion and kind#114negz merged 1 commit intocrossplane:mainfrom
apiVersion and kind#114Conversation
Signed-off-by: Christopher Haar <christopher.haar@upbound.io>
| 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) |
There was a problem hiding this comment.
I don't love this workaround but I can't think of a better option.
I used exclude_defaults to stop model_dump from serializing optional fields as explicit None values. We could use exclude_none instead, but I think apart from apiVersion and kind we really don't want to include defaults - since we want to leave the defaulting to server-side apply.
There was a problem hiding this comment.
Actually, how feasible would it be to do the following?
- Update the models to remove all default values (except for
apiVersionandkind) - Switch to
exclude_none
There was a problem hiding this comment.
people will lose the information which default values are available - in lsp - mhm
There was a problem hiding this comment.
apiVersion and kind are special - so i don't think the solution at all is a problem
|
Overriding failing tests - they need to be fixed in main. |
Description of your changes
In Pydantic, exclude_defaults=True in model_dump excludes fields that have their value equal to the default. If a field like
apiVersionis set to its default value 's3.aws.upbound.io/v1beta2' (and not explicitly provided during initialization), it will be excluded from the serialized output.so we will explicit add apiVersion and kind
Fixes #
I have:
Tests
tested via
before:
—
with this PR: