Skip to content

Commit e256f3a

Browse files
committed
test: Add more basic testing to ensure hoisting is not performed when there are no oneOfs at the top level of the schema
1 parent 285ab64 commit e256f3a

File tree

1 file changed

+49
-7
lines changed

1 file changed

+49
-7
lines changed

kube-core/src/schema.rs

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,14 @@ mod test {
257257

258258
use super::*;
259259

260-
/// A very simple enum with empty variants
260+
/// A very simple enum with unit variants, and no comments
261+
#[derive(Serialize, Deserialize, Debug, Clone, JsonSchema)]
262+
enum NormalEnumNoComments {
263+
A,
264+
B,
265+
}
266+
267+
/// A very simple enum with unit variants, and comments
261268
#[derive(Serialize, Deserialize, Debug, Clone, JsonSchema)]
262269
enum NormalEnum {
263270
/// First variant
@@ -270,19 +277,49 @@ mod test {
270277
D,
271278
}
272279

273-
#[derive(Serialize, Deserialize, Debug, Clone, JsonSchema)]
274-
enum B {}
280+
#[test]
281+
fn schema_for_enum_without_comments() {
282+
let schemars_schema = schema_for!(NormalEnumNoComments);
283+
284+
assert_json_eq!(
285+
schemars_schema,
286+
// replace the json_schema with this to get the full output.
287+
// serde_json::json!(42)
288+
json_schema!(
289+
{
290+
"$schema": "https://json-schema.org/draft/2020-12/schema",
291+
"description": "A very simple enum with unit variants, and no comments",
292+
"enum": [
293+
"A",
294+
"B"
295+
],
296+
"title": "NormalEnumNoComments",
297+
"type": "string"
298+
}
299+
)
300+
);
301+
302+
let kube_schema: crate::schema::Schema =
303+
schemars_schema_to_kube_schema(schemars_schema.clone()).unwrap();
304+
305+
let hoisted_kube_schema = hoist_one_of_enum(kube_schema.clone());
306+
307+
// No hoisting needed
308+
assert_json_eq!(hoisted_kube_schema, kube_schema);
309+
}
275310

276311
#[test]
277-
fn hoisting_a_schema() {
312+
fn schema_for_enum_with_comments() {
278313
let schemars_schema = schema_for!(NormalEnum);
279314

280315
assert_json_eq!(
281316
schemars_schema,
317+
// replace the json_schema with this to get the full output.
318+
// serde_json::json!(42)
282319
json_schema!(
283320
{
284321
"$schema": "https://json-schema.org/draft/2020-12/schema",
285-
"description": "A very simple enum with empty variants",
322+
"description": "A very simple enum with unit variants, and comments",
286323
"oneOf": [
287324
{
288325
"enum": [
@@ -311,13 +348,18 @@ mod test {
311348
let kube_schema: crate::schema::Schema =
312349
schemars_schema_to_kube_schema(schemars_schema.clone()).unwrap();
313350

314-
let hoisted_kube_schema = hoist_one_of_enum(kube_schema);
351+
let hoisted_kube_schema = hoist_one_of_enum(kube_schema.clone());
352+
353+
assert_ne!(
354+
hoisted_kube_schema, kube_schema,
355+
"Hoisting was performed, so hoisted_kube_schema != kube_schema"
356+
);
315357
assert_json_eq!(
316358
hoisted_kube_schema,
317359
json_schema!(
318360
{
319361
"$schema": "https://json-schema.org/draft/2020-12/schema",
320-
"description": "A very simple enum with empty variants",
362+
"description": "A very simple enum with unit variants, and comments",
321363
"type": "string",
322364
"enum": [
323365
"C",

0 commit comments

Comments
 (0)