Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
28 changes: 28 additions & 0 deletions pkg/openapi/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ func SchemaFrom(typ reflect.Type) *apiext.JSONSchemaProps {
return generateSchema(typ, true, make(typeCache))
}

// SchemaFor is a utility function for calling SchemaFrom generically without providing a reflect.Type.
func SchemaFor[T any]() *apiext.JSONSchemaProps {
return SchemaFrom(reflect.TypeFor[T]())
}

func generateSchema(typ reflect.Type, top bool, cache typeCache) *apiext.JSONSchemaProps {
type OpenAPISchemer interface {
OpenAPISchema() *apiext.JSONSchemaProps
Expand All @@ -48,6 +53,29 @@ func generateSchema(typ reflect.Type, top bool, cache typeCache) *apiext.JSONSch
return &apiext.JSONSchemaProps{Type: "string"}
}

if typ.PkgPath() == "k8s.io/apimachinery/pkg/util/intstr" && typ.Name() == "IntOrString" {
return &apiext.JSONSchemaProps{
XIntOrString: true,
AnyOf: []apiext.JSONSchemaProps{
{Type: "string"},
{Type: "integer"},
},
}
}

if typ.PkgPath() == "k8s.io/apimachinery/pkg/api/resource" && typ.Name() == "Quantity" {
return &apiext.JSONSchemaProps{
XIntOrString: true,
AnyOf: []apiext.JSONSchemaProps{
{Type: "string"},
{Type: "integer"},
},
// Pattern taken from the prometheus operator for resource quantities.
// https://github.com/prometheus-operator/prometheus-operator/blob/87ed2f1149edc971525f4e5b3b608c808bad0dc9/example/prometheus-operator-crd/monitoring.coreos.com_prometheuses.yaml#L8247-L8253
Pattern: `^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$`,
}
}

switch typ.Kind() {
case reflect.String:
return &apiext.JSONSchemaProps{Type: "string"}
Expand Down
35 changes: 27 additions & 8 deletions pkg/openapi/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"github.com/stretchr/testify/require"

apiext "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/utils/ptr"

"github.com/yokecd/yoke/pkg/apis/v1alpha1"
Expand All @@ -24,13 +26,15 @@ func TestGenerateSchema(t *testing.T) {

type S struct {
Embedded
Name string `json:"name" MinLength:"3"`
Age int `json:"age" Minimum:"18"`
Labels map[string]string `json:"labels,omitempty"`
Active bool `json:"active"`
Choice string `json:"choice" Enum:"yes,no,toaster"`
Rule string `json:"rule" XValidations:"[{\"rule\": \"has(self)\", \"message\":\"something\"}]"`
Map map[string]any `json:"map"`
Name string `json:"name" MinLength:"3"`
Age int `json:"age" Minimum:"18"`
Labels map[string]string `json:"labels,omitempty"`
Active bool `json:"active"`
Choice string `json:"choice" Enum:"yes,no,toaster"`
Rule string `json:"rule" XValidations:"[{\"rule\": \"has(self)\", \"message\":\"something\"}]"`
Map map[string]any `json:"map"`
IntOrString intstr.IntOrString `json:"intOrString,omitzero"`
Quantity resource.Quantity `json:"quantity,omitzero"`
}

require.EqualValues(
Expand Down Expand Up @@ -79,10 +83,25 @@ func TestGenerateSchema(t *testing.T) {
Type: "object",
XPreserveUnknownFields: ptr.To(true),
},
"intOrString": {
XIntOrString: true,
AnyOf: []apiext.JSONSchemaProps{
{Type: "string"},
{Type: "integer"},
},
},
"quantity": {
XIntOrString: true,
AnyOf: []apiext.JSONSchemaProps{
{Type: "string"},
{Type: "integer"},
},
Pattern: `^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$`,
},
},
Required: []string{"name", "age", "active", "choice", "rule", "map"},
},
openapi.SchemaFrom(reflect.TypeOf(S{})),
openapi.SchemaFrom(reflect.TypeFor[S]()),
)
}

Expand Down
Loading