Skip to content

Commit e06f56f

Browse files
committed
Fix cozystack-api to show correct List types in openapi
Signed-off-by: Andrei Kvapil <[email protected]>
1 parent 97d006e commit e06f56f

2 files changed

Lines changed: 25 additions & 6 deletions

File tree

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
cozystackAPI:
2-
image: ghcr.io/aenix-io/cozystack/cozystack-api:v0.20.2@sha256:fd7bebabd4b8d29c5749bc454feec1ef35bf29ce60b5edebb9a550ca6dcfed49
2+
image: ghcr.io/aenix-io/cozystack/cozystack-api:latest@sha256:1eb7f0387ea01754107a4aabe72c2e1e7d2c55303dc15cfe9caa2c0739c0215e

‎pkg/cmd/server/start.go‎

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -201,26 +201,30 @@ func (o *AppsServerOptions) Config() (*apiserver.Config, error) {
201201
serverConfig.OpenAPIConfig.PostProcessSpec = func(swagger *spec.Swagger) (*spec.Swagger, error) {
202202
defs := swagger.Definitions
203203

204-
// Check basic Application definition
204+
// Verify the presence of the base Application/ApplicationList definitions
205205
appDef, exists := defs["com.github.aenix.io.cozystack.pkg.apis.apps.v1alpha1.Application"]
206206
if !exists {
207207
return swagger, fmt.Errorf("Application definition not found")
208208
}
209209

210-
// Check basic ApplicationList definition
211210
listDef, exists := defs["com.github.aenix.io.cozystack.pkg.apis.apps.v1alpha1.ApplicationList"]
212211
if !exists {
213212
return swagger, fmt.Errorf("ApplicationList definition not found")
214213
}
215214

215+
// Iterate over all registered GVKs (e.g., Bucket, Database, etc.)
216216
for _, gvk := range v1alpha1.RegisteredGVKs {
217+
// This will be something like:
218+
// "com.github.aenix.io.cozystack.pkg.apis.apps.v1alpha1.Bucket"
217219
resourceName := fmt.Sprintf("com.github.aenix.io.cozystack.pkg.apis.apps.v1alpha1.%s", gvk.Kind)
220+
221+
// 1. Create a copy of the base Application definition for the new resource
218222
newDef, err := DeepCopySchema(&appDef)
219223
if err != nil {
220224
return nil, fmt.Errorf("failed to deepcopy schema for %s: %w", gvk.Kind, err)
221225
}
222226

223-
// Fix Extensions for resource
227+
// 2. Update x-kubernetes-group-version-kind to match the new resource
224228
if newDef.Extensions == nil {
225229
newDef.Extensions = map[string]interface{}{}
226230
}
@@ -231,17 +235,20 @@ func (o *AppsServerOptions) Config() (*apiserver.Config, error) {
231235
"kind": gvk.Kind,
232236
},
233237
}
238+
239+
// 3. Save the new resource definition under the correct name
234240
defs[resourceName] = *newDef
235241
klog.V(6).Infof("PostProcessSpec: Added OpenAPI definition for %s\n", resourceName)
236242

237-
// List resource
243+
// 4. Now handle the corresponding List type (e.g., BucketList).
244+
// We'll start by copying the ApplicationList definition.
238245
listResourceName := fmt.Sprintf("com.github.aenix.io.cozystack.pkg.apis.apps.v1alpha1.%sList", gvk.Kind)
239246
newListDef, err := DeepCopySchema(&listDef)
240247
if err != nil {
241248
return nil, fmt.Errorf("failed to deepcopy schema for %sList: %w", gvk.Kind, err)
242249
}
243250

244-
// Fix Extensions for List resource
251+
// 5. Update x-kubernetes-group-version-kind for the List definition
245252
if newListDef.Extensions == nil {
246253
newListDef.Extensions = map[string]interface{}{}
247254
}
@@ -252,10 +259,22 @@ func (o *AppsServerOptions) Config() (*apiserver.Config, error) {
252259
"kind": fmt.Sprintf("%sList", gvk.Kind),
253260
},
254261
}
262+
263+
// 6. IMPORTANT: Fix the "items" reference so it points to the new resource
264+
// rather than to "Application".
265+
if itemsProp, found := newListDef.Properties["items"]; found {
266+
if itemsProp.Items != nil && itemsProp.Items.Schema != nil {
267+
itemsProp.Items.Schema.Ref = spec.MustCreateRef("#/definitions/" + resourceName)
268+
newListDef.Properties["items"] = itemsProp
269+
}
270+
}
271+
272+
// 7. Finally, save the new List definition
255273
defs[listResourceName] = *newListDef
256274
klog.V(6).Infof("PostProcessSpec: Added OpenAPI definition for %s\n", listResourceName)
257275
}
258276

277+
// Remove the original Application/ApplicationList from the definitions
259278
delete(defs, "com.github.aenix.io.cozystack.pkg.apis.apps.v1alpha1.Application")
260279
delete(defs, "com.github.aenix.io.cozystack.pkg.apis.apps.v1alpha1.ApplicationList")
261280

0 commit comments

Comments
 (0)