Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/schemas/generator/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ func CRDsToJSONSchemas(crds []*extv1.CustomResourceDefinition) ([]CRDJSONSchema,
// Schema: sets additionalProperties to false on object types and rewrites
// component $ref paths to file references.
func mutateJSONSchema(s *jsonschema.Schema) *jsonschema.Schema {
if s.Type == "object" && s.AdditionalProperties == nil {
if s.Type == "object" && s.AdditionalProperties == nil && s.Properties.Len() > 0 {
s.AdditionalProperties = jsonschema.FalseSchema
}

Expand Down
28 changes: 28 additions & 0 deletions internal/schemas/generator/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,34 @@ func TestGenerateFromCRD(t *testing.T) {
}
}

func TestMutateJSONSchema(t *testing.T) {
t.Run("ObjectWithProperties", func(t *testing.T) {
s := &jsonschema.Schema{
Type: "object",
}
s.Properties = jsonschema.NewProperties()
s.Properties.Set("name", &jsonschema.Schema{Type: "string"})

mutateJSONSchema(s)

if s.AdditionalProperties != jsonschema.FalseSchema {
t.Error("expected additionalProperties to be false for object with properties")
}
})

t.Run("EmptyObject", func(t *testing.T) {
s := &jsonschema.Schema{
Type: "object",
}

mutateJSONSchema(s)

if s.AdditionalProperties != nil {
t.Error("expected additionalProperties to remain nil for empty object")
}
})
}

func TestGenerateFromOpenAPI(t *testing.T) {
inputFS := afero.NewBasePathFs(afero.FromIOFS{FS: testdataJSONFS}, "testdata")
schemaFS, err := jsonGenerator{}.GenerateFromOpenAPI(t.Context(), inputFS, nil)
Expand Down
Loading