Skip to content

Commit deebfcb

Browse files
authored
feat: Validate swagger definitions (#5694)
* docs: audit, deploymentconfig, files, parameters * Swagger comments in workspacebuilds.go * structs in workspacebuilds.go * workspaceagents: instance identity * workspaceagents.go in progress * workspaceagents.go in progress * Agents * workspacebuilds.go * /workspaces * templates.go, templateversions.go * templateversion.go in progress * cancel * templateversions * wip * Merge * x-apidocgen * NullTime hack not needed anymore * Fix: x-apidocgen * Members * Fixes * Fix * WIP * WIP * Users * Logout * User profile * Status suspend activate * User roles * User tokens * Keys * SSH key * All * Typo * Fix * Entitlements * Groups * SCIM * Fix * Fix * Clean templates * Sort API pages * Fix: HashedSecret * WIP * WIP * WIP * Fix: cover workspaceagents * Assert: consistent ID and summary * Assert: success or failure defined * Fix: parallel * Refactor * Support enterprise * Go comment goes to top * Security * assertPathParametersDefined * assertUniqueRoutes * assertRequestBody * More fixes * Fix: exceptions * Fix field format * Address PR comments * Refactor
1 parent dcab873 commit deebfcb

39 files changed

+2473
-736
lines changed

coderd/apidoc/docs.go

+628-270
Large diffs are not rendered by default.

coderd/apidoc/swagger.json

+567-247
Large diffs are not rendered by default.

coderd/apikey.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
// @Summary Create token API key
3030
// @ID create-token-api-key
3131
// @Security CoderSessionToken
32+
// @Accept json
3233
// @Produce json
3334
// @Tags Users
3435
// @Param user path string true "User ID, name, or me"
@@ -209,9 +210,8 @@ func (api *API) tokens(rw http.ResponseWriter, r *http.Request) {
209210
}
210211

211212
// @Summary Delete API key
212-
// @ID delete-user-tokens
213+
// @ID delete-api-key
213214
// @Security CoderSessionToken
214-
// @Produce json
215215
// @Tags Users
216216
// @Param user path string true "User ID, name, or me"
217217
// @Param keyid path string true "Key ID" format(uuid)

coderd/audit.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func (api *API) auditLogs(rw http.ResponseWriter, r *http.Request) {
8989
}
9090

9191
// @Summary Generate fake audit log
92-
// @ID generate-fake-audit-logs
92+
// @ID generate-fake-audit-log
9393
// @Security CoderSessionToken
9494
// @Accept json
9595
// @Tags Audit

coderd/coderdtest/swagger_test.go

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package coderdtest_test
2+
3+
import (
4+
"go/ast"
5+
"go/parser"
6+
"go/token"
7+
"strings"
8+
"testing"
9+
10+
"github.com/stretchr/testify/assert"
11+
"github.com/stretchr/testify/require"
12+
13+
"github.com/coder/coder/coderd/coderdtest"
14+
)
15+
16+
func TestEndpointsDocumented(t *testing.T) {
17+
t.Parallel()
18+
19+
swaggerComments, err := coderdtest.ParseSwaggerComments("..")
20+
require.NoError(t, err, "can't parse swagger comments")
21+
22+
_, _, api := coderdtest.NewWithAPI(t, nil)
23+
coderdtest.VerifySwaggerDefinitions(t, api.APIHandler, swaggerComments)
24+
}
25+
26+
func TestSDKFieldsFormatted(t *testing.T) {
27+
t.Parallel()
28+
29+
fileSet := token.NewFileSet()
30+
nodes, err := parser.ParseDir(fileSet, "../../codersdk", nil, parser.ParseComments)
31+
require.NoError(t, err, "parser.ParseDir failed")
32+
33+
for _, node := range nodes {
34+
ast.Inspect(node, func(n ast.Node) bool {
35+
typeSpec, ok := n.(*ast.TypeSpec)
36+
if !ok {
37+
return true
38+
}
39+
structureName := typeSpec.Name
40+
41+
structType, ok := typeSpec.Type.(*ast.StructType)
42+
if !ok {
43+
return true // not a structure
44+
}
45+
46+
for _, field := range structType.Fields.List {
47+
selectorExpr, ok := field.Type.(*ast.SelectorExpr)
48+
if !ok {
49+
continue // rather a basic, or primitive
50+
}
51+
52+
if field.Tag == nil || !strings.Contains(field.Tag.Value, `json:"`) {
53+
continue // not a JSON property
54+
}
55+
56+
switch selectorExpr.Sel.Name {
57+
case "UUID":
58+
assert.Contains(t, field.Tag.Value, `format:"uuid"`, `Swagger formatting requires to annotate the field with - format:"uuid". Location: %s/%s`, structureName, field.Names)
59+
case "Time":
60+
assert.Contains(t, field.Tag.Value, `format:"date-time"`, `Swagger formatting requires to annotate the field with - format:"date-time". Location: %s/%s`, structureName, field.Names)
61+
}
62+
}
63+
return true
64+
})
65+
}
66+
}

0 commit comments

Comments
 (0)