@@ -11,61 +11,6 @@ import (
1111 "github.com/kong/kong-operator/v2/crd-from-oas/pkg/parser"
1212)
1313
14- func TestObjectRefTypeName (t * testing.T ) {
15- tests := []struct {
16- name string
17- commonTypes * config.CommonTypesConfig
18- want string
19- }{
20- {
21- name : "nil commonTypes returns ObjectRef" ,
22- commonTypes : nil ,
23- want : "ObjectRef" ,
24- },
25- {
26- name : "generate true returns ObjectRef" ,
27- commonTypes : & config.CommonTypesConfig {
28- ObjectRef : & config.ObjectRefConfig {
29- Generate : new (true ),
30- },
31- },
32- want : "ObjectRef" ,
33- },
34- {
35- name : "import with alias returns qualified name" ,
36- commonTypes : & config.CommonTypesConfig {
37- ObjectRef : & config.ObjectRefConfig {
38- Import : & config.ImportConfig {
39- Path : "github.com/kong/kong-operator/v2/api/common/v1alpha1" ,
40- Alias : "commonv1alpha1" ,
41- },
42- },
43- },
44- want : "commonv1alpha1.ObjectRef" ,
45- },
46- {
47- name : "import without alias uses last path segment" ,
48- commonTypes : & config.CommonTypesConfig {
49- ObjectRef : & config.ObjectRefConfig {
50- Import : & config.ImportConfig {
51- Path : "github.com/kong/kong-operator/v2/api/common/v1alpha1" ,
52- },
53- },
54- },
55- want : "v1alpha1.ObjectRef" ,
56- },
57- }
58-
59- for _ , tc := range tests {
60- t .Run (tc .name , func (t * testing.T ) {
61- g := NewGenerator (Config {
62- CommonTypes : tc .commonTypes ,
63- })
64- assert .Equal (t , tc .want , g .objectRefTypeName ())
65- })
66- }
67- }
68-
6914func TestGoType_ObjectRef (t * testing.T ) {
7015 t .Run ("without import uses ObjectRef" , func (t * testing.T ) {
7116 g := NewGenerator (Config {})
@@ -809,35 +754,6 @@ func TestGenerateSDKOps_NormalizesBooleanFields(t *testing.T) {
809754 assert .NotContains (t , content , "if err := normalizeSDKOpsBoolFields(payload); err != nil {\n \t \t return nil, fmt.Errorf(\" failed to normalize PortalAPISpec for CreatePortal: %w\" , err)" )
810755}
811756
812- func TestGenerateSDKOpsTest_AssertsNormalizedPayload (t * testing.T ) {
813- g := NewGenerator (Config {APIVersion : "v1alpha1" })
814- schema := & parser.Schema {
815- Properties : []* parser.Property {
816- {
817- Name : "name" ,
818- Type : "string" ,
819- },
820- {
821- Name : "rbac_enabled" ,
822- Type : "boolean" ,
823- },
824- },
825- }
826- opsConfig := & config.EntityOpsConfig {
827- Ops : map [string ]* config.OpConfig {
828- "create" : {
829- Path : "github.com/Kong/sdk-konnect-go/models/components.CreatePortal" ,
830- },
831- },
832- }
833-
834- content , err := g .generateSDKOpsTest ("Portal" , schema , opsConfig )
835- require .NoError (t , err )
836- assert .Contains (t , content , `RBACEnabled: "Enabled"` )
837- assert .Contains (t , content , `require.Equal(t, true, payload["rbac_enabled"])` )
838- assert .Contains (t , content , `require.Equal(t, "test-value", payload["name"])` )
839- }
840-
841757func TestParseSDKTypePath (t * testing.T ) {
842758 tests := []struct {
843759 name string
@@ -893,3 +809,86 @@ func TestParseSDKTypePath(t *testing.T) {
893809 })
894810 }
895811}
812+
813+ func TestGenerateSchemaTypes_MapWithValueTypes (t * testing.T ) {
814+ g := NewGenerator (Config {
815+ APIVersion : "v1alpha1" ,
816+ })
817+
818+ parsed := & parser.ParsedSpec {
819+ Schemas : map [string ]* parser.Schema {
820+ "Labels" : {
821+ Name : "Labels" ,
822+ Description : "Labels store metadata." ,
823+ Type : "object" ,
824+ MaxProperties : func () * int64 { v := int64 (50 ); return & v }(),
825+ AdditionalProperties : & parser.Property {
826+ Type : "string" ,
827+ MinLength : func () * int64 { v := int64 (1 ); return & v }(),
828+ MaxLength : func () * int64 { v := int64 (63 ); return & v }(),
829+ Pattern : `^[a-z0-9A-Z]+$` ,
830+ },
831+ },
832+ "LabelsUpdate" : {
833+ Name : "LabelsUpdate" ,
834+ Description : "LabelsUpdate store metadata." ,
835+ Type : "object" ,
836+ AdditionalProperties : & parser.Property {
837+ Type : "string" ,
838+ MinLength : func () * int64 { v := int64 (1 ); return & v }(),
839+ MaxLength : func () * int64 { v := int64 (63 ); return & v }(),
840+ Pattern : `^[a-z0-9A-Z]+$` ,
841+ },
842+ },
843+ },
844+ }
845+
846+ refs := map [string ]bool {
847+ "Labels" : true ,
848+ "LabelsUpdate" : true ,
849+ }
850+
851+ content := g .generateSchemaTypes (refs , parsed )
852+
853+ // Labels should generate a value type with native markers, then a map type using it
854+ assert .Contains (t , content , "type LabelsValue string" )
855+ assert .Contains (t , content , "type Labels map[string]LabelsValue" )
856+ assert .Contains (t , content , "+kubebuilder:validation:MinLength=1" )
857+ assert .Contains (t , content , "+kubebuilder:validation:MaxLength=63" )
858+ assert .Contains (t , content , "+kubebuilder:validation:Pattern=`^[a-z0-9A-Z]+$`" )
859+
860+ // LabelsUpdate should also generate a value type
861+ assert .Contains (t , content , "type LabelsUpdateValue string" )
862+ assert .Contains (t , content , "type LabelsUpdate map[string]LabelsUpdateValue" )
863+
864+ // No CEL XValidation rules or MaxProperties on the type (goes on the field)
865+ assert .NotContains (t , content , "XValidation" )
866+ assert .NotContains (t , content , "MaxProperties" )
867+ }
868+
869+ func TestGenerateSchemaTypes_NoValueTypeForNonMapTypes (t * testing.T ) {
870+ g := NewGenerator (Config {
871+ APIVersion : "v1alpha1" ,
872+ })
873+
874+ parsed := & parser.ParsedSpec {
875+ Schemas : map [string ]* parser.Schema {
876+ "GatewayName" : {
877+ Name : "GatewayName" ,
878+ Description : "The name of the Gateway." ,
879+ Type : "string" ,
880+ },
881+ },
882+ }
883+
884+ refs := map [string ]bool {
885+ "GatewayName" : true ,
886+ }
887+
888+ content := g .generateSchemaTypes (refs , parsed )
889+
890+ assert .Contains (t , content , "type GatewayName string" )
891+ assert .NotContains (t , content , "Value" )
892+ assert .NotContains (t , content , "XValidation" )
893+ assert .NotContains (t , content , "MaxProperties" )
894+ }
0 commit comments