Skip to content

Commit 9ea13db

Browse files
authored
Merge pull request #22 from matsuri-tech/fix/issue-#19
fix(endpoints): separate QueryString from path
2 parents 0ab200e + 3827f56 commit 9ea13db

File tree

2 files changed

+175
-0
lines changed

2 files changed

+175
-0
lines changed

endpoints.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,27 @@ func (e *endpoints) generateOpenApiSchema(config OpenApiGeneratorConfig) (openap
117117
}
118118

119119
parameters := openapi3.Parameters{}
120+
121+
if strings.Contains(path, "?") {
122+
splits := strings.Split(path, "?")
123+
// pathを書き換えているので注意
124+
path = splits[0]
125+
queryStrings := strings.TrimPrefix(splits[1], "?")
126+
127+
for _, frag := range strings.Split(queryStrings, "&") {
128+
keyValue := strings.Split(frag, "=")
129+
parameters = append(parameters, &openapi3.ParameterRef{
130+
Value: &openapi3.Parameter{
131+
Name: keyValue[0],
132+
In: "query",
133+
Description: description,
134+
Required: true,
135+
Schema: &openapi3.SchemaRef{Value: &openapi3.Schema{Type: "string"}},
136+
},
137+
})
138+
}
139+
}
140+
120141
for _, frag := range strings.Split(path, "/") {
121142
if !strings.HasPrefix(frag, ":") {
122143
continue

endpoints_test.go

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
package endpoints
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"github.com/labstack/echo/v4"
7+
"reflect"
8+
"testing"
9+
)
10+
11+
type SampleHandler struct{}
12+
13+
func (handler SampleHandler) GetWithQuery(c echo.Context) error {
14+
return nil
15+
}
16+
func NewSampleHandler() SampleHandler {
17+
return SampleHandler{}
18+
}
19+
20+
func TestEchoWrapper_GenerateOpenApi(t *testing.T) {
21+
22+
e := echo.New()
23+
24+
ew := NewEchoWrapper(e)
25+
ew.AddEnv(
26+
Env{
27+
Version: "v1",
28+
Domain: Domain{
29+
Local: "http://localhost:8000",
30+
LocalDev: "https://local-dev.hoge.com",
31+
Dev: "https://dev.hoge.com",
32+
Prod: "https://hoge.com",
33+
},
34+
},
35+
Env{
36+
Version: "v2",
37+
Domain: Domain{
38+
Local: "http://localhost:8000",
39+
LocalDev: "https://local-dev.hoge.com",
40+
Dev: "https://v2.dev.hoge.com",
41+
Prod: "https://v2.hoge.com",
42+
},
43+
},
44+
)
45+
samples := ew.Group("/samples")
46+
sampleHandler := NewSampleHandler()
47+
samples.GET("/:id", sampleHandler.GetWithQuery, Desc{
48+
Name: "getSamplesWithQuery",
49+
Query: "yearMonth=2021-01",
50+
Desc: "GET samples",
51+
})
52+
buf := new(bytes.Buffer)
53+
conf := OpenApiGeneratorConfig{}
54+
55+
if err := ew.endpoints.generateOpenApiJson(buf, conf); err != nil {
56+
t.Errorf("err: %v", err)
57+
}
58+
59+
var result interface{}
60+
if err := json.Unmarshal(buf.Bytes(), &result); err != nil {
61+
t.Errorf("err: %v", err)
62+
}
63+
64+
expected := []byte(`
65+
{
66+
"components": {
67+
"securitySchemes": {
68+
"auth": {
69+
"in": "header",
70+
"type": "apiKey"
71+
}
72+
}
73+
},
74+
"info": {
75+
"title": "",
76+
"version": ""
77+
},
78+
"openapi": "3.0.0",
79+
"paths": {
80+
"/samples/{id}": {
81+
"get": {
82+
"description": "GET samples",
83+
"operationId": "getSamplesWithQuery",
84+
"parameters": [
85+
{
86+
"description":"Generated by endpoints-go",
87+
"in":"query",
88+
"name":"yearMonth",
89+
"required":true,
90+
"schema": {
91+
"type": "string"
92+
}
93+
},
94+
{
95+
"description": "Generated by endpoints-go",
96+
"in": "path",
97+
"name": "id",
98+
"required": true,
99+
"schema": {
100+
"type": "string"
101+
}
102+
}
103+
],
104+
"responses": {
105+
"200": {
106+
"description": "Generated by endpoints-go"
107+
}
108+
},
109+
"security": [
110+
{
111+
"auth": []
112+
}
113+
],
114+
"summary": "getSamplesWithQuery"
115+
}
116+
}
117+
},
118+
"servers": [
119+
{
120+
"description": "v1 at local",
121+
"url": "http://localhost:8000"
122+
},
123+
{
124+
"description": "v1 at dev",
125+
"url": "https://dev.hoge.com"
126+
},
127+
{
128+
"description": "v1 at prod",
129+
"url": "https://hoge.com"
130+
},
131+
{
132+
"description": "v2 at local",
133+
"url": "http://localhost:8000"
134+
},
135+
{
136+
"description": "v2 at dev",
137+
"url": "https://v2.dev.hoge.com"
138+
},
139+
{
140+
"description": "v2 at prod",
141+
"url": "https://v2.hoge.com"
142+
}
143+
]
144+
}`)
145+
var expectedJson interface{}
146+
if err := json.Unmarshal(expected, &expectedJson); err != nil {
147+
t.Errorf("err: %v", err)
148+
}
149+
150+
if !reflect.DeepEqual(expectedJson, result) {
151+
t.Errorf("error. \nresult: %v, \nexpected: %v", result, expectedJson)
152+
}
153+
154+
}

0 commit comments

Comments
 (0)