Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
295c431
Add some tests for CRD generation for enums
sbernauer Oct 8, 2025
13db30c
Update assertions to valid CRDs from kube 1.1.0
sbernauer Oct 9, 2025
fb77413
test: Add some un-documented variants to the NormalEnum
NickLarsenNZ Oct 9, 2025
0cb62f1
test: Partial new impl for one_of hoisting
NickLarsenNZ Oct 9, 2025
85eaf36
Finish enum oneOf hoisting
sbernauer Oct 9, 2025
285ab64
adjust and document the hoist_one_of_enum function
NickLarsenNZ Oct 10, 2025
e256f3a
test: Add more basic testing to ensure hoisting is not performed when…
NickLarsenNZ Oct 10, 2025
35034c5
hoist anyOf optional enums
NickLarsenNZ Oct 10, 2025
3850a19
left a todo for empty structural variants in untagged enums
NickLarsenNZ Oct 10, 2025
c2fbe70
test: Add missing enum entries to expected schemas
NickLarsenNZ Oct 10, 2025
8abef36
test: Add empty object handling to existing hoisting function
NickLarsenNZ Oct 10, 2025
8f578c2
fix: Change hoist_any_of_option_enum to cater to nested any_of as wel…
NickLarsenNZ Oct 14, 2025
c23aabc
test: Add test for internal function, and add docs after seeing it's …
NickLarsenNZ Oct 14, 2025
e91665b
feat: Add functions for specific hoisting tasks
NickLarsenNZ Oct 21, 2025
f13a751
chore: Add a crude fix to code that will be removed in a later commit
NickLarsenNZ Oct 21, 2025
730e0db
fix: update changed function name in tests
NickLarsenNZ Oct 21, 2025
7503fcc
chore: Make new hoisting functions pub(crate)
NickLarsenNZ Oct 21, 2025
17e7a28
chore: Move schema.rs to schema/mod.rs
NickLarsenNZ Oct 21, 2025
cc6b20a
feat: Implement new hoisting logic
NickLarsenNZ Oct 21, 2025
8e40d01
fix: Handle property hoisting for tagged enums (oneOf subschemas)
NickLarsenNZ Oct 22, 2025
e311361
lint: commit whitespace differences
NickLarsenNZ Oct 22, 2025
307e828
chore: use assert/assert_eq with message instead of panic
NickLarsenNZ Oct 22, 2025
25e10db
Add integration test for (optional) complex enums
sbernauer Oct 23, 2025
764c418
Add test case for Option<Option<Option<String>>>
sbernauer Oct 23, 2025
d52c2c6
Remove leftover newline
sbernauer Oct 23, 2025
6d156a0
Add (failing) test for enums without descriptions
sbernauer Oct 23, 2025
e3172d5
Improve test comment
sbernauer Oct 23, 2025
ef6da4f
fix: Remove trailing nulls for optional enums
sbernauer Oct 27, 2025
d0fe594
chore: fix comments, use iter_mut
NickLarsenNZ Oct 27, 2025
39df989
chore: fix comments/descriptions in tests
NickLarsenNZ Oct 27, 2025
2e0a7aa
chore: add expected results to complex enum tests
NickLarsenNZ Oct 27, 2025
4668b97
chore: Fix tests after merge
NickLarsenNZ Oct 27, 2025
021efc8
chore: Fix import after merge
NickLarsenNZ Oct 27, 2025
92fb0e4
fix: adjust metadata hoisting
NickLarsenNZ Oct 27, 2025
438bc7f
test: add few more hoisting tests
NickLarsenNZ Oct 27, 2025
a5d68d8
chore: Remove unused code
NickLarsenNZ Oct 27, 2025
920d6aa
test: Add some tests on untagged enums duplicated fields
sbernauer Oct 27, 2025
c856801
Apply suggestions from code review
NickLarsenNZ Oct 27, 2025
b5cf5a4
Merge remote-tracking branch 'origin/main' into test/add-enum-tests
NickLarsenNZ Oct 27, 2025
cde8cc8
Re-group imports
sbernauer Oct 28, 2025
9c19f10
Move tests into own "tests" module
sbernauer Oct 28, 2025
1ce7e47
Move null schema into NULL_SCHEMA static
sbernauer Oct 28, 2025
9eaedbb
refactor: use more descriptive variable names
NickLarsenNZ Oct 28, 2025
0477fd0
Apply suggestions from code review
NickLarsenNZ Oct 28, 2025
a237e1e
refactor: Replace panic with pattern match
NickLarsenNZ Oct 28, 2025
dcbe17e
refactor: Simplify description preservation and type dropping
NickLarsenNZ Oct 28, 2025
acf3d57
refactor: Simplify the getting or defaulting of the parent object
NickLarsenNZ Oct 28, 2025
925a8e3
refactor: Reorder test enums
NickLarsenNZ Oct 28, 2025
11a8ddc
docs: Revise doc comments
NickLarsenNZ Oct 28, 2025
26146ea
Revert "refactor: Simplify description preservation and type dropping"
NickLarsenNZ Oct 28, 2025
75c7ffa
refactor: Revise code after reverting
NickLarsenNZ Oct 28, 2025
2055029
test: Rework tests that check that the CRDs are valid
sbernauer Oct 28, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
158 changes: 90 additions & 68 deletions kube-core/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::{
collections::{btree_map::Entry, BTreeMap, BTreeSet},
ops::{Deref, Not},

};

