Skip to content

Commit

Permalink
Fix web.Handle with all available HTTP methods (#201)
Browse files Browse the repository at this point in the history
  • Loading branch information
vearutop committed Mar 29, 2024
1 parent a54f132 commit 15a0c23
Show file tree
Hide file tree
Showing 3 changed files with 162 additions and 9 deletions.
28 changes: 20 additions & 8 deletions nethttp/openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,26 @@ func OpenAPIMiddleware(s *openapi.Collector) func(http.Handler) http.Handler {
return h
}

err := s.CollectUseCase(
withRoute.RouteMethod(),
withRoute.RoutePattern(),
handler.UseCase(),
handler.HandlerTrait,
)
if err != nil {
panic(err)
var methods []string

method := withRoute.RouteMethod()

if method == "" {
methods = []string{"get", "put", "post", "delete", "options", "head", "patch", "trace"}
} else {
methods = []string{method}
}

for _, m := range methods {
err := s.CollectUseCase(
m,
withRoute.RoutePattern(),
handler.UseCase(),
handler.HandlerTrait,
)
if err != nil {
panic(err)
}
}

return h
Expand Down
141 changes: 140 additions & 1 deletion web/_testdata/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,145 @@
"version":"v1.0.0"
},
"paths":{
"/a/{id}":{
"delete":{
"tags":["Album"],"summary":"Album By ID",
"operationId":"rest/web_test.albumByID7",
"parameters":[
{"name":"locale","in":"query","schema":{"type":"string"}},
{
"name":"id","in":"path","required":true,"schema":{"type":"integer"}
}
],
"responses":{
"200":{
"description":"OK",
"content":{
"application/json":{"schema":{"$ref":"#/components/schemas/WebTestAlbum"}}
}
}
}
},
"get":{
"tags":["Album"],"summary":"Album By ID",
"operationId":"rest/web_test.albumByID4",
"parameters":[
{"name":"locale","in":"query","schema":{"type":"string"}},
{
"name":"id","in":"path","required":true,"schema":{"type":"integer"}
}
],
"responses":{
"200":{
"description":"OK",
"content":{
"application/json":{"schema":{"$ref":"#/components/schemas/WebTestAlbum"}}
}
}
}
},
"head":{
"tags":["Album"],"summary":"Album By ID",
"operationId":"rest/web_test.albumByID9",
"parameters":[
{"name":"locale","in":"query","schema":{"type":"string"}},
{
"name":"id","in":"path","required":true,"schema":{"type":"integer"}
}
],
"responses":{"200":{"description":"OK"}}
},
"options":{
"tags":["Album"],"summary":"Album By ID",
"operationId":"rest/web_test.albumByID8",
"parameters":[
{"name":"locale","in":"query","schema":{"type":"string"}},
{
"name":"id","in":"path","required":true,"schema":{"type":"integer"}
}
],
"responses":{
"200":{
"description":"OK",
"content":{
"application/json":{"schema":{"$ref":"#/components/schemas/WebTestAlbum"}}
}
}
}
},
"patch":{
"tags":["Album"],"summary":"Album By ID",
"operationId":"rest/web_test.albumByID10",
"parameters":[
{"name":"locale","in":"query","schema":{"type":"string"}},
{
"name":"id","in":"path","required":true,"schema":{"type":"integer"}
}
],
"responses":{
"200":{
"description":"OK",
"content":{
"application/json":{"schema":{"$ref":"#/components/schemas/WebTestAlbum"}}
}
}
}
},
"post":{
"tags":["Album"],"summary":"Album By ID",
"operationId":"rest/web_test.albumByID6",
"parameters":[
{"name":"locale","in":"query","schema":{"type":"string"}},
{
"name":"id","in":"path","required":true,"schema":{"type":"integer"}
}
],
"responses":{
"200":{
"description":"OK",
"content":{
"application/json":{"schema":{"$ref":"#/components/schemas/WebTestAlbum"}}
}
}
}
},
"put":{
"tags":["Album"],"summary":"Album By ID",
"operationId":"rest/web_test.albumByID5",
"parameters":[
{"name":"locale","in":"query","schema":{"type":"string"}},
{
"name":"id","in":"path","required":true,"schema":{"type":"integer"}
}
],
"responses":{
"200":{
"description":"OK",
"content":{
"application/json":{"schema":{"$ref":"#/components/schemas/WebTestAlbum"}}
}
}
}
},
"trace":{
"tags":["Album"],"summary":"Album By ID",
"operationId":"rest/web_test.albumByID11",
"parameters":[
{"name":"locale","in":"query","schema":{"type":"string"}},
{
"name":"id","in":"path","required":true,"schema":{"type":"integer"}
}
],
"responses":{
"200":{
"description":"OK",
"content":{
"application/json":{"schema":{"$ref":"#/components/schemas/WebTestAlbum"}}
}
}
}
}
},
"/albums":{
"options":{
"tags":["Album"],"summary":"Post Albums",
Expand Down Expand Up @@ -154,4 +293,4 @@
}
}
}
}
}
2 changes: 2 additions & 0 deletions web/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ func TestDefaultService(t *testing.T) {
}),
)

service.Handle("/a/{id}", nethttp.NewHandler(albumByID()))

rw := httptest.NewRecorder()
r, err := http.NewRequest(http.MethodGet, "http://localhost/docs/openapi.json", nil)
require.NoError(t, err)
Expand Down

0 comments on commit 15a0c23

Please sign in to comment.