mirror of
https://github.com/coder/coder.git
synced 2025-07-03 16:13:58 +00:00
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
This commit is contained in:
66
coderd/coderdtest/swagger_test.go
Normal file
66
coderd/coderdtest/swagger_test.go
Normal file
@ -0,0 +1,66 @@
|
||||
package coderdtest_test
|
||||
|
||||
import (
|
||||
"go/ast"
|
||||
"go/parser"
|
||||
"go/token"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/coder/coder/coderd/coderdtest"
|
||||
)
|
||||
|
||||
func TestEndpointsDocumented(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
swaggerComments, err := coderdtest.ParseSwaggerComments("..")
|
||||
require.NoError(t, err, "can't parse swagger comments")
|
||||
|
||||
_, _, api := coderdtest.NewWithAPI(t, nil)
|
||||
coderdtest.VerifySwaggerDefinitions(t, api.APIHandler, swaggerComments)
|
||||
}
|
||||
|
||||
func TestSDKFieldsFormatted(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
fileSet := token.NewFileSet()
|
||||
nodes, err := parser.ParseDir(fileSet, "../../codersdk", nil, parser.ParseComments)
|
||||
require.NoError(t, err, "parser.ParseDir failed")
|
||||
|
||||
for _, node := range nodes {
|
||||
ast.Inspect(node, func(n ast.Node) bool {
|
||||
typeSpec, ok := n.(*ast.TypeSpec)
|
||||
if !ok {
|
||||
return true
|
||||
}
|
||||
structureName := typeSpec.Name
|
||||
|
||||
structType, ok := typeSpec.Type.(*ast.StructType)
|
||||
if !ok {
|
||||
return true // not a structure
|
||||
}
|
||||
|
||||
for _, field := range structType.Fields.List {
|
||||
selectorExpr, ok := field.Type.(*ast.SelectorExpr)
|
||||
if !ok {
|
||||
continue // rather a basic, or primitive
|
||||
}
|
||||
|
||||
if field.Tag == nil || !strings.Contains(field.Tag.Value, `json:"`) {
|
||||
continue // not a JSON property
|
||||
}
|
||||
|
||||
switch selectorExpr.Sel.Name {
|
||||
case "UUID":
|
||||
assert.Contains(t, field.Tag.Value, `format:"uuid"`, `Swagger formatting requires to annotate the field with - format:"uuid". Location: %s/%s`, structureName, field.Names)
|
||||
case "Time":
|
||||
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)
|
||||
}
|
||||
}
|
||||
return true
|
||||
})
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user