Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a565c15

Browse files
committed
assertPathParametersDefined
1 parent 38b6dad commit a565c15

File tree

7 files changed

+64
-11
lines changed

7 files changed

+64
-11
lines changed

coderd/apidoc/docs.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2224,6 +2224,14 @@ const docTemplate = `{
22242224
"summary": "Cancel template version dry-run by job ID",
22252225
"operationId": "cancel-template-version-dry-run-by-job-id",
22262226
"parameters": [
2227+
{
2228+
"type": "string",
2229+
"format": "uuid",
2230+
"description": "Job ID",
2231+
"name": "jobID",
2232+
"in": "path",
2233+
"required": true
2234+
},
22272235
{
22282236
"type": "string",
22292237
"format": "uuid",
@@ -2271,7 +2279,7 @@ const docTemplate = `{
22712279
"type": "string",
22722280
"format": "uuid",
22732281
"description": "Job ID",
2274-
"name": "job-ID",
2282+
"name": "jobID",
22752283
"in": "path",
22762284
"required": true
22772285
},
@@ -4399,7 +4407,7 @@ const docTemplate = `{
43994407
"type": "string",
44004408
"format": "uuid",
44014409
"description": "Workspace ID",
4402-
"name": "id",
4410+
"name": "workspace",
44034411
"in": "path",
44044412
"required": true
44054413
},

coderd/apidoc/swagger.json

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1950,6 +1950,14 @@
19501950
"summary": "Cancel template version dry-run by job ID",
19511951
"operationId": "cancel-template-version-dry-run-by-job-id",
19521952
"parameters": [
1953+
{
1954+
"type": "string",
1955+
"format": "uuid",
1956+
"description": "Job ID",
1957+
"name": "jobID",
1958+
"in": "path",
1959+
"required": true
1960+
},
19531961
{
19541962
"type": "string",
19551963
"format": "uuid",
@@ -1993,7 +2001,7 @@
19932001
"type": "string",
19942002
"format": "uuid",
19952003
"description": "Job ID",
1996-
"name": "job-ID",
2004+
"name": "jobID",
19972005
"in": "path",
19982006
"required": true
19992007
},
@@ -3876,7 +3884,7 @@
38763884
"type": "string",
38773885
"format": "uuid",
38783886
"description": "Workspace ID",
3879-
"name": "id",
3887+
"name": "workspace",
38803888
"in": "path",
38813889
"required": true
38823890
},

coderd/coderdtest/swaggerparser.go

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,16 @@ type SwaggerComment struct {
2727
hasSuccess bool
2828
hasFailure bool
2929

30+
parameters []parameter
31+
3032
raw []*ast.Comment
3133
}
3234

35+
type parameter struct {
36+
name string
37+
kind string
38+
}
39+
3340
func ParseSwaggerComments(dirs ...string) ([]SwaggerComment, error) {
3441
fileSet := token.NewFileSet()
3542

@@ -68,7 +75,8 @@ func ParseSwaggerComments(dirs ...string) ([]SwaggerComment, error) {
6875

6976
func parseSwaggerComment(commentGroup *ast.CommentGroup) SwaggerComment {
7077
c := SwaggerComment{
71-
raw: commentGroup.List,
78+
raw: commentGroup.List,
79+
parameters: []parameter{},
7280
}
7381
for _, line := range commentGroup.List {
7482
splitN := strings.SplitN(strings.TrimSpace(line.Text), " ", 2)
@@ -97,6 +105,13 @@ func parseSwaggerComment(commentGroup *ast.CommentGroup) SwaggerComment {
97105
} else if strings.HasPrefix(text, "@Security ") {
98106
args := strings.SplitN(text, " ", 2)
99107
c.security = args[1]
108+
} else if strings.HasPrefix(text, "@Param ") {
109+
args := strings.SplitN(text, " ", 4)
110+
p := parameter{
111+
name: args[1],
112+
kind: args[2],
113+
}
114+
c.parameters = append(c.parameters, p)
100115
}
101116
}
102117
return c
@@ -122,7 +137,7 @@ func VerifySwaggerDefinitions(t *testing.T, router chi.Router, swaggerComments [
122137
assertSuccessOrFailureDefined(t, *c)
123138
assertRequiredAnnotations(t, *c)
124139
assertGoCommentFirst(t, *c)
125-
assertURLParamsDefined(t, *c)
140+
assertPathParametersDefined(t, *c)
126141
assertSecurityDefined(t, *c)
127142
})
128143
return nil
@@ -177,7 +192,27 @@ func assertGoCommentFirst(t *testing.T, comment SwaggerComment) {
177192
}
178193
}
179194

180-
func assertURLParamsDefined(t *testing.T, comment SwaggerComment) {
195+
var urlParameterRegexp = regexp.MustCompile(`{[^{}]*}`)
196+
197+
func assertPathParametersDefined(t *testing.T, comment SwaggerComment) {
198+
matches := urlParameterRegexp.FindAllString(comment.router, -1)
199+
if matches == nil {
200+
return // router does not require any parameters
201+
}
202+
203+
for _, m := range matches {
204+
var matched bool
205+
for _, p := range comment.parameters {
206+
if p.kind == "path" && "{"+p.name+"}" == m {
207+
matched = true
208+
break
209+
}
210+
}
211+
212+
if !matched {
213+
assert.Failf(t, "Missing @Param annotation", "Path parameter: %s", m)
214+
}
215+
}
181216
}
182217

183218
func assertSecurityDefined(t *testing.T, comment SwaggerComment) {

coderd/templateversions.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ func (api *API) templateVersionDryRunResources(rw http.ResponseWriter, r *http.R
394394
// @Produce json
395395
// @Tags Templates
396396
// @Param templateversion path string true "Template version ID" format(uuid)
397-
// @Param job-ID path string true "Job ID" format(uuid)
397+
// @Param jobID path string true "Job ID" format(uuid)
398398
// @Param before query int false "Before Unix timestamp"
399399
// @Param after query int false "After Unix timestamp"
400400
// @Param follow query bool false "Follow log stream"
@@ -414,6 +414,7 @@ func (api *API) templateVersionDryRunLogs(rw http.ResponseWriter, r *http.Reques
414414
// @Security CoderSessionToken
415415
// @Produce json
416416
// @Tags Templates
417+
// @Param jobID path string true "Job ID" format(uuid)
417418
// @Param templateversion path string true "Template version ID" format(uuid)
418419
// @Success 200 {object} codersdk.Response
419420
// @Router /templateversions/{templateversion}/dry-run/{jobID}/cancel [patch]

coderd/workspaces.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ var (
4848
// @Security CoderSessionToken
4949
// @Produce json
5050
// @Tags Workspaces
51-
// @Param id path string true "Workspace ID" format(uuid)
51+
// @Param workspace path string true "Workspace ID" format(uuid)
5252
// @Param include_deleted query bool false "Return data instead of HTTP 404 if the workspace is deleted"
5353
// @Success 200 {object} codersdk.Workspace
5454
// @Router /workspaces/{workspace} [get]

docs/api/templates.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1401,6 +1401,7 @@ curl -X PATCH http://coder-server:8080/api/v2/templateversions/{templateversion}
14011401

14021402
| Name | In | Type | Required | Description |
14031403
| ----------------- | ---- | ------------ | -------- | ------------------- |
1404+
| `jobID` | path | string(uuid) | true | Job ID |
14041405
| `templateversion` | path | string(uuid) | true | Template version ID |
14051406

14061407
### Example responses
@@ -1446,7 +1447,7 @@ curl -X GET http://coder-server:8080/api/v2/templateversions/{templateversion}/d
14461447
| Name | In | Type | Required | Description |
14471448
| ----------------- | ----- | ------------ | -------- | --------------------- |
14481449
| `templateversion` | path | string(uuid) | true | Template version ID |
1449-
| `job-ID` | path | string(uuid) | true | Job ID |
1450+
| `jobID` | path | string(uuid) | true | Job ID |
14501451
| `before` | query | integer | false | Before Unix timestamp |
14511452
| `after` | query | integer | false | After Unix timestamp |
14521453
| `follow` | query | boolean | false | Follow log stream |

docs/api/workspaces.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,7 @@ curl -X GET http://coder-server:8080/api/v2/workspaces/{workspace} \
522522

523523
| Name | In | Type | Required | Description |
524524
| ----------------- | ----- | ------------ | -------- | ----------------------------------------------------------- |
525-
| `id` | path | string(uuid) | true | Workspace ID |
525+
| `workspace` | path | string(uuid) | true | Workspace ID |
526526
| `include_deleted` | query | boolean | false | Return data instead of HTTP 404 if the workspace is deleted |
527527

528528
### Example responses

0 commit comments

Comments
 (0)