Skip to content

Commit 8eb0d70

Browse files
authored
Merge pull request #48 from matsuri-tech/feat/47
feat: endpointsのパスや名前が重複している場合、生成時にエラーを吐かせる
2 parents b327714 + 9af9696 commit 8eb0d70

File tree

2 files changed

+49
-5
lines changed

2 files changed

+49
-5
lines changed

endpoints.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,24 @@ func (e *endpoints) addFrontends(frontends ...string) {
3636
e.frontends = append(e.frontends, frontends...)
3737
}
3838

39+
func (e *endpoints) validate() error {
40+
// 重複したnameと、重複したpathとmethodの組み合わせがないかチェック
41+
names := map[string]struct{}{}
42+
paths := map[string]struct{}{}
43+
for _, v := range e.api {
44+
if _, ok := names[v.Name]; ok {
45+
return fmt.Errorf("duplicate name: %s", v.Name)
46+
}
47+
names[v.Name] = struct{}{}
48+
49+
if _, ok := paths[v.Path+v.Method]; ok {
50+
return fmt.Errorf("duplicate path and method: %s %s", v.Path, v.Method)
51+
}
52+
paths[v.Path+v.Method] = struct{}{}
53+
}
54+
return nil
55+
}
56+
3957
func (e *endpoints) generateJson() ([]byte, error) {
4058
endpoints := orderedmap.New()
4159
for _, v := range e.env {
@@ -80,6 +98,10 @@ func (e *endpoints) generateJson() ([]byte, error) {
8098
}
8199

82100
func (e *endpoints) generate(filename string) error {
101+
if err := e.validate(); err != nil {
102+
return err
103+
}
104+
83105
bs, err := e.generateJson()
84106
if err != nil {
85107
return err

endpoints_test.go

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func newRoute(e *echo.Echo) *EchoWrapper {
7777
Desc: "GET samples",
7878
})
7979
samples.GETTyped("/:id/another", sampleHandler.GetWithQuery, Desc{
80-
Name: "getSamplesWithQuery",
80+
Name: "getSamplesWithQueryAnother",
8181
Query: "yearMonth=2021-01",
8282
Desc: "GET samples",
8383
}, SampleModel{})
@@ -215,7 +215,7 @@ func TestEchoWrapper_GenerateOpenApi(t *testing.T) {
215215
"/samples/{id}/another": {
216216
"get": {
217217
"description": "GET samples",
218-
"operationId": "getSamplesWithQuery",
218+
"operationId": "getSamplesWithQueryAnother",
219219
"parameters": [
220220
{
221221
"description":"Generated by endpoints-go",
@@ -246,7 +246,7 @@ func TestEchoWrapper_GenerateOpenApi(t *testing.T) {
246246
"auth": []
247247
}
248248
],
249-
"summary": "getSamplesWithQuery"
249+
"summary": "getSamplesWithQueryAnother"
250250
}
251251
},
252252
"/samples": {
@@ -295,7 +295,7 @@ func TestEchoWrapper_GenerateOpenApi(t *testing.T) {
295295
]
296296
}`)
297297

298-
assert.JSONEq(t, string(expected), buf.String(), buf.String())
298+
assert.JSONEqf(t, string(expected), buf.String(), buf.String())
299299
}
300300

301301
func TestEchoWrapper_Generate(t *testing.T) {
@@ -318,6 +318,17 @@ func TestEchoWrapper_Generate(t *testing.T) {
318318
},
319319
"api": {
320320
"getSamplesWithQuery": {
321+
"authSchema": {
322+
"header": "",
323+
"type": ""
324+
},
325+
"desc": "GET samples",
326+
"method": "GET",
327+
"path": "samples/:id?yearMonth=2021-01",
328+
"request": null,
329+
"response": null
330+
},
331+
"getSamplesWithQueryAnother": {
321332
"authSchema": {
322333
"header": "",
323334
"type": ""
@@ -354,6 +365,17 @@ func TestEchoWrapper_Generate(t *testing.T) {
354365
},
355366
"api": {
356367
"getSamplesWithQuery": {
368+
"path": "samples/:id?yearMonth=2021-01",
369+
"desc": "GET samples",
370+
"method": "GET",
371+
"authSchema": {
372+
"type": "",
373+
"header": ""
374+
},
375+
"request": null,
376+
"response": null
377+
},
378+
"getSamplesWithQueryAnother": {
357379
"authSchema": {
358380
"header": "",
359381
"type": ""
@@ -467,5 +489,5 @@ func TestEchoWrapper_Generate(t *testing.T) {
467489
}
468490
}`)
469491

470-
assert.JSONEq(t, string(expected), string(actual), string(actual))
492+
assert.JSONEqf(t, string(expected), string(actual), string(actual))
471493
}

0 commit comments

Comments
 (0)