Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle optional Vec and BTreeMap as Option<t> #41

Merged
merged 1 commit into from
Apr 2, 2022

Conversation

alex-hunt-materialize
Copy link
Contributor

Previously, optional array properties are required in the generated struct, but skipped if serializing. This is incorrect behavior, since an empty Vec is not the same as not supplying a Vec.

This breaks server-side apply-ing new resources to remove things from the Vec, as well as cases where a property is required in a one-of, but you actually want to send the empty Vec as one of those properties.

This change instead makes it an Option<Vec> with #[serde(default, skip_serializing_if = "Option::is_none")].

The above also applies to BTreeMaps.

Resolves #40

Previously, optional array properties are required in the generated struct, but skipped if serializing. This is incorrect behavior, since an empty Vec is not the same as not supplying a Vec.

This breaks server-side apply-ing new resources to remove things from the Vec, as well as cases where a property is required in a one-of, but you actually want to send the empty Vec as one of those properties.

This change instead makes it an Option<Vec<T>> with #[serde(default, skip_serializing_if = "Option::is_none")].

The above also applies to BTreeMaps.

Resolves kube-rs#40

Signed-off-by: Alex Hunt <alex.hunt@materialize.com>
@alex-hunt-materialize alex-hunt-materialize marked this pull request as ready for review April 1, 2022 22:53
@@ -17,7 +17,6 @@ Requirements:
## Features

- **Instantly queryable**: generated type uses [`kube-derive`](https://docs.rs/kube/latest/kube/derive.CustomResource.html) to provide api integration with `kube`
- **Ergonomic Rust types**: `#[serde(default)]` on `Vec`/`BTreeMap` over `Option` wrapping
Copy link
Member

@clux clux Apr 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This breaks server-side apply-ing new resources to remove things from the Vec, as well as cases where a property is required in a one-of, but you actually want to send the empty Vec as one of those properties.

While there are ways to get around apply (json patch, merge patch), this definitely make the current setup less attractive. There's a similar issue in kube-rs on what we should do with it, but I think this is ultimately the right decision 😞

@@ -210,32 +210,14 @@ fn analyze_object_properties(
docs: member_doc,
})
} else {
// option wrapping possibly needed if not required
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah lol, foreshadowing comment haunting me

Comment on lines -51 to -52
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub groups: Vec<PrometheusRuleGroups>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for doing this, and regenerating the docs!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem, thanks for reviewing it!

@clux clux merged commit 8cb2024 into kube-rs:main Apr 2, 2022
@alex-hunt-materialize alex-hunt-materialize deleted the handle_optional_vec branch April 4, 2022 16:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Optional array properties should be Option<Vec<T>>, not Vec<T> with skip_serializing_if = "Vec::is_empty"
2 participants