kopium 0.16.4

kubernetes openapi unmangler
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
//! Deals entirely with schema analysis for the purpose of creating output structs + members
use crate::{Container, Member, Output};
use anyhow::{bail, Result};
use heck::ToUpperCamelCase;
use k8s_openapi::apiextensions_apiserver::pkg::apis::apiextensions::v1::{
    JSONSchemaProps, JSONSchemaPropsOrArray, JSONSchemaPropsOrBool, JSON,
};
use std::collections::{BTreeMap, HashMap};

const IGNORED_KEYS: [&str; 3] = ["metadata", "apiVersion", "kind"];

/// Scan a schema for structs and members, and recurse to find all structs
///
/// All found output structs will have its names prefixed by the kind it is for
pub fn analyze(schema: JSONSchemaProps, kind: &str) -> Result<Output> {
    let mut res = vec![];
    analyze_(&schema, "", kind, 0, &mut res)?;
    Ok(Output(res))
}

/// Scan a schema for structs and members, and recurse to find all structs
///
/// schema: root schema / sub schema
/// current: current key name (or empty string for first call) - must capitalize first letter
/// stack: stacked concat of kind + current_{n-1} + ... + current (used to create dedup names/types)
/// level: recursion level (start at 0)
/// results: multable list of generated structs (not deduplicated)
fn analyze_(
    schema: &JSONSchemaProps,
    current: &str,
    stack: &str,
    level: u8,
    results: &mut Vec<Container>,
) -> Result<()> {
    let props = schema.properties.clone().unwrap_or_default();
    let mut array_recurse_level: HashMap<String, u8> = Default::default();

    // create a Container if we have a container type:
    //trace!("analyze_ with {} + {}", current, stack);
    if schema.type_.clone().unwrap_or_default() == "object" {
        // we can have additionalProperties XOR properties
        // https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions/#validation
        if let Some(JSONSchemaPropsOrBool::Schema(s)) = schema.additional_properties.as_ref() {
            let dict_type = s.type_.clone().unwrap_or_default();
            // object with additionalProperties == map
            if let Some(extra_props) = &s.properties {
                // map values is an object with properties
                debug!("Generating map struct for {} (under {})", current, stack);
                let c = extract_container(extra_props, stack, &mut array_recurse_level, level, schema)?;
                results.push(c);
            } else if !dict_type.is_empty() {
                warn!("not generating type {} - using {} map", current, dict_type);
                return Ok(()); // no members here - it'll be inlined
            }
        } else {
            // else, regular properties only
            debug!("Generating struct for {} (under {})", current, stack);
            // initial analysis of properties (we do not recurse here, we need to find members first)
            if props.is_empty() && schema.x_kubernetes_preserve_unknown_fields.unwrap_or(false) {
                warn!("not generating type {} - using BTreeMap", current);
                return Ok(());
            }
            let c = extract_container(&props, stack, &mut array_recurse_level, level, schema)?;
            results.push(c);
        }
    }
    //trace!("full schema here: {}", serde_yaml::to_string(&schema).unwrap());

    // If the container has members, we recurse through these members to find more containers.
    // NB: find_containers initiates recursion **for this container** and will end up invoking this fn,
    // so that we can create the Container with its members (same fn, above) in one step.
    // Once the Container has been made, we drop down here and restarting the process for its members.
    //
    // again; additionalProperties XOR properties
    let extras = if let Some(JSONSchemaPropsOrBool::Schema(s)) = schema.additional_properties.as_ref() {
        let extra_props = s.properties.clone().unwrap_or_default();
        find_containers(&extra_props, stack, &mut array_recurse_level, level, schema)?
    } else {
        // regular properties only
        find_containers(&props, stack, &mut array_recurse_level, level, schema)?
    };
    results.extend(extras);

    Ok(())
}

