Skip to content

Commit

Permalink
fix: generating crd validation code fails when API code comments cont…
Browse files Browse the repository at this point in the history
…ain backticks

Signed-off-by: janeczku <[email protected]>
  • Loading branch information
janeczku committed Jun 10, 2022
1 parent cdc072a commit d3c3d19
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions tools/crd-validation-generator/validation-generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
}
Expand All @@ -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
}

0 comments on commit d3c3d19

Please sign in to comment.