Skip to content

Commit b2d838f

Browse files
committed
More fixes
1 parent d0964f9 commit b2d838f

File tree

10 files changed

+56
-57
lines changed

10 files changed

+56
-57
lines changed

coderd/apidoc/docs.go

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -339,9 +339,6 @@ const docTemplate = `{
339339
"consumes": [
340340
"application/json"
341341
],
342-
"produces": [
343-
"text/plain"
344-
],
345342
"tags": [
346343
"General"
347344
],
@@ -1074,9 +1071,6 @@ const docTemplate = `{
10741071
"CoderSessionToken": []
10751072
}
10761073
],
1077-
"produces": [
1078-
"application/json"
1079-
],
10801074
"tags": [
10811075
"Enterprise"
10821076
],
@@ -2827,9 +2821,6 @@ const docTemplate = `{
28272821
"CoderSessionToken": []
28282822
}
28292823
],
2830-
"produces": [
2831-
"application/json"
2832-
],
28332824
"tags": [
28342825
"Users"
28352826
],
@@ -2849,9 +2840,6 @@ const docTemplate = `{
28492840
"CoderSessionToken": []
28502841
}
28512842
],
2852-
"produces": [
2853-
"application/json"
2854-
],
28552843
"tags": [
28562844
"Users"
28572845
],
@@ -3186,9 +3174,6 @@ const docTemplate = `{
31863174
"CoderSessionToken": []
31873175
}
31883176
],
3189-
"produces": [
3190-
"application/json"
3191-
],
31923177
"tags": [
31933178
"Users"
31943179
],
@@ -3306,9 +3291,6 @@ const docTemplate = `{
33063291
"consumes": [
33073292
"application/json"
33083293
],
3309-
"produces": [
3310-
"application/json"
3311-
],
33123294
"tags": [
33133295
"Users"
33143296
],
@@ -3822,9 +3804,6 @@ const docTemplate = `{
38223804
}
38233805
],
38243806
"description": "It accepts a WebSocket connection to an agent that listens to\nincoming connections and publishes node updates.",
3825-
"produces": [
3826-
"application/json"
3827-
],
38283807
"tags": [
38293808
"Agents"
38303809
],
@@ -4486,9 +4465,6 @@ const docTemplate = `{
44864465
"consumes": [
44874466
"application/json"
44884467
],
4489-
"produces": [
4490-
"application/json"
4491-
],
44924468
"tags": [
44934469
"Workspaces"
44944470
],
@@ -4530,9 +4506,6 @@ const docTemplate = `{
45304506
"consumes": [
45314507
"application/json"
45324508
],
4533-
"produces": [
4534-
"application/json"
4535-
],
45364509
"tags": [
45374510
"Workspaces"
45384511
],
@@ -4730,9 +4703,6 @@ const docTemplate = `{
47304703
"consumes": [
47314704
"application/json"
47324705
],
4733-
"produces": [
4734-
"application/json"
4735-
],
47364706
"tags": [
47374707
"Workspaces"
47384708
],

coderd/apidoc/swagger.json

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,6 @@
287287
}
288288
],
289289
"consumes": ["application/json"],
290-
"produces": ["text/plain"],
291290
"tags": ["General"],
292291
"summary": "Report CSP violations",
293292
"operationId": "report-csp-violations",
@@ -934,7 +933,6 @@
934933
"CoderSessionToken": []
935934
}
936935
],
937-
"produces": ["application/json"],
938936
"tags": ["Enterprise"],
939937
"summary": "Serve provisioner daemon",
940938
"operationId": "serve-provisioner-daemon",
@@ -2481,7 +2479,6 @@
24812479
"CoderSessionToken": []
24822480
}
24832481
],
2484-
"produces": ["application/json"],
24852482
"tags": ["Users"],
24862483
"summary": "OAuth 2.0 GitHub Callback",
24872484
"operationId": "oauth-20-github-callback",
@@ -2499,7 +2496,6 @@
24992496
"CoderSessionToken": []
25002497
}
25012498
],
2502-
"produces": ["application/json"],
25032499
"tags": ["Users"],
25042500
"summary": "OpenID Connect Callback",
25052501
"operationId": "openid-connect-callback",
@@ -2794,7 +2790,6 @@
27942790
"CoderSessionToken": []
27952791
}
27962792
],
2797-
"produces": ["application/json"],
27982793
"tags": ["Users"],
27992794
"summary": "Delete API key",
28002795
"operationId": "delete-api-key",
@@ -2900,7 +2895,6 @@
29002895
}
29012896
],
29022897
"consumes": ["application/json"],
2903-
"produces": ["application/json"],
29042898
"tags": ["Users"],
29052899
"summary": "Update user password",
29062900
"operationId": "update-user-password",
@@ -3352,7 +3346,6 @@
33523346
}
33533347
],
33543348
"description": "It accepts a WebSocket connection to an agent that listens to\nincoming connections and publishes node updates.",
3355-
"produces": ["application/json"],
33563349
"tags": ["Agents"],
33573350
"summary": "Coordinate workspace agent via Tailnet",
33583351
"operationId": "coordinate-workspace-agent-via-tailnet",
@@ -3937,7 +3930,6 @@
39373930
}
39383931
],
39393932
"consumes": ["application/json"],
3940-
"produces": ["application/json"],
39413933
"tags": ["Workspaces"],
39423934
"summary": "Update workspace metadata by ID",
39433935
"operationId": "update-workspace-metadata-by-id",
@@ -3975,7 +3967,6 @@
39753967
}
39763968
],
39773969
"consumes": ["application/json"],
3978-
"produces": ["application/json"],
39793970
"tags": ["Workspaces"],
39803971
"summary": "Update workspace autostart schedule by ID",
39813972
"operationId": "update-workspace-autostart-schedule-by-id",
@@ -4153,7 +4144,6 @@
41534144
}
41544145
],
41554146
"consumes": ["application/json"],
4156-
"produces": ["application/json"],
41574147
"tags": ["Workspaces"],
41584148
"summary": "Update workspace TTL by ID",
41594149
"operationId": "update-workspace-ttl-by-id",

