MCPcopy Index your code
hub / github.com/coder/coder / VerifySwaggerDefinitions

Function VerifySwaggerDefinitions

coderd/coderdtest/swaggerparser.go:171–228  ·  view source on GitHub ↗
(t *testing.T, router chi.Router, swaggerComments []SwaggerComment, opts ...SwaggerOption)

Source from the content-addressed store, hash-verified

169}
170
171func VerifySwaggerDefinitions(t *testing.T, router chi.Router, swaggerComments []SwaggerComment, opts ...SwaggerOption) {
172 cfg := swaggerOptions{}
173 for _, opt := range opts {
174 opt(&cfg)
175 }
176
177 assertUniqueRoutes(t, swaggerComments)
178 assertSingleAnnotations(t, swaggerComments)
179
180 err := chi.Walk(router, func(method, route string, _ http.Handler, _ ...func(http.Handler) http.Handler) error {
181 method = strings.ToLower(method)
182 if route != "/" && strings.HasSuffix(route, "/") {
183 route = route[:len(route)-1]
184 }
185
186 // chi.Walk yields routes relative to the router that
187 // VerifySwaggerDefinitions was called with. Prepend the configured
188 // mount prefix so routes match the absolute paths used in @Router
189 // annotations.
190 if cfg.routePrefix != "" {
191 if route == "/" {
192 route = cfg.routePrefix + "/"
193 } else {
194 route = cfg.routePrefix + route
195 }
196 }
197
198 t.Run(method+" "+route, func(t *testing.T) {
199 t.Parallel()
200
201 // Wildcard routes break the swaggo parser, so we do not document
202 // them.
203 if strings.HasSuffix(route, "/*") {
204 return
205 }
206 if isExperimentalEndpoint(route) {
207 return
208 }
209
210 c := findSwaggerCommentByMethodAndRoute(swaggerComments, method, route)
211 assert.NotNil(t, c, "Missing @Router annotation")
212 if c == nil {
213 return // do not fail next assertion for this route
214 }
215
216 assertConsistencyBetweenRouteIDAndSummary(t, *c)
217 assertSuccessOrFailureDefined(t, *c)
218 assertRequiredAnnotations(t, *c)
219 assertGoCommentFirst(t, *c)
220 assertPathParametersDefined(t, *c)
221 assertSecurityDefined(t, *c)
222 assertAccept(t, *c)
223 assertProduce(t, *c)
224 })
225 return nil
226 })
227 require.NoError(t, err, "chi.Walk should not fail")
228}

Callers 2

TestEndpointsDocumentedFunction · 0.92

Calls 13

assertUniqueRoutesFunction · 0.85
assertSingleAnnotationsFunction · 0.85
isExperimentalEndpointFunction · 0.85
assertGoCommentFirstFunction · 0.85
assertSecurityDefinedFunction · 0.85
assertAcceptFunction · 0.85
assertProduceFunction · 0.85

Tested by 2

TestEndpointsDocumentedFunction · 0.74