-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: added tests and handle required recursively
- Loading branch information
1 parent
ef52087
commit 1b79c7c
Showing
4 changed files
with
156 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package crd | ||
|
||
import ( | ||
v1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" | ||
) | ||
|
||
func ShallowJsonSchema(schema *v1.JSONSchemaProps, separator string) *v1.JSONSchemaProps { | ||
clonedSchema := schema.DeepCopy() | ||
shallowProperties(clonedSchema, "", separator, clonedSchema) | ||
|
||
if schema.Type == "object" { | ||
return &v1.JSONSchemaProps{ | ||
Type: "object", | ||
Properties: clonedSchema.Properties, | ||
Required: shallowRequired(schema, "", separator), | ||
} | ||
} | ||
|
||
return schema | ||
} | ||
|
||
func shallowProperties(schema *v1.JSONSchemaProps, parent string, seperator string, originalSchema *v1.JSONSchemaProps) { | ||
for k, v := range schema.Properties { | ||
shallowedKey := k | ||
|
||
if parent != "" { | ||
shallowedKey = parent + seperator + k | ||
} | ||
|
||
if v.Type != "object" { | ||
originalSchema.Properties[shallowedKey] = v | ||
} else { | ||
shallowProperties(&v, shallowedKey, seperator, originalSchema) | ||
delete(originalSchema.Properties, k) | ||
} | ||
} | ||
} | ||
|
||
// shallowRequired recursively traverses the JSONSchemaProps and returns a list of required fields with nested field names concatenated by the provided separator. | ||
func shallowRequired(schema *v1.JSONSchemaProps, prefix, separator string) []string { | ||
var requiredFields []string | ||
|
||
for _, field := range schema.Required { | ||
if propSchema, ok := schema.Properties[field]; ok { | ||
fullFieldName := field | ||
if prefix != "" { | ||
fullFieldName = prefix + separator + field | ||
} | ||
|
||
if propSchema.Type == "object" { | ||
// Recursively process nested objects but don't add the object field itself | ||
nestedRequiredFields := shallowRequired(&propSchema, fullFieldName, separator) | ||
requiredFields = append(requiredFields, nestedRequiredFields...) | ||
} else { | ||
// Add non-object fields to the required list | ||
requiredFields = append(requiredFields, fullFieldName) | ||
} | ||
} | ||
} | ||
|
||
return requiredFields | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package crd | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
v1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" | ||
) | ||
|
||
func TestCRD_crd_shallowNestedSchema(t *testing.T) { | ||
originalSchema := &v1.JSONSchemaProps{ | ||
Type: "object", | ||
Properties: map[string]v1.JSONSchemaProps{ | ||
"spec": { | ||
Type: "object", | ||
Properties: map[string]v1.JSONSchemaProps{ | ||
"stringProperty": { | ||
Type: "string", | ||
}, | ||
"intProperty": { | ||
Type: "integer", | ||
}, | ||
"boolProperty": { | ||
Type: "boolean", | ||
}, | ||
"nestedProperty": { | ||
Type: "object", | ||
Properties: map[string]v1.JSONSchemaProps{ | ||
"nestedStringProperty": { | ||
Type: "string", | ||
}, | ||
"nestedIntProperty": { | ||
Type: "integer", | ||
}, | ||
}, | ||
Required: []string{"nestedStringProperty"}, | ||
}, | ||
"multiNestedProperty": { | ||
Type: "object", | ||
Properties: map[string]v1.JSONSchemaProps{ | ||
"nestedObjectProperty": { | ||
Type: "object", | ||
Properties: map[string]v1.JSONSchemaProps{ | ||
"nestedStringProperty": { | ||
Type: "string", | ||
}, | ||
}, | ||
Required: []string{"nestedStringProperty"}, | ||
}, | ||
}, | ||
Required: []string{}, | ||
}, | ||
}, | ||
Required: []string{"stringProperty", "nestedProperty", "multiNestedProperty"}, | ||
}, | ||
}, | ||
Required: []string{"spec"}, | ||
} | ||
|
||
shallowedSchema := ShallowJsonSchema(originalSchema, "__") | ||
|
||
expectedSchema := &v1.JSONSchemaProps{ | ||
Type: "object", | ||
Properties: map[string]v1.JSONSchemaProps{ | ||
"spec__stringProperty": { | ||
Type: "string", | ||
}, | ||
"spec__intProperty": { | ||
Type: "integer", | ||
}, | ||
"spec__boolProperty": { | ||
Type: "boolean", | ||
}, | ||
"spec__nestedProperty__nestedStringProperty": { | ||
Type: "string", | ||
}, | ||
"spec__nestedProperty__nestedIntProperty": { | ||
Type: "integer", | ||
}, | ||
"spec__multiNestedProperty__nestedObjectProperty__nestedStringProperty": { | ||
Type: "string", | ||
}, | ||
}, | ||
Required: []string{"spec__stringProperty", "spec__nestedProperty__nestedStringProperty"}, | ||
} | ||
|
||
if reflect.DeepEqual(shallowedSchema, expectedSchema) { | ||
t.Logf("Shallowed schema is as expected") | ||
} else { | ||
t.Errorf("Shallowed schema is not as expected") | ||
} | ||
} |