/// Dive into passed properties
///
/// This will recursively invoke the analyzer from any new type that needs investigation.
/// Upon recursion, we concatenate container names (so they are always unique across the tree)
/// and bump the level to have a way to sort the containers by depth.
fn find_containers(
    props: &BTreeMap<String, JSONSchemaProps>,
    stack: &str,
    array_recurse_level: &mut HashMap<String, u8>,
    level: u8,
    schema: &JSONSchemaProps,
) -> Result<Vec<Container>> {
    //trace!("finding containers in: {}", serde_yaml::to_string(&props)?);
    let mut results = vec![];
    for (key, value) in props {
        if level == 0 && IGNORED_KEYS.contains(&(key.as_ref())) {
            debug!("not recursing into ignored {}", key); // handled elsewhere
            continue;
        }
        let next_key = key.to_upper_camel_case();
        let next_stack = format!("{}{}", stack, next_key);
        let value_type = value.type_.clone().unwrap_or_default();
        match value_type.as_ref() {
            "object" => {
                // objects, maps
                let mut handled_inner = false;
                if let Some(JSONSchemaPropsOrBool::Schema(s)) = &value.additional_properties {
                    let dict_type = s.type_.clone().unwrap_or_default();
                    if dict_type == "array" {
                        // unpack the inner object from the array wrap
                        if let Some(JSONSchemaPropsOrArray::Schema(items)) = &s.as_ref().items {
                            debug!("..recursing into object member {}", key);
                            analyze_(items, &next_key, &next_stack, level + 1, &mut results)?;
                            handled_inner = true;
                        }
                    }
                    // TODO: not sure if these nested recurses are necessary - cluster test case does not have enough data
                    //if let Some(extra_props) = &s.properties {
                    //    for (_key, value) in extra_props {
                    //        debug!("..nested recurse into {} {} - key: {}", next_key, next_stack, _key);
                    //        analyze_(value.clone(), &next_key, &next_stack, level +1, results)?;
                    //    }
                    //}
                }
                if !handled_inner {
                    // normal object recurse
                    analyze_(value, &next_key, &next_stack, level + 1, &mut results)?;
                }
            }
            "array" => {
                if let Some(recurse) = array_recurse_level.get(key).cloned() {
                    let mut inner = value.clone();
                    for _i in 0..recurse {
                        debug!("..recursing into props for {}", key);
                        if let Some(sub) = inner.items {
                            match sub {
                                JSONSchemaPropsOrArray::Schema(s) => {
                                    //info!("got inner: {}", serde_json::to_string_pretty(&s)?);
                                    inner = *s.clone();
                                }
                                _ => bail!("only handling single type in arrays"),
                            }
                        } else {
                            bail!("could not recurse into vec");
                        }
                    }
                    analyze_(&inner, &next_key, &next_stack, level + 1, &mut results)?;
                }
            }
            "" => {
                if value.x_kubernetes_int_or_string.is_some() {
                    debug!("..not recursing into IntOrString {}", key)
                } else {
                    debug!("..not recursing into unknown empty type {}", key)
                }
            }
            x => {
                if let Some(en) = &value.enum_ {
                    // plain enums do not need to recurse, can collect it here
                    // ....although this makes it impossible for us to handle enums at the top level
                    // TODO: move this to the top level
                    let new_result = analyze_enum_properties(en, &next_stack, level, schema)?;
                    results.push(new_result);
                } else {
                    debug!("..not recursing into {} ('{}' is not a container)", key, x)
                }
            }
        }
    }
    Ok(results)
}

// helper to figure out what output enums and embedded members are contained in the current object schema
fn analyze_enum_properties(
    items: &Vec<JSON>,
    stack: &str,
    level: u8,
    schema: &JSONSchemaProps,
) -> Result<Container, anyhow::Error> {
    let mut members = vec![];
    debug!("analyzing enum {}", serde_json::to_string(&schema).unwrap());
    for en in items {
        debug!("got enum {:?}", en);
        // TODO: do we need to verify enum elements? only in oneOf only right?
        let name = match &en.0 {
            serde_json::Value::String(name) => name.to_string(),
            serde_json::Value::Number(val) => {
                if !val.is_u64() {
                    bail!("enum member cannot have signed/floating discriminants");
                }
                val.to_string()
            }
            _ => bail!("not handling non-string/int enum outside oneOf block"),
        };
        let rust_type = "".to_string();
        // Create member and wrap types correctly
        let member_doc = None;
        debug!("with enum member {}", name);
        members.push(Member {
            type_: rust_type,
            name: name.to_string(),
            serde_annot: vec![],
            extra_annot: vec![],
            docs: member_doc,
        })
    }
    Ok(Container {
        name: stack.to_string(),
        members,
        level,
        docs: schema.description.clone(),
        is_enum: true,
    })
}

