Skip to content

Commit 0f0fe5a

Browse files
Add failing test
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 0f0fe5a

1 file changed

Lines changed: 119 additions & 0 deletions

File tree

kube-derive/tests/crd_complex_enum_test.rs

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,29 @@ 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+
/// This should not appear in the schema because the "variant" disappears
52+
/// and this comment cannot pertain to all fields within the struct variant.
53+
A { one: String },
54+
/// Used in case the `two` field of type [`NormalEnum`] is present
55+
/// This should not appear in the schema because the "variant" disappears
56+
/// and this comment cannot pertain to all fields within the struct variant.
57+
B { two: NormalEnum, three: String },
58+
/// Used in case no fields are present
59+
C {},
60+
}
61+
62+
/// Put a [`UntaggedEnum`] behind `#[serde(flatten)]`
63+
#[derive(Serialize, Deserialize, Debug, Clone, JsonSchema)]
64+
struct FlattenedUntaggedEnum {
65+
#[serde(flatten)]
66+
inner: UntaggedEnum,
67+
}
68+
4669
// CRD definitions
4770

4871
#[derive(CustomResource, Serialize, Deserialize, Debug, Clone, JsonSchema)]
@@ -77,9 +100,17 @@ struct ComplexEnumTestSpec {
77100
#[kube(group = "clux.dev", version = "v1", kind = "OptionalComplexEnumTest")]
78101
struct OptionalComplexEnumTestSpec {
79102
/// Optional complex enum field
103+
// When this doc-comment is missing, we suggest using the doc-comment of the
104+
// inner type - though that is debatable
80105
foo: Option<ComplexEnum>,
81106
}
82107

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

0 commit comments

Comments
 (0)