/// schemars [`Visitor`] that rewrites a [`Schema`] to conform to Kubernetes' "structural schema" rules
Expand Down Expand Up @@ -252,9 +252,10 @@ enum SingleOrVec<T> {
#[cfg(test)]
mod test {
use assert_json_diff::assert_json_eq;
use schemars::{json_schema, schema_for, schema_for_value, JsonSchema};
use serde::{de::Expected, Deserialize, Serialize};
use serde_json::json;
use schemars::{json_schema, schema_for, JsonSchema};
use serde::{Deserialize, Serialize};

use super::*;

/// A very simple enum with empty variants
#[derive(Serialize, Deserialize, Debug, Clone, JsonSchema)]
Expand All @@ -274,82 +275,103 @@ mod test {

#[test]
fn hoisting_a_schema() {
let incoming = json_schema!(
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"description": "A very simple enum with empty variants",
"oneOf": [
{
"enum": [
"C",
"D"
let schemars_schema = schema_for!(NormalEnum);
let mut kube_schema: crate::schema::Schema =
schemars_schema_to_kube_schema(schemars_schema.clone()).unwrap();

assert_json_eq!(
schemars_schema,
json_schema!(
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"description": "A very simple enum with empty variants",
"oneOf": [
{
"enum": [
"C",
"D"
],
"type": "string"
},
{
"const": "A",
"description": "First variant",
"type": "string"
},
{
"const": "B",
"description": "Second variant",
"type": "string"
}
],
"type": "string"
},
{
"const": "A",
"description": "First variant",
"type": "string"
},
{
"const": "B",
"description": "Second variant",
"type": "string"
"title": "NormalEnum"
}
],
"title": "NormalEnum"
}
)
);

// Initial check that the text schema above is correct for NormalEnum
assert_json_eq!(schema_for!(NormalEnum), incoming);

let expected = json_schema!(
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"description": "A very simple enum with empty variants",
"type": "string",
"enum": [
"C",
"D",
"A",
"B"
],
"title": "NormalEnum"
}
hoist_one_of_enum(&mut kube_schema);
assert_json_eq!(
kube_schema,
json_schema!(
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"description": "A very simple enum with empty variants",
"type": "string",
"enum": [
"C",
"D",
"A",
"B"
],
"title": "NormalEnum"
}
)
);

// let actual = hoist

// assert_json_eq!(expected, actual);
}
}

fn hoist_one_of_enum(incoming: Schema) -> Schema {
let Schema::Object(SchemaObject {
#[cfg(test)]
fn schemars_schema_to_kube_schema(incoming: schemars::Schema) -> Result<Schema, serde_json::Error> {
serde_json::from_value(incoming.to_value())
}

#[cfg(test)]
fn hoist_one_of_enum(schema: &mut Schema) {
if let Schema::Object(SchemaObject {
subschemas: Some(subschemas),
instance_type: object_type,
enum_values: object_ebum,
..
}) = &incoming
else {
return incoming;
};

let SubschemaValidation {
}) = schema && let SubschemaValidation {
one_of: Some(one_of), ..
} = subschemas.deref()
else {
return incoming;
};

if one_of.is_empty() {
return incoming;
}

// now the meat. Need to get the oneOf variants up into `enum`
// panic if the types differ
} = &**subschemas
&& !one_of.is_empty()
{
let mut types = one_of.iter().map(|obj| match obj {
Schema::Object(SchemaObject { instance_type: Some(type_), ..}) => type_,
Schema::Object(_) => panic!("oneOf variants need to define a type!: {obj:?}"),
Schema::Bool(_) => panic!("oneOf variants can not be of type boolean"),
});
let enums = one_of.iter().flat_map(|obj| match obj {
Schema::Object(SchemaObject {enum_values: Some(enum_), ..}) => enum_.clone(),
Schema::Object(SchemaObject {other, ..})=> match other.get("const") {
Some(const_) => vec![const_.clone()],
None => panic!("oneOf variant did not provide \"enum\" or \"const\": {obj:?}"),
},
Schema::Bool(_) => panic!("oneOf variants can not be of type boolean"),
});

let first_type = types
.next()
.expect("oneOf must have at least one variant - we already checked that");
if types.any(|t| t != first_type) {
panic!("All oneOf variants must have the same type");
}

*object_type = Some(first_type.clone());
*object_ebum = Some(enums.collect());
subschemas.one_of = None;

todo!("finish it")
}
}

impl Transform for StructuralSchemaRewriter {
Expand Down
Loading