// fully populate a Container with all its members given the current stack and schema position
fn extract_container(
    props: &BTreeMap<String, JSONSchemaProps>,
    stack: &str,
    array_recurse_level: &mut HashMap<String, u8>,
    level: u8,
    schema: &JSONSchemaProps,
) -> Result<Container, anyhow::Error> {
    let mut members = vec![];
    //debug!("analyzing object {}", serde_json::to_string(&schema).unwrap());
    let reqs = schema.required.clone().unwrap_or_default();
    for (key, value) in props {
        let value_type = value.type_.clone().unwrap_or_default();
        let rust_type = match value_type.as_ref() {
            "object" => {
                let mut dict_key = None;
                if let Some(additional) = &value.additional_properties {
                    dict_key = resolve_additional_properties(additional, stack, key, value)?;
                } else if value.properties.is_none()
                    && value.x_kubernetes_preserve_unknown_fields.unwrap_or(false)
                {
                    dict_key = Some("serde_json::Value".into());
                }
                if let Some(dict) = dict_key {
                    format!("BTreeMap<String, {}>", dict)
                } else {
                    format!("{}{}", stack, key.to_upper_camel_case())
                }
            }
            "string" => {
                if let Some(_en) = &value.enum_ {
                    trace!("got enum string: {}", serde_json::to_string(&schema).unwrap());
                    format!("{}{}", stack, key.to_upper_camel_case())
                } else {
                    "String".to_string()
                }
            }
            "boolean" => "bool".to_string(),
            "date" => extract_date_type(value)?,
            "number" => extract_number_type(value)?,
            "integer" => extract_integer_type(value)?,
            "array" => {
                // recurse through repeated arrays until we find a concrete type (keep track of how deep we went)
                let (array_type, recurse_level) = array_recurse_for_type(value, stack, key, 1)?;
                trace!("got array {} for {} in level {}", array_type, key, recurse_level);
                array_recurse_level.insert(key.clone(), recurse_level);
                array_type
            }
            "" => {
                if value.x_kubernetes_int_or_string.is_some() {
                    "IntOrString".into()
                } else if value.x_kubernetes_preserve_unknown_fields == Some(true) {
                    "HashMap<String, serde_json::Value>".into()
                } else {
                    bail!("unknown empty dict type for {}", key)
                }
            }
            x => bail!("unknown type {}", x),
        };

        // Create member and wrap types correctly
        let member_doc = value.description.clone();
        if reqs.contains(key) {
            debug!("with required member {} of type {}", key, &rust_type);
            members.push(Member {
                type_: rust_type,
                name: key.to_string(),
                serde_annot: vec![],
                extra_annot: vec![],
                docs: member_doc,
            })
        } else {
            // option wrapping needed if not required
            debug!("with optional member {} of type {}", key, rust_type);
            members.push(Member {
                type_: format!("Option<{}>", rust_type),
                name: key.to_string(),
                serde_annot: vec![
                    "default".into(),
                    "skip_serializing_if = \"Option::is_none\"".into(),
                ],
                extra_annot: vec![],
                docs: member_doc,
            })
            // TODO: must capture `default` key here instead of blindly using serde default
            // this will require us storing default properties for the member in above loop
            // This is complicated because serde default requires a default fn / impl Default
            // probably better to do impl Default to avoid having to make custom fns
        }
    }
    Ok(Container {
        name: stack.to_string(),
        members,
        level,
        docs: schema.description.clone(),
        is_enum: false,
    })
}

fn resolve_additional_properties(
    additional: &JSONSchemaPropsOrBool,
    stack: &str,
    key: &str,
    value: &JSONSchemaProps,
) -> Result<Option<String>, anyhow::Error> {
    debug!("got additional: {}", serde_json::to_string(&additional)?);
    let JSONSchemaPropsOrBool::Schema(s) = additional else {
        return Ok(None);
    };

    // This case is for maps. It is generally String -> Something, depending on the type key:
    let dict_type = s.type_.clone().unwrap_or_default();
    let dict_key = match dict_type.as_ref() {
        "string" => Some("String".into()),
        // We are not 100% sure the array and object subcases here are correct but they pass tests atm.
        // authoratative, but more detailed sources than crd validation docs below are welcome
        // https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions/#validation
        "array" => {
            let mut simple_inner = None;
            if let Some(JSONSchemaPropsOrArray::Schema(ix)) = &s.items {
                simple_inner = ix.type_.clone();
                debug!("additional simple inner  type: {:?}", simple_inner);
            }
            // Simple case: additionalProperties contain: {items: {type: K}}
            // Then it's a simple map (service_monitor_params) - but key is useless
            match simple_inner.as_deref() {
                Some("string") => Some("String".into()),
                Some("integer") => Some(extract_integer_type(s)?),
                Some("date") => Some(extract_date_type(value)?),
                Some("") => {
                    if s.x_kubernetes_int_or_string.is_some() {
                        Some("IntOrString".into())
                    } else {
                        bail!("unknown inner empty dict type for {}", key)
                    }
                }
                // can probably cover the regulars here as well

                // Harder case: inline structs under items (agent test with `validationInfo`)
                // key becomes the struct
                Some("object") => Some(format!("{}{}", stack, key.to_upper_camel_case())),
                None => Some(format!("{}{}", stack, key.to_upper_camel_case())),

                // leftovers, array of arrays?... need a better way to recurse probably
                Some(x) => bail!("unknown inner empty dict type {} for {}", x, key),
            }
        }
        "object" => {
            // cluster test with `failureDomains` uses this spec format
            Some(format!("{}{}", stack, key.to_upper_camel_case()))
        }
        "" => {
            if s.x_kubernetes_int_or_string.is_some() {
                Some("IntOrString".into())
            } else {
                bail!("unknown empty dict type for {}", key)
            }
        }
        "boolean" => Some("bool".to_string()),
        "integer" => Some(extract_integer_type(s)?),
        // think the type we get is the value type
        x => Some(x.to_upper_camel_case()), // best guess
    };

    Ok(dict_key)
}

