Skip to content

Commit 1a40d62

Browse files
committed
test(kube-derive): Add failing test for a flattened and untagged enum
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 <sebastian.bernauer@stackable.tech> Signed-off-by: Nick Larsen <nick.larsen@stackable.tech>
1 parent 58cf5a6 commit 1a40d62

1 file changed

Lines changed: 128 additions & 0 deletions

File tree

kube-derive/tests/crd_complex_enum_test.rs

Lines changed: 128 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,95 @@ 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+
"three",
558+
"two"
559+
]
560+
},
561+
{}
562+
],
563+
"description": "Put a [`UntaggedEnum`] behind `#[serde(flatten)]`",
564+
"properties": {
565+
"one": {
566+
"type": "string"
567+
},
568+
"two": {
569+
"description": "A very simple enum with unit variants",
570+
"enum": [
571+
"C",
572+
"D",
573+
"A",
574+
"B"
575+
],
576+
"type": "string"
577+
},
578+
"three": {
579+
"type": "string"
580+
},
581+
},
582+
"type": "object"
583+
}
584+
},
585+
"required": [
586+
"foo"
587+
],
588+
"type": "object"
589+
}
590+
},
591+
"required": [
592+
"spec"
593+
],
594+
"title": "FlattenedUntaggedEnumTest",
595+
"type": "object"
596+
}
597+
},
598+
"served": true,
599+
"storage": true,
600+
"subresources": {}
601+
}
602+
]
603+
}
604+
}
605+
)
606+
);
607+
}

0 commit comments

Comments
 (0)