|
7 | 7 | apischema "github.com/openmfp/kubernetes-graphql-gateway/listener/pkg/apischema"
|
8 | 8 | apimocks "github.com/openmfp/kubernetes-graphql-gateway/listener/pkg/apischema/mocks"
|
9 | 9 | "github.com/stretchr/testify/assert"
|
| 10 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
10 | 11 | "k8s.io/client-go/openapi"
|
11 | 12 | "k8s.io/kube-openapi/pkg/validation/spec"
|
12 | 13 | )
|
@@ -82,3 +83,47 @@ func Test_build_kind_registry_lowercases_keys_and_picks_first(t *testing.T) {
|
82 | 83 | // ensure it referenced the first group
|
83 | 84 | assert.Contains(t, added.Ref.String(), "#/definitions/a.example.v1.Thing")
|
84 | 85 | }
|
| 86 | + |
| 87 | +func Test_preferred_version_takes_priority_over_fallback(t *testing.T) { |
| 88 | + mock := apimocks.NewMockClient(t) |
| 89 | + mock.EXPECT().Paths().Return(map[string]openapi.GroupVersion{}, nil) |
| 90 | + b := apischema.NewSchemaBuilder(mock, nil, testlogger.New().Logger) |
| 91 | + |
| 92 | + // Multiple schemas with same Kind - a.example would win alphabetically, |
| 93 | + // but we'll set z.last as preferred to verify it takes priority |
| 94 | + childA := schemaWithGVK("a.example", "v1", "Child") |
| 95 | + childB := schemaWithGVK("b.example", "v1", "Child") |
| 96 | + childZ := schemaWithGVK("z.last", "v1", "Child") // would be last alphabetically |
| 97 | + |
| 98 | + b.SetSchemas(map[string]*spec.Schema{ |
| 99 | + "a.example.v1.Child": childA, |
| 100 | + "b.example.v1.Child": childB, |
| 101 | + "z.last.v1.Child": childZ, |
| 102 | + }) |
| 103 | + |
| 104 | + // Set z.last as preferred (even though it would be last alphabetically) |
| 105 | + b.WithPreferredVersions([]*metav1.APIResourceList{ |
| 106 | + { |
| 107 | + GroupVersion: "z.last/v1", |
| 108 | + APIResources: []metav1.APIResource{ |
| 109 | + {Kind: "Child"}, |
| 110 | + }, |
| 111 | + }, |
| 112 | + }) |
| 113 | + |
| 114 | + b.WithRelationships() |
| 115 | + |
| 116 | + // Add a parent schema that references childRef |
| 117 | + parentSchema := &spec.Schema{SchemaProps: spec.SchemaProps{Properties: map[string]spec.Schema{ |
| 118 | + "childRef": {SchemaProps: spec.SchemaProps{Type: spec.StringOrArray{"object"}}}, |
| 119 | + }}} |
| 120 | + b.GetSchemas()["x.v1.Parent"] = parentSchema |
| 121 | + |
| 122 | + b.WithRelationships() |
| 123 | + added, ok := b.GetSchemas()["x.v1.Parent"].Properties["child"] |
| 124 | + assert.True(t, ok, "expected relationship field 'child'") |
| 125 | + |
| 126 | + // Should reference z.last because it's the preferred version, not a.example (alphabetical first) |
| 127 | + assert.Contains(t, added.Ref.String(), "#/definitions/z.last.v1.Child", |
| 128 | + "expected preferred version z.last to be chosen over alphabetically first a.example") |
| 129 | +} |
0 commit comments