// recurse into an array type to find its nested type
// this recursion is intialised and ended within a single step of the outer recursion
fn array_recurse_for_type(
    value: &JSONSchemaProps,
    stack: &str,
    key: &str,
    level: u8,
) -> Result<(String, u8)> {
    if let Some(items) = &value.items {
        match items {
            JSONSchemaPropsOrArray::Schema(s) => {
                if s.type_.is_none() && s.x_kubernetes_preserve_unknown_fields == Some(true) {
                    return Ok(("Vec<HashMap<String, serde_json::Value>>".into(), level));
                }
                let inner_array_type = s.type_.clone().unwrap_or_default();
                return match inner_array_type.as_ref() {
                    "object" => {
                        // Same logic as in `extract_container` to simplify types to maps.
                        let mut dict_value = None;
                        if let Some(additional) = &s.additional_properties {
                            dict_value = resolve_additional_properties(additional, stack, key, s)?;
                        }

                        let vec_value = if let Some(dict_value) = dict_value {
                            format!("BTreeMap<String, {dict_value}>")
                        } else {
                            let structsuffix = key.to_upper_camel_case();
                            format!("{stack}{structsuffix}")
                        };

                        Ok((format!("Vec<{}>", vec_value), level))
                    }
                    "string" => Ok(("Vec<String>".into(), level)),
                    "boolean" => Ok(("Vec<bool>".into(), level)),
                    "date" => Ok((format!("Vec<{}>", extract_date_type(value)?), level)),
                    "number" => Ok((format!("Vec<{}>", extract_number_type(value)?), level)),
                    "integer" => Ok((format!("Vec<{}>", extract_integer_type(value)?), level)),
                    "array" => Ok(array_recurse_for_type(s, stack, key, level + 1)?),
                    unknown => {
                        bail!("unsupported recursive array type \"{unknown}\" for {key}")
                    }
                };
            }
            // maybe fallback to serde_json::Value
            _ => bail!("only support single schema in array {}", key),
        }
    } else {
        bail!("missing items in array type")
    }
}

// ----------------------------------------------------------------------------
// helpers

fn extract_date_type(value: &JSONSchemaProps) -> Result<String> {
    Ok(if let Some(f) = &value.format {
        // NB: these need chrono feature on serde
        match f.as_ref() {
            // Not sure if the first actually works properly..
            // might need a Date<Utc> but chrono docs advocated for NaiveDate
            "date" => "NaiveDate".to_string(),
            "date-time" => "DateTime<Utc>".to_string(),
            x => {
                bail!("unknown date {}", x);
            }
        }
    } else {
        "String".to_string()
    })
}

fn extract_number_type(value: &JSONSchemaProps) -> Result<String> {
    // TODO: byte / password here?
    Ok(if let Some(f) = &value.format {
        match f.as_ref() {
            "float" => "f32".to_string(),
            "double" => "f64".to_string(),
            x => {
                bail!("unknown number {}", x);
            }
        }
    } else {
        "f64".to_string()
    })
}

fn extract_integer_type(value: &JSONSchemaProps) -> Result<String> {
    // Think kubernetes go types just do signed ints, but set a minimum to zero..
    // rust will set uint, so emitting that when possbile
    Ok(if let Some(f) = &value.format {
        match f.as_ref() {
            "int8" => "i8".to_string(),
            "int16" => "i16".to_string(),
            "int32" => "i32".to_string(),
            "int64" => "i64".to_string(),
            "int128" => "i128".to_string(),
            "uint8" => "u8".to_string(),
            "uint16" => "u16".to_string(),
            "uint32" => "u32".to_string(),
            "uint64" => "u64".to_string(),
            "uint128" => "u128".to_string(),
            x => {
                bail!("unknown integer {}", x);
            }
        }
    } else {
        "i64".to_string()
    })
}

// unit tests particular schema patterns
#[cfg(test)]
mod test {
    use crate::analyze;
    use k8s_openapi::apiextensions_apiserver::pkg::apis::apiextensions::v1::JSONSchemaProps;

    use std::sync::Once;

    static START: Once = Once::new();
    fn init() {
        START.call_once(|| {
            env_logger::init();
        });
    }
    // To debug individual tests:
    // RUST_LOG=debug cargo test --lib -- --nocapture testname

