Skip to content

Commit 23a7281

Browse files
committed
[cozystack-api] Disable startegic-json-patch support
Signed-off-by: Andrei Kvapil <[email protected]>
1 parent f32c642 commit 23a7281

1 file changed

Lines changed: 31 additions & 11 deletions

File tree

‎pkg/cmd/server/openapi.go‎

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
const (
1717
baseRef = "com.github.cozystack.cozystack.pkg.apis.apps.v1alpha1.Application"
1818
baseListRef = baseRef + "List"
19+
smp = "application/strategic-merge-patch+json"
1920
)
2021

2122
func deepCopySchema(in *spec.Schema) *spec.Schema {
@@ -101,21 +102,20 @@ func patchSpec(target *spec.Schema, raw string) error {
101102
func buildPostProcessV3(kindSchemas map[string]string) func(*spec3.OpenAPI) (*spec3.OpenAPI, error) {
102103

103104
return func(doc *spec3.OpenAPI) (*spec3.OpenAPI, error) {
105+
106+
// Replace the basic "Application" schema with the user-supplied kinds.
104107
if doc.Components == nil {
105108
doc.Components = &spec3.Components{}
106109
}
107110
if doc.Components.Schemas == nil {
108111
doc.Components.Schemas = map[string]*spec.Schema{}
109112
}
110-
111113
base, ok := doc.Components.Schemas[baseRef]
112114
if !ok {
113115
return doc, fmt.Errorf("base schema %q not found", baseRef)
114116
}
115-
116117
for kind, raw := range kindSchemas {
117118
ref := fmt.Sprintf("%s.%s", "com.github.cozystack.cozystack.pkg.apis.apps.v1alpha1", kind)
118-
119119
s := doc.Components.Schemas[ref]
120120
if s == nil { // first time – clone "Application"
121121
s = deepCopySchema(base)
@@ -128,7 +128,6 @@ func buildPostProcessV3(kindSchemas map[string]string) func(*spec3.OpenAPI) (*sp
128128
}
129129
doc.Components.Schemas[ref] = s
130130
}
131-
132131
container := findSpecContainer(s)
133132
if container == nil { // fallback: use the root
134133
container = s
@@ -137,9 +136,19 @@ func buildPostProcessV3(kindSchemas map[string]string) func(*spec3.OpenAPI) (*sp
137136
return nil, fmt.Errorf("kind %s: %w", kind, err)
138137
}
139138
}
140-
141139
delete(doc.Components.Schemas, baseRef)
142140
delete(doc.Components.Schemas, baseListRef)
141+
142+
// Disable strategic-merge-patch+json support in all PATCH operations
143+
for p, pi := range doc.Paths.Paths {
144+
if pi == nil || pi.Patch == nil || pi.Patch.RequestBody == nil {
145+
continue
146+
}
147+
delete(pi.Patch.RequestBody.Content, smp)
148+
149+
doc.Paths.Paths[p] = pi
150+
}
151+
143152
return doc, nil
144153
}
145154
}
@@ -150,15 +159,15 @@ func buildPostProcessV3(kindSchemas map[string]string) func(*spec3.OpenAPI) (*sp
150159
func buildPostProcessV2(kindSchemas map[string]string) func(*spec.Swagger) (*spec.Swagger, error) {
151160

152161
return func(sw *spec.Swagger) (*spec.Swagger, error) {
162+
163+
// Replace the basic "Application" schema with the user-supplied kinds.
153164
defs := sw.Definitions
154165
base, ok := defs[baseRef]
155166
if !ok {
156167
return sw, fmt.Errorf("base schema %q not found", baseRef)
157168
}
158-
159169
for kind, raw := range kindSchemas {
160170
ref := fmt.Sprintf("%s.%s", "com.github.cozystack.cozystack.pkg.apis.apps.v1alpha1", kind)
161-
162171
s := deepCopySchema(&base)
163172
s.Extensions = map[string]interface{}{
164173
"x-kubernetes-group-version-kind": []interface{}{
@@ -167,13 +176,10 @@ func buildPostProcessV2(kindSchemas map[string]string) func(*spec.Swagger) (*spe
167176
},
168177
},
169178
}
170-
171179
if err := patchSpec(s, raw); err != nil {
172180
return nil, fmt.Errorf("kind %s: %w", kind, err)
173181
}
174-
175182
defs[ref] = *s
176-
177183
// clone the List variant
178184
listName := ref + "List"
179185
listSrc := defs[baseListRef]
@@ -193,9 +199,23 @@ func buildPostProcessV2(kindSchemas map[string]string) func(*spec.Swagger) (*spe
193199
}
194200
defs[listName] = *listCopy
195201
}
196-
197202
delete(defs, baseRef)
198203
delete(defs, baseListRef)
204+
205+
// Disable strategic-merge-patch+json support in all PATCH operations
206+
for p, op := range sw.Paths.Paths {
207+
if op.Patch != nil && len(op.Patch.Consumes) > 0 {
208+
var out []string
209+
for _, c := range op.Patch.Consumes {
210+
if c != smp {
211+
out = append(out, c)
212+
}
213+
}
214+
op.Patch.Consumes = out
215+
sw.Paths.Paths[p] = op
216+
}
217+
}
218+
199219
return sw, nil
200220
}
201221
}

0 commit comments

Comments
 (0)