Skip to content

Commit 15a0c23

Browse files
authored
Fix web.Handle with all available HTTP methods (#201)
1 parent a54f132 commit 15a0c23

File tree

3 files changed

+162
-9
lines changed

3 files changed

+162
-9
lines changed

nethttp/openapi.go

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,26 @@ func OpenAPIMiddleware(s *openapi.Collector) func(http.Handler) http.Handler {
2525
return h
2626
}
2727

28-
err := s.CollectUseCase(
29-
withRoute.RouteMethod(),
30-
withRoute.RoutePattern(),
31-
handler.UseCase(),
32-
handler.HandlerTrait,
33-
)
34-
if err != nil {
35-
panic(err)
28+
var methods []string
29+
30+
method := withRoute.RouteMethod()
31+
32+
if method == "" {
33+
methods = []string{"get", "put", "post", "delete", "options", "head", "patch", "trace"}
34+
} else {
35+
methods = []string{method}
36+
}
37+
38+
for _, m := range methods {
39+
err := s.CollectUseCase(
40+
m,
41+
withRoute.RoutePattern(),
42+
handler.UseCase(),
43+
handler.HandlerTrait,
44+
)
45+
if err != nil {
46+
panic(err)
47+
}
3648
}
3749

3850
return h

web/_testdata/openapi.json

Lines changed: 140 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,145 @@
66
"version":"v1.0.0"
77
},
88
"paths":{
9+
"/a/{id}":{
10+
"delete":{
11+
"tags":["Album"],"summary":"Album By ID",
12+
"operationId":"rest/web_test.albumByID7",
13+
"parameters":[
14+
{"name":"locale","in":"query","schema":{"type":"string"}},
15+
{
16+
"name":"id","in":"path","required":true,"schema":{"type":"integer"}
17+
}
18+
],
19+
"responses":{
20+
"200":{
21+
"description":"OK",
22+
"content":{
23+
"application/json":{"schema":{"$ref":"#/components/schemas/WebTestAlbum"}}
24+
}
25+
}
26+
}
27+
},
28+
"get":{
29+
"tags":["Album"],"summary":"Album By ID",
30+
"operationId":"rest/web_test.albumByID4",
31+
"parameters":[
32+
{"name":"locale","in":"query","schema":{"type":"string"}},
33+
{
34+
"name":"id","in":"path","required":true,"schema":{"type":"integer"}
35+
}
36+
],
37+
"responses":{
38+
"200":{
39+
"description":"OK",
40+
"content":{
41+
"application/json":{"schema":{"$ref":"#/components/schemas/WebTestAlbum"}}
42+
}
43+
}
44+
}
45+
},
46+
"head":{
47+
"tags":["Album"],"summary":"Album By ID",
48+
"operationId":"rest/web_test.albumByID9",
49+
"parameters":[
50+
{"name":"locale","in":"query","schema":{"type":"string"}},
51+
{
52+
"name":"id","in":"path","required":true,"schema":{"type":"integer"}
53+
}
54+
],
55+
"responses":{"200":{"description":"OK"}}
56+
},
57+
"options":{
58+
"tags":["Album"],"summary":"Album By ID",
59+
"operationId":"rest/web_test.albumByID8",
60+
"parameters":[
61+
{"name":"locale","in":"query","schema":{"type":"string"}},
62+
{
63+
"name":"id","in":"path","required":true,"schema":{"type":"integer"}
64+
}
65+
],
66+
"responses":{
67+
"200":{
68+
"description":"OK",
69+
"content":{
70+
"application/json":{"schema":{"$ref":"#/components/schemas/WebTestAlbum"}}
71+
}
72+
}
73+
}
74+
},
75+
"patch":{
76+
"tags":["Album"],"summary":"Album By ID",
77+
"operationId":"rest/web_test.albumByID10",
78+
"parameters":[
79+
{"name":"locale","in":"query","schema":{"type":"string"}},
80+
{
81+
"name":"id","in":"path","required":true,"schema":{"type":"integer"}
82+
}
83+
],
84+
"responses":{
85+
"200":{
86+
"description":"OK",
87+
"content":{
88+
"application/json":{"schema":{"$ref":"#/components/schemas/WebTestAlbum"}}
89+
}
90+
}
91+
}
92+
},
93+
"post":{
94+
"tags":["Album"],"summary":"Album By ID",
95+
"operationId":"rest/web_test.albumByID6",
96+
"parameters":[
97+
{"name":"locale","in":"query","schema":{"type":"string"}},
98+
{
99+
"name":"id","in":"path","required":true,"schema":{"type":"integer"}
100+
}
101+
],
102+
"responses":{
103+
"200":{
104+
"description":"OK",
105+
"content":{
106+
"application/json":{"schema":{"$ref":"#/components/schemas/WebTestAlbum"}}
107+
}
108+
}
109+
}
110+
},
111+
"put":{
112+
"tags":["Album"],"summary":"Album By ID",
113+
"operationId":"rest/web_test.albumByID5",
114+
"parameters":[
115+
{"name":"locale","in":"query","schema":{"type":"string"}},
116+
{
117+
"name":"id","in":"path","required":true,"schema":{"type":"integer"}
118+
}
119+
],
120+
"responses":{
121+
"200":{
122+
"description":"OK",
123+
"content":{
124+
"application/json":{"schema":{"$ref":"#/components/schemas/WebTestAlbum"}}
125+
}
126+
}
127+
}
128+
},
129+
"trace":{
130+
"tags":["Album"],"summary":"Album By ID",
131+
"operationId":"rest/web_test.albumByID11",
132+
"parameters":[
133+
{"name":"locale","in":"query","schema":{"type":"string"}},
134+
{
135+
"name":"id","in":"path","required":true,"schema":{"type":"integer"}
136+
}
137+
],
138+
"responses":{
139+
"200":{
140+
"description":"OK",
141+
"content":{
142+
"application/json":{"schema":{"$ref":"#/components/schemas/WebTestAlbum"}}
143+
}
144+
}
145+
}
146+
}
147+
},
9148
"/albums":{
10149
"options":{
11150
"tags":["Album"],"summary":"Post Albums",
@@ -154,4 +293,4 @@
154293
}
155294
}
156295
}
157-
}
296+
}

web/service_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ func TestDefaultService(t *testing.T) {
7676
}),
7777
)
7878

79+
service.Handle("/a/{id}", nethttp.NewHandler(albumByID()))
80+
7981
rw := httptest.NewRecorder()
8082
r, err := http.NewRequest(http.MethodGet, "http://localhost/docs/openapi.json", nil)
8183
require.NoError(t, err)

0 commit comments

Comments
 (0)