    #[test]
    fn map_of_struct() {
        init();
        // validationsInfo from agent test
        let schema_str = r#"
        description: AgentStatus defines the observed state of Agent
        properties:
          validationsInfo:
            additionalProperties:
              items:
                properties:
                  id:
                    type: string
                  message:
                    type: string
                  status:
                    type: string
                required:
                - id
                - message
                - status
                type: object
              type: array
            description: ValidationsInfo is a JSON-formatted string containing
              the validation results for each validation id grouped by category
              (network, hosts-data, etc.)
            type: object
        type: object
"#;
        let schema: JSONSchemaProps = serde_yaml::from_str(schema_str).unwrap();
        //println!("schema: {}", serde_json::to_string_pretty(&schema).unwrap());

        let structs = analyze(schema, "Agent").unwrap().0;
        //println!("{:?}", structs);
        let root = &structs[0];
        assert_eq!(root.name, "Agent");
        assert_eq!(root.level, 0);
        // should have a member with a key to the map:
        let map = &root.members[0];
        assert_eq!(map.name, "validationsInfo");
        assert_eq!(map.type_, "Option<BTreeMap<String, AgentValidationsInfo>>");
        // should have a separate struct
        let other = &structs[1];
        assert_eq!(other.name, "AgentValidationsInfo");
        assert_eq!(other.level, 1);
        assert_eq!(other.members[0].name, "id");
        assert_eq!(other.members[0].type_, "String");
        assert_eq!(other.members[1].name, "message");
        assert_eq!(other.members[1].type_, "String");
        assert_eq!(other.members[2].name, "status");
        assert_eq!(other.members[2].type_, "String");
    }

    #[test]
    fn empty_preserve_unknown_fields() {
        init();
        let schema_str = r#"
description: |-
  Identifies servers in the same namespace for which this authorization applies.
required:
  - selector
properties:
  selector:
    description: A label query over servers on which this authorization
      applies.
    required:
      - matchLabels
    properties:
      matchLabels:
        type: object
        x-kubernetes-preserve-unknown-fields: true
    type: object
type: object
"#;
        let schema: JSONSchemaProps = serde_yaml::from_str(schema_str).unwrap();
        //println!("schema: {}", serde_json::to_string_pretty(&schema).unwrap());
        let structs = analyze(schema, "Server").unwrap().0;
        //println!("{:#?}", structs);

        let root = &structs[0];
        assert_eq!(root.name, "Server");
        assert_eq!(root.level, 0);
        let root_member = &root.members[0];
        assert_eq!(root_member.name, "selector");
        assert_eq!(root_member.type_, "ServerSelector");
        let server_selector = &structs[1];
        assert_eq!(server_selector.name, "ServerSelector");
        assert_eq!(server_selector.level, 1);
        let match_labels = &server_selector.members[0];
        assert_eq!(match_labels.name, "matchLabels");
        assert_eq!(match_labels.type_, "BTreeMap<String, serde_json::Value>");
    }

    #[test]
    fn int_or_string() {
        init();
        let schema_str = r#"
            properties:
              port:
                description: A port name or number. Must exist in a pod spec.
                x-kubernetes-int-or-string: true
            required:
            - port
            type: object
"#;
        let schema: JSONSchemaProps = serde_yaml::from_str(schema_str).unwrap();

        let structs = analyze(schema, "Server").unwrap().0;
        let root = &structs[0];
        assert_eq!(root.name, "Server");
        // should have an IntOrString member:
        let member = &root.members[0];
        assert_eq!(member.name, "port");
        assert_eq!(member.type_, "IntOrString");
        assert!(root.uses_int_or_string());
        // TODO: check that anyOf: [type: integer, type: string] also works
    }

    #[test]
    fn boolean_in_additionals() {
        // as found in argo-app
        init();
        let schema_str = r#"
            properties:
              options:
                additionalProperties:
                  type: boolean
                type: object
              patch:
                type: string
            type: object
"#;
        let schema: JSONSchemaProps = serde_yaml::from_str(schema_str).unwrap();
        let structs = analyze(schema, "Options").unwrap().0;
        println!("got {:?}", structs);
        let root = &structs[0];
        assert_eq!(root.name, "Options");
        assert_eq!(root.level, 0);
        assert_eq!(&root.members[0].name, "options");
        assert_eq!(&root.members[0].type_, "Option<BTreeMap<String, bool>>");
    }

