Skip to content

Commit 16180ab

Browse files
test(kube-derive): Add test for untagged flattened enums
Note: This comes from kube-rs#1839, with some modifications (an extra field to the B variant, and change comments to indicate untagged variant descriptions should not leak into fields). Co-authored-by: Sebastian Bernauer <[email protected]> Signed-off-by: Nick Larsen <[email protected]>
1 parent 58cf5a6 commit 16180ab

File tree

1 file changed

+124
-0
lines changed

1 file changed

+124
-0
lines changed

kube-derive/tests/crd_complex_enum_test.rs

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,34 @@ enum ComplexEnum {
4343
},
4444
}
4545

46+
/// An untagged enum with a nested enum inside
47+
#[derive(Serialize, Deserialize, Debug, Clone, JsonSchema)]
48+
#[serde(untagged)]
49+
enum UntaggedEnum {
50+
/// Used in case the `one` field of type [`u32`] is present
51+
///
52+
/// This should not appear in the schema because the "variant" disappears
53+
/// and this comment cannot pertain to all fields within the struct variant.
54+
A { one: String },
55+
/// Used in case the `two` field of type [`NormalEnum`] is present
56+
///
57+
/// This should not appear in the schema because the "variant" disappears
58+
/// and this comment cannot pertain to all fields within the struct variant.
59+
B { two: NormalEnum, three: String },
60+
/// Used in case no fields are present
61+
///
62+
/// This should not appear in the schema because the "variant" disappears
63+
/// and this comment cannot pertain to all fields within the struct variant.
64+
C {},
65+
}
66+
67+
/// Put a [`UntaggedEnum`] behind `#[serde(flatten)]`
68+
#[derive(Serialize, Deserialize, Debug, Clone, JsonSchema)]
69+
struct FlattenedUntaggedEnum {
70+
#[serde(flatten)]
71+
inner: UntaggedEnum,
72+
}
73+
4674
// CRD definitions
4775

4876
#[derive(CustomResource, Serialize, Deserialize, Debug, Clone, JsonSchema)]
@@ -77,9 +105,17 @@ struct ComplexEnumTestSpec {
77105
#[kube(group = "clux.dev", version = "v1", kind = "OptionalComplexEnumTest")]
78106
struct OptionalComplexEnumTestSpec {
79107
/// Optional complex enum field
108+
// When this doc-comment is missing, we suggest using the doc-comment of the
109+
// inner type - though that is debatable
80110
foo: Option<ComplexEnum>,
81111
}
82112

113+
#[derive(CustomResource, Serialize, Deserialize, Debug, Clone, JsonSchema)]
114+
#[kube(group = "clux.dev", version = "v1", kind = "FlattenedUntaggedEnumTest")]
115+
struct FlattenedUntaggedEnumTestSpec {
116+
foo: FlattenedUntaggedEnum,
117+
}
118+
83119
#[test]
84120
fn complex_enum() {
85121
assert_json_eq!(
@@ -477,3 +513,91 @@ fn optional_complex_enum() {
477513
})
478514
);
479515
}
516+
517+
#[test]
518+
fn flattened_untagged_enum() {
519+
assert_json_eq!(
520+
FlattenedUntaggedEnumTest::crd(),
521+
json!(
522+
{
523+
"apiVersion": "apiextensions.k8s.io/v1",
524+
"kind": "CustomResourceDefinition",
525+
"metadata": {
526+
"name": "flatteneduntaggedenumtests.clux.dev"
527+
},
528+
"spec": {
529+
"group": "clux.dev",
530+
"names": {
531+
"categories": [],
532+
"kind": "FlattenedUntaggedEnumTest",
533+
"plural": "flatteneduntaggedenumtests",
534+
"shortNames": [],
535+
"singular": "flatteneduntaggedenumtest"
536+
},
537+
"scope": "Cluster",
538+
"versions": [
539+
{
540+
"additionalPrinterColumns": [],
541+
"name": "v1",
542+
"schema": {
543+
"openAPIV3Schema": {
544+
"description": "Auto-generated derived type for FlattenedUntaggedEnumTestSpec via `CustomResource`",
545+
"properties": {
546+
"spec": {
547+
"properties": {
548+
"foo": {
549+
"anyOf": [
550+
{
551+
"required": [
552+
"one"
553+
]
554+
},
555+
{
556+
"required": [
557+
"two"
558+
]
559+
},
560+
{}
561+
],
562+
"description": "Put a [`UntaggedEnum`] behind `#[serde(flatten)]`",
563+
"properties": {
564+
"one": {
565+
"type": "string"
566+
},
567+
"two": {
568+
"description": "A very simple enum with unit variants",
569+
"enum": [
570+
"C",
571+
"D",
572+
"A",
573+
"B"
574+
],
575+
"type": "string"
576+
}
577+
},
578+
"type": "object"
579+
}
580+
},
581+
"required": [
582+
"foo"
583+
],
584+
"type": "object"
585+
}
586+
},
587+
"required": [
588+
"spec"
589+
],
590+
"title": "FlattenedUntaggedEnumTest",
591+
"type": "object"
592+
}
593+
},
594+
"served": true,
595+
"storage": true,
596+
"subresources": {}
597+
}
598+
]
599+
}
600+
}
601+
)
602+
);
603+
}

0 commit comments

Comments
 (0)