Skip to content

Instantly share code, notes, and snippets.

@nightkr
Created March 22, 2024 16:37
Show Gist options
  • Save nightkr/af2236b2d3cf77eb71242124d588a638 to your computer and use it in GitHub Desktop.
Save nightkr/af2236b2d3cf77eb71242124d588a638 to your computer and use it in GitHub Desktop.
diff --git a/crates/stackable-operator/examples/nodesel.rs b/crates/stackable-operator/examples/nodesel.rs
new file mode 100644
index 0000000..d6de42b
--- /dev/null
+++ b/crates/stackable-operator/examples/nodesel.rs
@@ -0,0 +1,19 @@
+use schemars::JsonSchema;
+use stackable_operator::commons::affinity::{
+ optional_stackable_node_selector_schema, StackableNodeSelector,
+};
+
+#[derive(JsonSchema)]
+struct Foo {
+ required: StackableNodeSelector,
+ optional_old: Option<StackableNodeSelector>,
+ #[schemars(schema_with = "optional_stackable_node_selector_schema")]
+ optional_new: Option<StackableNodeSelector>,
+}
+
+fn main() {
+ println!(
+ "{}",
+ serde_yaml::to_string(&schemars::schema_for!(Foo)).unwrap()
+ );
+}
diff --git a/crates/stackable-operator/src/commons/affinity.rs b/crates/stackable-operator/src/commons/affinity.rs
index 32fde90..67ccfe5 100644
--- a/crates/stackable-operator/src/commons/affinity.rs
+++ b/crates/stackable-operator/src/commons/affinity.rs
@@ -51,8 +51,9 @@ pub struct StackableAffinity {
/// We `#[serde(flatten)]` the contained [`BTreeMap<String, String>`], so `serde_yaml` can deserialize everything as
/// expected. However the generated JsonSchema will be wrong, so we need to provide our custom one (see
/// <https://github.com/stackabletech/issues/issues/554> for details).
-#[derive(Clone, Debug, Eq, Deserialize, PartialEq, Serialize)]
+#[derive(Clone, Debug, Eq, Deserialize, JsonSchema, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
+#[schemars(deny_unknown_fields)]
pub struct StackableNodeSelector {
#[serde(flatten)]
pub node_selector: BTreeMap<String, String>,
$schema: http://json-schema.org/draft-07/schema#
title: Foo
type: object
required:
- optional_new
- required
properties:
optional_new:
type:
- object
- 'null'
additionalProperties:
type: string
optional_old:
anyOf:
- $ref: '#/definitions/StackableNodeSelector'
- type: 'null'
required:
$ref: '#/definitions/StackableNodeSelector'
definitions:
StackableNodeSelector:
description: |-
We can not simply use [`BTreeMap<String, String>`] in [`StackableAffinity`], as the fields needs to be [`Atomic`]. We can not mark it as [`Atomic`], as [`crate::config::fragment::FromFragment`] is already implemented for [`BTreeMap<String, String>`].
We `#[serde(flatten)]` the contained [`BTreeMap<String, String>`], so `serde_yaml` can deserialize everything as expected. However the generated JsonSchema will be wrong, so we need to provide our custom one (see <https://github.com/stackabletech/issues/issues/554> for details).
type: object
additionalProperties:
type: string
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment