Skip to content

Commit

Permalink
💈 support anyOf in json schema (#687)
Browse files Browse the repository at this point in the history
  • Loading branch information
petar-cvit authored Nov 21, 2024
1 parent c5af027 commit 782b7a8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
27 changes: 27 additions & 0 deletions cyclops-ctrl/internal/mapper/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import (
)

func HelmSchemaToFields(name string, schema helm.Property, defs map[string]helm.Property, dependencies []*models.Template) models.Field {
if shouldResolvePropertyComposition(schema) {
return HelmSchemaToFields(name, resolvePropertyComposition(schema), defs, dependencies)
}

if schema.Type == "array" {
return models.Field{
Name: name,
Expand Down Expand Up @@ -211,3 +215,26 @@ func resolveJSONSchemaRef(defs map[string]helm.Property, ref []string) helm.Prop

return resolveJSONSchemaRef(def.Properties, ref[1:])
}

func shouldResolvePropertyComposition(schema helm.Property) bool {
return len(schema.AnyOf) != 0
}

func resolvePropertyComposition(schema helm.Property) helm.Property {
if len(schema.AnyOf) == 0 {
return schema
}

// go over all options of anyOf and if there is a boolean version, return it.
// If a field can be represented with a boolean we should use it since it is
// the easiest way to input a correct value.
//
// If there are multiple boolean anyOfs, return the first one.
for _, p := range schema.AnyOf {
if p.Type == "boolean" {
return p
}
}

return schema.AnyOf[0]
}
3 changes: 3 additions & 0 deletions cyclops-ctrl/internal/models/helm/helmschema.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ type Property struct {
MinLength *int `json:"minLength"`
MaxLength *int `json:"maxLength"`
Pattern *string `json:"pattern"`

// schema compositions
AnyOf []Property `json:"anyOf"`
}

type PropertyType string
Expand Down

0 comments on commit 782b7a8

Please sign in to comment.