    #[test]
    fn enum_string() {
        init();
        let schema_str = r#"
      properties:
        operator:
          enum:
          - In
          - NotIn
          - Exists
          - DoesNotExist
          type: string
      required:
      - operator
      type: object
"#;

        let schema: JSONSchemaProps = serde_yaml::from_str(schema_str).unwrap();
        let structs = analyze(schema, "MatchExpressions").unwrap().0;
        println!("got {:?}", structs);
        let root = &structs[0];
        assert_eq!(root.name, "MatchExpressions");
        assert_eq!(root.level, 0);
        assert_eq!(&root.members[0].name, "operator");
        assert_eq!(&root.members[0].type_, "MatchExpressionsOperator");

        // operator member
        let op = &structs[1];
        assert!(op.is_enum);
        assert_eq!(op.name, "MatchExpressionsOperator");

        // should have enum members:
        assert_eq!(&op.members[0].name, "In");
        assert_eq!(&op.members[0].type_, "");
        assert_eq!(&op.members[1].name, "NotIn");
        assert_eq!(&op.members[1].type_, "");
        assert_eq!(&op.members[2].name, "Exists");
        assert_eq!(&op.members[2].type_, "");
        assert_eq!(&op.members[3].name, "DoesNotExist");
        assert_eq!(&op.members[3].type_, "");
    }

    #[test]
    fn enum_string_within_container() {
        init();
        let schema_str = r#"
      description: Endpoint
      properties:
        relabelings:
          items:
            properties:
              action:
                default: replace
                enum:
                - replace
                - keep
                - drop
                - hashmod
                - labelmap
                - labeldrop
                - labelkeep
                type: string
              modulus:
                format: int64
                type: integer
            type: object
          type: array
      type: object
        "#;

        let schema: JSONSchemaProps = serde_yaml::from_str(schema_str).unwrap();
        let structs = analyze(schema, "Endpoint").unwrap().0;
        println!("got {:?}", structs);
        let root = &structs[0];
        assert_eq!(root.name, "Endpoint");
        assert_eq!(root.level, 0);
        assert_eq!(root.is_enum, false);
        assert_eq!(&root.members[0].name, "relabelings");
        assert_eq!(&root.members[0].type_, "Option<Vec<EndpointRelabelings>>");

        let rel = &structs[1];
        assert_eq!(rel.name, "EndpointRelabelings");
        assert_eq!(rel.is_enum, false);
        assert_eq!(&rel.members[0].name, "action");
        assert_eq!(&rel.members[0].type_, "Option<EndpointRelabelingsAction>");
        // TODO: verify rel.members[0].field_annot uses correct default

        // action enum member
        let act = &structs[2];
        assert_eq!(act.name, "EndpointRelabelingsAction");
        assert_eq!(act.is_enum, true);

        // should have enum members:
        assert_eq!(&act.members[0].name, "replace");
        assert_eq!(&act.members[0].type_, "");
        assert_eq!(&act.members[1].name, "keep");
        assert_eq!(&act.members[1].type_, "");
        assert_eq!(&act.members[2].name, "drop");
        assert_eq!(&act.members[2].type_, "");
        assert_eq!(&act.members[3].name, "hashmod");
        assert_eq!(&act.members[3].type_, "");
    }

    #[test]
    #[ignore] // oneof support not done
    fn enum_oneof() {
        init();
        let schema_str = r#"
    description: "Auto-generated derived type for ServerSpec via `CustomResource`"
    properties:
      spec:
        properties:
          podSelector:
            oneOf:
              - required:
                  - matchExpressions
              - required:
                  - matchLabels
            properties:
              matchExpressions:
                items:
                  properties:
                    key:
                      type: string
                    operator:
                      enum:
                        - In
                        - NotIn
                        - Exists
                        - DoesNotExists
                      type: string
                    values:
                      items:
                        type: string
                      nullable: true
                      type: array
                  required:
                    - key
                    - operator
                  type: object
                type: array
              matchLabels:
                additionalProperties:
                  type: string
                type: object
            type: object
        required:
          - podSelector
        type: object
    required:
      - spec
    title: Server
    type: object"#;

        let schema: JSONSchemaProps = serde_yaml::from_str(schema_str).unwrap();
        let structs = analyze(schema, "ServerSpec").unwrap().0;
        println!("got {:?}", structs);
        let root = &structs[0];
        assert_eq!(root.name, "ServerSpec");
        assert_eq!(root.level, 0);

        // should have a required selector
        let member = &root.members[0];
        assert_eq!(member.name, "pod_selector");
        assert_eq!(member.type_, "ServerPodSelector");

        // and this should be an enum
        let ps = &structs[1]; // TODO: encode as struct?
        assert_eq!(ps.name, "ServerPodSelector");
        assert_eq!(ps.level, 1);

        // should have enum members: TODO: encode inner type as type_?
        assert_eq!(&ps.members[0].name, "MatchExpressions");
        assert_eq!(&ps.members[0].type_, "Vec<ServerPodSelectorMatchExpressions");
        assert_eq!(&ps.members[1].name, "MatchLabels");
        assert_eq!(&ps.members[1].type_, "BTreeMap<String, String>");

        // should have the inner struct match expressions
        let me = &structs[2];
        assert_eq!(me.name, "ServerPodSelectorMatchExpressions");
        assert_eq!(me.level, 2);

        // which should have 3 members
        assert_eq!(&me.members[0].name, "key");
        assert_eq!(&me.members[0].type_, "String");
        assert_eq!(&me.members[1].name, "operator");
        assert_eq!(&me.members[1].type_, "ServerPodSelectorMatchExpressionsOperator");
        assert_eq!(&me.members[2].name, "values");
        assert_eq!(&me.members[2].type_, " Option<Vec<String>>");

        // last struct being the innermost enum operator:
        let op = &structs[3];
        assert_eq!(op.name, "ServerPodSelectorMatchExpressionsOperator");
        assert_eq!(op.level, 3);

        // with enum members:
        assert_eq!(&op.members[0].name, "In");
        assert_eq!(&op.members[1].name, "In");
        assert_eq!(&op.members[2].name, "In");
        assert_eq!(&op.members[3].name, "In");
    }

