Skip to content

Commit a461dc4

Browse files
committed
fix(api): keep the name declaration off the published spec schema
The whole openAPISchema is published as the schema of spec, so x-cozystack-name showed up as a vendor extension of spec in the aggregated OpenAPI. That describes something spec does not contain. Assisted-by: LLM Signed-off-by: Timofei Larkin <[email protected]>
1 parent 3852ba8 commit a461dc4

2 files changed

Lines changed: 21 additions & 0 deletions

File tree

‎pkg/cmd/server/openapi.go‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"strings"
77

8+
appsv1alpha1 "github.com/cozystack/cozystack/pkg/apis/apps/v1alpha1"
89
"github.com/cozystack/cozystack/pkg/apiserver"
910
"github.com/cozystack/cozystack/pkg/config"
1011
sampleopenapi "github.com/cozystack/cozystack/pkg/generated/openapi"
@@ -155,6 +156,8 @@ func patchSpec(target *spec.Schema, raw string) error {
155156
if err := json.Unmarshal([]byte(raw), &custom); err != nil {
156157
return err
157158
}
159+
// The name declaration describes metadata.name, not anything inside spec.
160+
delete(custom.Extensions, appsv1alpha1.NameSchemaExtension)
158161

159162
// Neutralize any crash-prone boolean additionalProperties at any depth in
160163
// the user-supplied schema before publishing it.

‎pkg/cmd/server/openapi_test.go‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"strings"
2222
"testing"
2323

24+
appsv1alpha1 "github.com/cozystack/cozystack/pkg/apis/apps/v1alpha1"
2425
celopenapi "k8s.io/apiserver/pkg/cel/openapi"
2526
"k8s.io/kube-openapi/pkg/validation/spec"
2627
)
@@ -116,6 +117,23 @@ func TestPatchSpecKeepsObjectFormAdditionalProperties(t *testing.T) {
116117
}
117118
}
118119

120+
// TestPatchSpecLeavesNameDeclarationOffSpec keeps the published spec from
121+
// carrying a declaration about metadata.name.
122+
func TestPatchSpecLeavesNameDeclarationOffSpec(t *testing.T) {
123+
raw := `{"type":"object","x-cozystack-name":{"type":"string","maxLength":32},"x-other":true,"properties":{}}`
124+
target := newObjectContainer()
125+
if err := patchSpec(target, raw); err != nil {
126+
t.Fatalf("patchSpec: %v", err)
127+
}
128+
specSchema := target.Properties["spec"]
129+
if _, ok := specSchema.Extensions[appsv1alpha1.NameSchemaExtension]; ok {
130+
t.Errorf("published spec carries %s", appsv1alpha1.NameSchemaExtension)
131+
}
132+
if _, ok := specSchema.Extensions["x-other"]; !ok {
133+
t.Errorf("unrelated vendor extension dropped from spec")
134+
}
135+
}
136+
119137
// TestPatchedSpecDoesNotPanicVAPTypeChecker is the behavioral regression test
120138
// for #2863. SchemaDeclType is the exact entry point kube-controller-manager's
121139
// ValidatingAdmissionPolicy status controller uses to type-check a VAP against

0 commit comments

Comments
 (0)