Skip to content

Commit 59398d9

Browse files
feat: allow for order of rendering be overridden in docs
1 parent 6a2af99 commit 59398d9

File tree

3 files changed

+158
-2
lines changed

3 files changed

+158
-2
lines changed

tools/api-docs-generator/generator/reference_docs.go

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package generator
22

33
import (
4+
"cmp"
45
"fmt"
56
"os"
67
"path"
78
"path/filepath"
9+
"slices"
810
"sort"
11+
"strconv"
912
"strings"
1013

1114
"github.com/getkin/kin-openapi/openapi3"
@@ -20,6 +23,7 @@ type operationPath struct {
2023
specPath string
2124
method string
2225
docsHint string
26+
order int
2327
}
2428

2529
func GenerateReferenceDocs(cfg *config.Config, docsBasePath string) error {
@@ -35,6 +39,9 @@ func GenerateReferenceDocs(cfg *config.Config, docsBasePath string) error {
3539
}
3640

3741
for label, operations := range aggregatedDocs {
42+
slices.SortFunc(operations, func(i, j operationPath) int {
43+
return cmp.Compare(i.order, j.order)
44+
})
3845
destinationPath := path.Join(docsBasePath, cfg.Output.APIReferencePath, labelToFileName(label))
3946
summary = append(summary, fmt.Sprintf("* [%s](%s)\n", label, path.Join(cfg.Output.APIReferencePath, labelToFileName(label))))
4047

@@ -139,14 +146,23 @@ func processOperation(pathURL string,
139146
operation *openapi3.Operation,
140147
spec config.Spec,
141148
specDocs map[string][]operationPath) error {
149+
var order int
142150
for _, tag := range operation.Tags {
143151
if tag == "OpenAPI" {
144152
continue
145153
}
146154

147155
if snykDocsExtension, ok := operation.Extensions["x-snyk-documentation"]; ok && snykDocsExtension != nil {
148156
var err error
149-
tag, err = extractCategoryNameFromExtension(snykDocsExtension)
157+
renamedTag, err := extractCategoryNameFromExtension(snykDocsExtension)
158+
if err != nil {
159+
return err
160+
}
161+
if renamedTag != "" {
162+
tag = renamedTag
163+
}
164+
165+
order, err = extractOrderFromExtension(snykDocsExtension)
150166
if err != nil {
151167
return err
152168
}
@@ -164,6 +180,7 @@ func processOperation(pathURL string,
164180
specPath: spec.Path,
165181
method: method,
166182
docsHint: spec.DocsHint,
183+
order: order,
167184
})
168185
}
169186
return nil
@@ -181,14 +198,30 @@ func isBeta(operation *openapi3.Operation) bool {
181198
return stabilityStr == "beta"
182199
}
183200

201+
func extractOrderFromExtension(extension interface{}) (int, error) {
202+
extensionMap, worked := extension.(map[string]interface{})
203+
if !worked {
204+
return 0, fmt.Errorf("failed to parse docs extension as an object")
205+
}
206+
orderValue, worked := extensionMap["order"].(string)
207+
if !worked {
208+
return 0, nil
209+
}
210+
orderAsInt, err := strconv.Atoi(orderValue)
211+
if err != nil {
212+
return 0, fmt.Errorf("x-snyk-documentation extension order field not a int, %w", err)
213+
}
214+
return orderAsInt, nil
215+
}
216+
184217
func extractCategoryNameFromExtension(extension interface{}) (string, error) {
185218
extensionMap, worked := extension.(map[string]interface{})
186219
if !worked {
187220
return "", fmt.Errorf("failed to parse docs extension as an object")
188221
}
189222
categoryValue, worked := extensionMap["category"].(string)
190223
if !worked {
191-
return "", fmt.Errorf("x-snyk-documentation extension category field not a string")
224+
return "", nil
192225
}
193226
return categoryValue, nil
194227
}

tools/api-docs-generator/generator/reference_docs_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,39 @@ func Test_aggregateSpecs(t *testing.T) {
294294
},
295295
wantErr: assert.NoError,
296296
},
297+
{
298+
name: "uses override order if present",
299+
args: args{
300+
cfg: &config.Config{
301+
Specs: []config.Spec{
302+
{
303+
Path: "spec_with_order_override.yaml",
304+
},
305+
},
306+
},
307+
docsBasePath: "../testdata/reference_docs/",
308+
},
309+
want: map[string][]operationPath{
310+
"test": {
311+
{
312+
method: "POST",
313+
specPath: "spec_with_order_override.yaml",
314+
pathURL: "/test",
315+
},
316+
{
317+
method: "POST",
318+
specPath: "spec_with_order_override.yaml",
319+
pathURL: "/test/1",
320+
},
321+
{
322+
method: "POST",
323+
specPath: "spec_with_order_override.yaml",
324+
pathURL: "/test/2",
325+
},
326+
},
327+
},
328+
wantErr: assert.NoError,
329+
},
297330
{
298331
name: "filters out beta paths",
299332
args: args{
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
openapi: 3.0.3
2+
info:
3+
title: Test
4+
contact: {}
5+
version: 3.0.0
6+
description: Sample API
7+
servers:
8+
- url: /test
9+
description: Test
10+
tags:
11+
- name: Test
12+
description: test
13+
paths:
14+
/test:
15+
post:
16+
x-snyk-api-stability: ga
17+
x-snyk-documentation:
18+
order: -1
19+
tags:
20+
- test
21+
description: test
22+
operationId: test
23+
requestBody:
24+
content:
25+
application/vnd.api+json:
26+
schema:
27+
type: object
28+
properties:
29+
prop1:
30+
type: string
31+
responses:
32+
"201":
33+
description: success
34+
content:
35+
application/vnd.api+json:
36+
schema:
37+
type: object
38+
location:
39+
schema:
40+
type: string
41+
/test/1:
42+
post:
43+
x-snyk-api-stability: ga
44+
tags:
45+
- test
46+
description: test
47+
operationId: test
48+
requestBody:
49+
content:
50+
application/vnd.api+json:
51+
schema:
52+
type: object
53+
properties:
54+
prop1:
55+
type: string
56+
responses:
57+
"201":
58+
description: success
59+
content:
60+
application/vnd.api+json:
61+
schema:
62+
type: object
63+
location:
64+
schema:
65+
type: string
66+
/test/2:
67+
post:
68+
x-snyk-api-stability: ga
69+
tags:
70+
- test
71+
description: test
72+
operationId: test
73+
requestBody:
74+
content:
75+
application/vnd.api+json:
76+
schema:
77+
type: object
78+
properties:
79+
prop1:
80+
type: string
81+
responses:
82+
"201":
83+
description: success
84+
content:
85+
application/vnd.api+json:
86+
schema:
87+
type: object
88+
location:
89+
schema:
90+
type: string

0 commit comments

Comments
 (0)