    #[test]
    fn service_monitor_params() {
        init();
        let schema_str = r#"
        properties:
          endpoints:
            items:
              description: Endpoint defines a scrapeable endpoint serving Prometheus
                metrics.
              properties:
                params:
                  additionalProperties:
                    items:
                      type: string
                    type: array
                  description: Optional HTTP URL parameters
                  type: object
              type: object
            type: array
        required:
        - endpoints
        type: object
"#;
        let schema: JSONSchemaProps = serde_yaml::from_str(schema_str).unwrap();
        let structs = analyze(schema, "ServiceMonitor").unwrap().0;
        println!("got {:?}", structs);
        let root = &structs[0];
        assert_eq!(root.name, "ServiceMonitor");
        assert_eq!(root.level, 0);

        // should have a required endpoints member
        let member = &root.members[0];
        assert_eq!(member.name, "endpoints");
        assert_eq!(member.type_, "Vec<ServiceMonitorEndpoints>");

        // Should have a endpoints struct:
        let eps = &structs[1];
        assert_eq!(eps.name, "ServiceMonitorEndpoints");
        assert_eq!(eps.level, 1);
        // should have an params member:
        let member = &eps.members[0];
        assert_eq!(member.name, "params");
        assert_eq!(member.type_, "Option<BTreeMap<String, String>>");
    }

    #[test]
    fn integer_handling_in_maps() {
        init();
        // via https://istio.io/latest/docs/reference/config/networking/destination-rule/
        // distribute:
        // - from: us-west/zone1/*
        //   to:
        //     "us-west/zone1/*": 80
        //     "us-west/zone2/*": 20
        // - from: us-west/zone2/*
        //   to:
        //     "us-west/zone1/*": 20
        //     "us-west/zone2/*": 80

        // i.e. distribute is an array of {from: String, to: BTreeMap<String, Integer>}
        // with the correct integer type

        // the schema is found in destinationrule-crd.yaml with this excerpt:
        let schema_str = r#"
        properties:
          distribute:
            description: 'Optional: only one of distribute, failover
              or failoverPriority can be set.'
            items:
              properties:
                from:
                  description: Originating locality, '/' separated
                  type: string
                to:
                  additionalProperties:
                    type: integer
                    format: int32
                  description: Map of upstream localities to traffic
                    distribution weights.
                  type: object
              type: object
            type: array
        type: object
"#;
        let schema: JSONSchemaProps = serde_yaml::from_str(schema_str).unwrap();

        //println!("schema: {}", serde_json::to_string_pretty(&schema).unwrap());
        let structs = analyze(schema, "DestinationRule").unwrap().0;
        //println!("{:#?}", structs);

        // this should produce the root struct struct
        let root = &structs[0];
        assert_eq!(root.name, "DestinationRule");
        // which contains the distribute member:
        let distmember = &root.members[0];
        assert_eq!(distmember.name, "distribute");
        assert_eq!(distmember.type_, "Option<Vec<DestinationRuleDistribute>>");
        // which references the map type with {from,to} so find that struct:
        let ruledist = &structs[1];
        assert_eq!(ruledist.name, "DestinationRuleDistribute");
        // and has from and to members
        let from = &ruledist.members[0];
        let to = &ruledist.members[1];
        assert_eq!(from.name, "from");
        assert_eq!(to.name, "to");
        assert_eq!(from.type_, "Option<String>");
        assert_eq!(to.type_, "Option<BTreeMap<String, i32>>");
    }

