Skip to content

Commit

Permalink
Merge pull request #852 from stackabletech/fix/enum-descriptions
Browse files Browse the repository at this point in the history
Fix invalid CRD when Enum variants have descriptions
  • Loading branch information
nightkr authored Mar 18, 2022
2 parents 931cd48 + 10cfc4b commit a9d9975
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
25 changes: 24 additions & 1 deletion kube-core/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::collections::btree_map::Entry;
#[allow(unused_imports)] use schemars::gen::SchemaSettings;

use schemars::{
schema::{ObjectValidation, Schema, SchemaObject},
schema::{Metadata, ObjectValidation, Schema, SchemaObject},
visit::Visitor,
};

Expand Down Expand Up @@ -39,9 +39,24 @@ impl Visitor for StructuralSchemaRewriter {
if let Schema::Object(SchemaObject {
instance_type: variant_type,
object: Some(variant_obj),
metadata: variant_metadata,
..
}) = variant
{
if let Some(variant_metadata) = variant_metadata {
// Move enum variant description from oneOf clause to its corresponding property
if let Some(description) = std::mem::take(&mut variant_metadata.description) {
if let Some(Schema::Object(variant_object)) =
only_item(variant_obj.properties.values_mut())
{
let metadata = variant_object
.metadata
.get_or_insert_with(|| Box::new(Metadata::default()));
metadata.description = Some(description);
}
}
}

// Move all properties
let variant_properties = std::mem::take(&mut variant_obj.properties);
for (property_name, property) in variant_properties {
Expand Down Expand Up @@ -79,3 +94,11 @@ impl Visitor for StructuralSchemaRewriter {
}
}
}

fn only_item<I: Iterator>(mut i: I) -> Option<I::Item> {
let item = i.next()?;
if i.next().is_some() {
return None;
}
Some(item)
}
21 changes: 18 additions & 3 deletions kube-derive/tests/crd_schema_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ struct FooSpec {
// Using feature `chrono`
timestamp: DateTime<Utc>,

/// This is a complex enum
complex_enum: ComplexEnum,
}

Expand All @@ -51,8 +52,12 @@ fn default_nullable() -> Option<String> {
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone, JsonSchema)]
#[serde(rename_all = "camelCase")]
enum ComplexEnum {
/// First variant with an int
VariantOne { int: i32 },
/// Second variant with an String
VariantTwo { str: String },
/// Third variant which doesn't has an attribute
VariantThree {},
}

#[test]
Expand Down Expand Up @@ -177,7 +182,8 @@ fn test_crd_schema_matches_expected() {
"format": "int32"
}
},
"required": ["int"]
"required": ["int"],
"description": "First variant with an int"
},
"variantTwo": {
"type": "object",
Expand All @@ -186,7 +192,12 @@ fn test_crd_schema_matches_expected() {
"type": "string"
}
},
"required": ["str"]
"required": ["str"],
"description": "Second variant with an String"
},
"variantThree": {
"type": "object",
"description": "Third variant which doesn't has an attribute"
}
},
"oneOf": [
Expand All @@ -195,8 +206,12 @@ fn test_crd_schema_matches_expected() {
},
{
"required": ["variantTwo"]
},
{
"required": ["variantThree"]
}
]
],
"description": "This is a complex enum"
}
},
"required": [
Expand Down

0 comments on commit a9d9975

Please sign in to comment.