fix: generating crd validation code fails when API code comments contain backticks
@@ -68,6 +68,7 @@ func generateGoFile(outputDir string, validations map[string]*extv1.CustomResour
for _, crdname := range crds {
crd := validations[crdname]
+ crd.OpenAPIV3Schema = sanitizeSchema(crd.OpenAPIV3Schema)
b, _ := yaml.Marshal(crd)
file.WriteString(fmt.Sprintf(variable, crdname, string(b)))
}
@@ -89,3 +90,33 @@ func getValidation(filename string) (string, *extv1.CustomResourceValidation) {
}
return crd.Spec.Names.Singular, crd.Spec.Versions[0].Schema
}
+
+// sanitizeSchema traverses the given JSON-Schema object and replaces all occurrences of the
+// backtick (`) character in the (sub-)schema Description fields with single quote characters
+func sanitizeSchema(inSchema *extv1.JSONSchemaProps) *extv1.JSONSchemaProps {
+ schema := inSchema.DeepCopy()
+ if schema.Description != "" {
+ schema.Description = strings.ReplaceAll(schema.Description, "`", "'")
+ }
+
+ // Traverse Items
+ if schema.Items != nil {
+ if schema.Items.Schema != nil {
+ schema.Items.Schema = sanitizeSchema(schema.Items.Schema)
+ }
+ if len(schema.Items.JSONSchemas) > 0 {
+ sanitizedProps := make([]extv1.JSONSchemaProps, 0, len(schema.Items.JSONSchemas))
+ for _, schema := range schema.Items.JSONSchemas {
+ sanitizedProps = append(sanitizedProps, *sanitizeSchema(&schema))
+ }
+ schema.Items.JSONSchemas = sanitizedProps
+ }
+ }
+
+ // Traverse Properties
+ for name, prop := range schema.Properties {
+ schema.Properties[name] = *sanitizeSchema(&prop)
+ }
+
+ return schema
+}
