Skip to content

Commit

Permalink
[v0.4] Fix schemas leak (#589)
Browse files Browse the repository at this point in the history
* remove s.schemas in doRemoveSchema

* add tests for addSchema

---------

Co-authored-by: joshmeranda <[email protected]>
  • Loading branch information
joshmeranda and joshmeranda authored Jan 14, 2025
1 parent 9176ea6 commit bf01b4d
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
5 changes: 5 additions & 0 deletions types/schemas.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"fmt"
"reflect"
"slices"
"strings"
"sync"

Expand Down Expand Up @@ -84,6 +85,10 @@ func (s *Schemas) doRemoveSchema(schema Schema) *Schemas {
s.removeEmbed(&schema)
}

s.schemas = slices.DeleteFunc(s.schemas, func(candidate *Schema) bool {
return candidate.ID == schema.ID
})

return s
}

Expand Down
76 changes: 76 additions & 0 deletions types/schemas_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package types

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestSchemas(t *testing.T) {
version := APIVersion{
Group: "meta.cattle.io",
Version: "v1",
Path: "/shire",
}

s := NewSchemas().
AddSchema(Schema{
ID: "baggins",
PluralName: "bagginses",
Version: version,
CollectionMethods: []string{},
ResourceMethods: []string{},
ResourceFields: map[string]Field{},
}).
AddSchema(Schema{
ID: "hobbit",
PluralName: "hobbits",
Embed: true,
EmbedType: "baggins",
Version: version,
CollectionMethods: []string{},
ResourceMethods: []string{},
ResourceFields: map[string]Field{
"breakfasts": {Type: "int"},
"name": {Type: "string"},
},
})

expected := []*Schema{
{
ID: "hobbit",
PluralName: "hobbits",
Embed: true,
EmbedType: "baggins",
Version: version,
CollectionMethods: []string{},
ResourceMethods: []string{},
ResourceFields: map[string]Field{
"breakfasts": {Type: "int"},
"name": {Type: "string"},
},
CodeName: "Hobbit",
CodeNamePlural: "Hobbits",
BaseType: "hobbit",
Type: "/meta/schemas/schema",
},
{
ID: "baggins",
PluralName: "bagginses",
Version: version,
CollectionMethods: []string{},
ResourceMethods: []string{},
ResourceFields: map[string]Field{
"breakfasts": {Type: "int"},
"name": {Type: "string"},
},
CodeName: "Baggins",
CodeNamePlural: "Bagginses",
BaseType: "baggins",
Type: "/meta/schemas/schema",
},
}
actual := s.Schemas()

assert.ElementsMatch(t, expected, actual)
}

0 comments on commit bf01b4d

Please sign in to comment.