|
| 1 | +/* |
| 2 | +Copyright 2026 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package crd |
| 18 | + |
| 19 | +import ( |
| 20 | + "go/ast" |
| 21 | + |
| 22 | + . "github.com/onsi/ginkgo" |
| 23 | + . "github.com/onsi/gomega" |
| 24 | + pkgstest "golang.org/x/tools/go/packages/packagestest" |
| 25 | + apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" |
| 26 | + "sigs.k8s.io/controller-tools/pkg/loader" |
| 27 | + testloader "sigs.k8s.io/controller-tools/pkg/loader/testutils" |
| 28 | + "sigs.k8s.io/controller-tools/pkg/markers" |
| 29 | +) |
| 30 | + |
| 31 | +var _ = Describe("mapToSchema", func() { |
| 32 | + Context("TextMarshaler map key validation", func() { |
| 33 | + // cleanup holds the teardown function returned by the fake package loader. |
| 34 | + // AfterEach calls it so temp directories are removed after every It block. |
| 35 | + var cleanup func() |
| 36 | + |
| 37 | + AfterEach(func() { |
| 38 | + if cleanup != nil { |
| 39 | + cleanup() |
| 40 | + cleanup = nil |
| 41 | + } |
| 42 | + }) |
| 43 | + |
| 44 | + // schemaForType loads src as a fake package and returns the schema for the |
| 45 | + // named type, together with the package so callers can inspect errors. |
| 46 | + // It sets cleanup so AfterEach can release temp files after the It block ends. |
| 47 | + schemaForType := func(src, typeName string) (*apiextensionsv1.JSONSchemaProps, *loader.Package) { |
| 48 | + const mod = "sigs.k8s.io/controller-tools/pkg/crd" |
| 49 | + pkgs, exported, err := testloader.LoadFakeRoots(pkgstest.Modules, []pkgstest.Module{{ |
| 50 | + Name: mod, |
| 51 | + Files: map[string]any{"test.go": src}, |
| 52 | + }}, mod) |
| 53 | + if exported != nil { |
| 54 | + cleanup = exported.Cleanup |
| 55 | + } |
| 56 | + Expect(err).NotTo(HaveOccurred()) |
| 57 | + Expect(pkgs).To(HaveLen(1)) |
| 58 | + |
| 59 | + pkg := pkgs[0] |
| 60 | + pkg.NeedTypesInfo() |
| 61 | + |
| 62 | + schemaCtx := newSchemaContext(pkg, nil, true, false).ForInfo(&markers.TypeInfo{}) |
| 63 | + |
| 64 | + var definedType ast.Expr |
| 65 | + for _, decl := range pkg.Syntax[0].Decls { |
| 66 | + if gd, ok := decl.(*ast.GenDecl); ok { |
| 67 | + for _, spec := range gd.Specs { |
| 68 | + if ts, ok := spec.(*ast.TypeSpec); ok && ts.Name.Name == typeName { |
| 69 | + definedType = ts.Type |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + Expect(definedType).NotTo(BeNil(), "type %q not found in fake package", typeName) |
| 75 | + return typeToSchema(schemaCtx, definedType), pkg |
| 76 | + } |
| 77 | + |
| 78 | + // src defines three map types covering the three cases below. |
| 79 | + const src = ` |
| 80 | +package crd |
| 81 | +
|
| 82 | +// valueKey implements encoding.TextMarshaler via a value receiver. |
| 83 | +// encoding/json can call MarshalText on map keys of this type because |
| 84 | +// the method is reachable without a pointer. |
| 85 | +type valueKey struct{} |
| 86 | +func (valueKey) MarshalText() ([]byte, error) { return nil, nil } |
| 87 | +
|
| 88 | +// ptrKey implements encoding.TextMarshaler via a pointer receiver only. |
| 89 | +// Map keys are not addressable, so encoding/json cannot call *ptrKey.MarshalText |
| 90 | +// and will return an error at runtime — this type must be rejected. |
| 91 | +type ptrKey struct{} |
| 92 | +func (*ptrKey) MarshalText() ([]byte, error) { return nil, nil } |
| 93 | +
|
| 94 | +// noMarshalKey is a plain struct with no TextMarshaler implementation. |
| 95 | +type noMarshalKey struct{} |
| 96 | +
|
| 97 | +type ValueKeyMap map[valueKey]string |
| 98 | +type PtrKeyMap map[ptrKey]string |
| 99 | +type NoMarshalKeyMap map[noMarshalKey]string |
| 100 | +` |
| 101 | + |
| 102 | + It("accepts a value-receiver TextMarshaler type as a map key", func() { |
| 103 | + output, pkg := schemaForType(src, "ValueKeyMap") |
| 104 | + Expect(pkg.Errors).To(BeEmpty()) |
| 105 | + Expect(output).To(Equal(&apiextensionsv1.JSONSchemaProps{ |
| 106 | + Type: "object", |
| 107 | + AdditionalProperties: &apiextensionsv1.JSONSchemaPropsOrBool{ |
| 108 | + Allows: true, |
| 109 | + Schema: &apiextensionsv1.JSONSchemaProps{Type: "string"}, |
| 110 | + }, |
| 111 | + })) |
| 112 | + }) |
| 113 | + |
| 114 | + It("rejects a pointer-receiver-only TextMarshaler type as a map key", func() { |
| 115 | + _, pkg := schemaForType(src, "PtrKeyMap") |
| 116 | + Expect(pkg.Errors).NotTo(BeEmpty()) |
| 117 | + Expect(pkg.Errors[0].Msg).To(ContainSubstring("map keys must be strings")) |
| 118 | + }) |
| 119 | + |
| 120 | + It("rejects a struct that does not implement TextMarshaler", func() { |
| 121 | + _, pkg := schemaForType(src, "NoMarshalKeyMap") |
| 122 | + Expect(pkg.Errors).NotTo(BeEmpty()) |
| 123 | + Expect(pkg.Errors[0].Msg).To(ContainSubstring("map keys must be strings")) |
| 124 | + }) |
| 125 | + }) |
| 126 | +}) |
0 commit comments