Skip to content

Commit

Permalink
Fix intstr.IntOrString to be number or string instead of object (#453) (
Browse files Browse the repository at this point in the history
#457)

* Fix intstr.IntOrString to be number or string instead of object (#453)

* add intstr.IntOrStr custom handling and test

* whitespace changes from codegen

* add changelog

* more autogenerated whitespace changes

* remove more whitespaces

* reset watcher.go

* Move changelog to v0.30.2

---------

Co-authored-by: Nik Matthiopoulos <nikolasmatt@gmail.com>
  • Loading branch information
t-edris and nikolasmatt committed Jun 2, 2023
1 parent 8c0d770 commit cb41245
Show file tree
Hide file tree
Showing 4 changed files with 230 additions and 336 deletions.
5 changes: 5 additions & 0 deletions changelog/v0.30.2/fix-intstr-handling.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
changelog:
- type: FIX
issueLink: https://github.com/solo-io/gloo-mesh-enterprise/issues/9194
description: intstr.IntOrString fields now output the correct schema instead of an object
skipCI: false
10 changes: 10 additions & 0 deletions codegen/render/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/utils/strings/slices"

"github.com/BurntSushi/toml"
Expand Down Expand Up @@ -709,6 +710,15 @@ func createCustomTypeMapper(values values.UserHelmValues) customTypeMapper {
}
},

reflect.TypeOf(intstr.IntOrString{}): func(t reflect.Type, defaultSchema *jsonschema.Schema) *jsonschema.Schema {
return &jsonschema.Schema{
AnyOf: []*jsonschema.Schema{
{Type: "string"},
{Type: "number"},
},
}
},

reflect.TypeOf(v1.SecurityContext{}): func(t reflect.Type, defaultSchema *jsonschema.Schema) *jsonschema.Schema {
return &jsonschema.Schema{
AnyOf: []*jsonschema.Schema{
Expand Down
25 changes: 25 additions & 0 deletions codegen/render/funcs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"

"github.com/solo-io/skv2/codegen/model/values"
"github.com/solo-io/skv2/codegen/render"
Expand Down Expand Up @@ -648,6 +649,30 @@ var _ = Describe("toJSONSchema", func() {
Expect(result).To(Equal(expected))
})

It("maps intstr.IntOrString as either a string or number", func() {
type Type1 struct {
Field1 intstr.IntOrString
}
result := render.ToJSONSchema(values.UserHelmValues{CustomValues: &Type1{}})
expected := prepareExpected(`
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"properties": {
"Field1": {
"anyOf": [
{
"type": "string"
},
{
"type": "number"
}
]
}
}
}`)
Expect(result).To(Equal(expected))
})

It("maps security context as something that can be mapped to a boolean", func() {
type Type1 struct {
Field1 *v1.SecurityContext
Expand Down
Loading

0 comments on commit cb41245

Please sign in to comment.