    #[test]
    #[ignore] // currently do not handle top level enums, and this has an integration test
    fn top_level_enum_with_integers() {
        init();
        let schema_str = r#"
        default: 302
        enum:
        - 301
        - 302
        type: integer
        "#;
        let schema: JSONSchemaProps = serde_yaml::from_str(schema_str).unwrap();
        println!("got schema {}", serde_yaml::to_string(&schema).unwrap());
        let structs = analyze(schema, "StatusCode").unwrap().0;
        println!("got {:?}", structs);
        let root = &structs[0];
        assert_eq!(root.name, "StatusCode");
        assert_eq!(root.level, 0);
        assert_eq!(root.is_enum, true);
        assert_eq!(&root.members[0].name, "301");
        assert_eq!(&root.members[0].name, "302");
        assert_eq!(&root.members[0].type_, "");
    }

    #[test]
    fn array_of_preserve_unknown_objects() {
        init();
        // example from flux kustomization crd
        let schema_str = r#"
        properties:
          patchesStrategicMerge:
            description: Strategic merge patches, defined as inline YAML objects.
            items:
              x-kubernetes-preserve-unknown-fields: true
            type: array
        type: object
        "#;

        let schema: JSONSchemaProps = serde_yaml::from_str(schema_str).unwrap();
        let structs = analyze(schema, "KustomizationSpec").unwrap().0;
        println!("got {:?}", structs);
        let root = &structs[0];
        assert_eq!(root.name, "KustomizationSpec");
        assert_eq!(root.level, 0);
        assert_eq!(root.is_enum, false);
        assert_eq!(&root.members[0].name, "patchesStrategicMerge");
        assert_eq!(
            &root.members[0].type_,
            "Option<Vec<HashMap<String, serde_json::Value>>>"
        );
    }

    #[test]
    fn nested_properties_in_additional_properties() {
        init();
        // example from flux kustomization crd
        let schema_str = r#"
        properties:
          jwtTokensByRole:
            additionalProperties:
              description: JWTTokens represents a list of JWT tokens
              properties:
                items:
                  items:
                    properties:
                      exp:
                        format: int64
                        type: integer
                      iat:
                        format: int64
                        type: integer
                      id:
                        type: string
                    required:
                    - iat
                    type: object
                  type: array
              type: object
            type: object
        type: object"#;
        let schema: JSONSchemaProps = serde_yaml::from_str(schema_str).unwrap();
        let structs = analyze(schema, "AppProjectStatus").unwrap().0;
        println!("got {:?}", structs);
        let root = &structs[0];
        assert_eq!(root.name, "AppProjectStatus");
        assert_eq!(root.level, 0);
        assert_eq!(root.is_enum, false);
        assert_eq!(&root.members[0].name, "jwtTokensByRole");
        assert_eq!(
            &root.members[0].type_,
            "Option<BTreeMap<String, AppProjectStatusJwtTokensByRole>>"
        );
        let role = &structs[1];
        assert_eq!(role.level, 1);
        assert_eq!(role.name, "AppProjectStatusJwtTokensByRole");
        assert_eq!(&role.members[0].name, "items");
        let items = &structs[2];
        assert_eq!(items.level, 2);
        assert_eq!(items.name, "AppProjectStatusJwtTokensByRoleItems");
        assert_eq!(&items.members[0].name, "exp");
        assert_eq!(&items.members[1].name, "iat");
        assert_eq!(&items.members[2].name, "id");
    }

    #[test]
    fn underscore_to_camel_case() {
        init();
        let schema_str = r#"
        properties:
          validations_info:
            type: object
        type: object
"#;
        let schema: JSONSchemaProps = serde_yaml::from_str(schema_str).unwrap();

        let structs = analyze(schema, "Agent").unwrap().0;

        let root = &structs[0];
        assert_eq!(root.name, "Agent");
        assert_eq!(root.level, 0);

        let map = &root.members[0];
        assert_eq!(map.name, "validations_info");
        assert_eq!(map.type_, "Option<AgentValidationsInfo>");

        let other = &structs[1];
        assert_eq!(other.name, "AgentValidationsInfo");
        assert_eq!(other.level, 1);
    }

    #[test]
    fn skipped_type_as_map_nested_in_array() {
        init();
        let schema_str = r#"
properties:
  records:
    items:
      additionalProperties:
        type: string
      type: object
    type: array
type: object
"#;
        let schema: JSONSchemaProps = serde_yaml::from_str(schema_str).unwrap();

        let structs = analyze(schema, "Geoip").unwrap().0;

        assert_eq!(structs.len(), 1);
        assert_eq!(structs[0].members.len(), 1);
        assert_eq!(structs[0].members[0].name, "records");
        assert_eq!(
            structs[0].members[0].type_,
            "Option<Vec<BTreeMap<String, String>>>"
        );
    }
}