coderd/apikey.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,6 @@ func (api *API) tokens(rw http.ResponseWriter, r *http.Request) {
212212
// @Summary Delete API key
213213
// @ID delete-api-key
214214
// @Security CoderSessionToken
215-
// @Produce json
216215
// @Tags Users
217216
// @Param user path string true "User ID, name, or me"
218217
// @Param keyid path string true "Key ID" format(uuid)

coderd/coderdtest/swaggerparser.go

Lines changed: 56 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ type SwaggerComment struct {
2626
method string
2727
router string
2828

29-
hasSuccess bool
30-
hasFailure bool
29+
successes []response
30+
failures []response
3131

3232
parameters []parameter
3333

@@ -39,6 +39,12 @@ type parameter struct {
3939
kind string
4040
}
4141

42+
type response struct {
43+
status string
44+
kind string // {object} or {array}
45+
model string
46+
}
47+
4248
func ParseSwaggerComments(dirs ...string) ([]SwaggerComment, error) {
4349
fileSet := token.NewFileSet()
4450

@@ -79,6 +85,8 @@ func parseSwaggerComment(commentGroup *ast.CommentGroup) SwaggerComment {
7985
c := SwaggerComment{
8086
raw: commentGroup.List,
8187
parameters: []parameter{},
88+
successes: []response{},
89+
failures: []response{},
8290
}
8391
for _, line := range commentGroup.List {
8492
splitN := strings.SplitN(strings.TrimSpace(line.Text), " ", 2)
@@ -98,9 +106,33 @@ func parseSwaggerComment(commentGroup *ast.CommentGroup) SwaggerComment {
98106
args := strings.SplitN(text, " ", 2)
99107
c.id = args[1]
100108
} else if strings.HasPrefix(text, "@Success ") {
101-
c.hasSuccess = true
109+
args := strings.Split(text, " ")
110+
111+
var success response
112+
if len(args) > 1 {
113+
success.status = args[1]
114+
}
115+
if len(args) > 2 {
116+
success.kind = args[2]
117+
}
118+
if len(args) > 3 {
119+
success.model = args[3]
120+
}
121+
c.successes = append(c.successes, success)
102122
} else if strings.HasPrefix(text, "@Failure ") {
103-
c.hasFailure = true
123+
args := strings.Split(text, " ")
124+
125+
var failure response
126+
if len(args) > 1 {
127+
failure.status = args[1]
128+
}
129+
if len(args) > 2 {
130+
failure.kind = args[2]
131+
}
132+
if len(args) > 3 {
133+
failure.model = args[3]
134+
}
135+
c.failures = append(c.successes, failure)
104136
} else if strings.HasPrefix(text, "@Tags ") {
105137
args := strings.SplitN(text, " ", 2)
106138
c.tags = args[1]
@@ -149,7 +181,8 @@ func VerifySwaggerDefinitions(t *testing.T, router chi.Router, swaggerComments [
149181
assertGoCommentFirst(t, *c)
150182
assertPathParametersDefined(t, *c)
151183
assertSecurityDefined(t, *c)
152-
assertRequestBody(t, *c)
184+
assertAccept(t, *c)
185+
assertProduce(t, *c)
153186
})
154187
return nil
155188
})
@@ -189,7 +222,7 @@ func assertConsistencyBetweenRouteIDAndSummary(t *testing.T, comment SwaggerComm
189222
}
190223

191224
func assertSuccessOrFailureDefined(t *testing.T, comment SwaggerComment) {
192-
assert.True(t, comment.hasSuccess || comment.hasFailure, "At least one @Success or @Failure annotation must be defined")
225+
assert.True(t, len(comment.successes) > 0 || len(comment.failures) > 0, "At least one @Success or @Failure annotation must be defined")
193226
}
194227

195228
func assertRequiredAnnotations(t *testing.T, comment SwaggerComment) {
@@ -248,7 +281,7 @@ func assertSecurityDefined(t *testing.T, comment SwaggerComment) {
248281
assert.Equal(t, "CoderSessionToken", comment.security, "@Security must be equal CoderSessionToken")
249282
}
250283

251-
func assertRequestBody(t *testing.T, comment SwaggerComment) {
284+
func assertAccept(t *testing.T, comment SwaggerComment) {
252285
var hasRequestBody bool
253286
for _, c := range comment.parameters {
254287
if c.name == "request" && c.kind == "body" ||
@@ -271,3 +304,19 @@ func assertRequestBody(t *testing.T, comment SwaggerComment) {
271304
assert.False(t, !hasRequestBody && hasAccept, "Route with @Accept annotation requires the request body or file formData parameter")
272305
}
273306
}
307+
308+
func assertProduce(t *testing.T, comment SwaggerComment) {
309+
var hasResponseModel bool
310+
for _, r := range comment.successes {
311+
if r.model != "" {
312+
hasResponseModel = true
313+
break
314+
}
315+
}
316+
317+
if hasResponseModel {
318+
assert.True(t, comment.produce != "", "Route must have @Produce annotation as it responds with a model structure")
319+
} else {
320+
assert.True(t, comment.produce == "", "Response model is undefined, so we can't predict the content type", comment)
321+
}
322+
}

coderd/csp.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ type cspViolation struct {
2020
// @ID report-csp-violations
2121
// @Security CoderSessionToken
2222
// @Accept json
23-
// @Produce text/plain
2423
// @Tags General
2524
// @Param request body cspViolation true "Violation report"
2625
// @Success 200

coderd/userauth.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ func (api *API) userAuthMethods(rw http.ResponseWriter, r *http.Request) {
6161
// @Summary OAuth 2.0 GitHub Callback
6262
// @ID oauth-20-github-callback
6363
// @Security CoderSessionToken
64-
// @Produce json
6564
// @Tags Users
6665
// @Success 307
6766
// @Router /users/oauth2/github/callback [get]
@@ -220,7 +219,6 @@ type OIDCConfig struct {
220219
// @Summary OpenID Connect Callback
221220
// @ID openid-connect-callback
222221
// @Security CoderSessionToken
223-
// @Produce json
224222
// @Tags Users
225223
// @Success 307
226224
// @Router /users/oidc/callback [get]

coderd/users.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,6 @@ func (api *API) putUserStatus(status database.UserStatus) func(rw http.ResponseW
619619
// @ID update-user-password
620620
// @Security CoderSessionToken
621621
// @Accept json
622-
// @Produce json
623622
// @Tags Users
624623
// @Param user path string true "User ID, name, or me"
625624
// @Param request body codersdk.UpdateUserPasswordRequest true "Update password request"

coderd/workspaceagents.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,6 @@ func (api *API) workspaceAgentConnection(rw http.ResponseWriter, r *http.Request
492492
// @Description incoming connections and publishes node updates.
493493
// @ID coordinate-workspace-agent-via-tailnet
494494
// @Security CoderSessionToken
495-
// @Produce json
496495
// @Tags Agents
497496
// @Success 101
498497
// @Router /workspaceagents/me/coordinate [get]

coderd/workspaces.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,6 @@ func (api *API) postWorkspacesByOrganization(rw http.ResponseWriter, r *http.Req
566566
// @ID update-workspace-metadata-by-id
567567
// @Security CoderSessionToken
568568
// @Accept json
569-
// @Produce json
570569
// @Tags Workspaces
571570
// @Param workspace path string true "Workspace ID" format(uuid)
572571
// @Param request body codersdk.UpdateWorkspaceRequest true "Metadata update request"
@@ -656,7 +655,6 @@ func (api *API) patchWorkspace(rw http.ResponseWriter, r *http.Request) {
656655
// @ID update-workspace-autostart-schedule-by-id
657656
// @Security CoderSessionToken
658657
// @Accept json
659-
// @Produce json
660658
// @Tags Workspaces
661659
// @Param workspace path string true "Workspace ID" format(uuid)
662660
// @Param request body codersdk.UpdateWorkspaceAutostartRequest true "Schedule update request"
@@ -719,7 +717,6 @@ func (api *API) putWorkspaceAutostart(rw http.ResponseWriter, r *http.Request) {
719717
// @ID update-workspace-ttl-by-id
720718
// @Security CoderSessionToken
721719
// @Accept json
722-
// @Produce json
723720
// @Tags Workspaces
724721
// @Param workspace path string true "Workspace ID" format(uuid)
725722
// @Param request body codersdk.UpdateWorkspaceTTLRequest true "Workspace TTL update request"

enterprise/coderd/provisionerdaemons.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ func (api *API) provisionerDaemons(rw http.ResponseWriter, r *http.Request) {
9494
// @Summary Serve provisioner daemon
9595
// @ID serve-provisioner-daemon
9696
// @Security CoderSessionToken
97-
// @Produce json
9897
// @Tags Enterprise
9998
// @Param organization path string true "Organization ID" format(uuid)
10099
// @Success 101

0 commit comments

Comments
 (0)