Skip to content

Commit cd7820d

Browse files
authored
fix: detect unknown operation ids (#201)
1 parent 5655a6d commit cd7820d

File tree

10 files changed

+43
-43
lines changed

10 files changed

+43
-43
lines changed

Taskfile.yml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,14 @@ vars:
55

66
tasks:
77
fmt-imports:
8+
# macOS requires to install GNU sed first. Use `brew install gnu-sed` to install it.
9+
# It has to be added to PATH as `sed` command, to replace default BSD sed.
10+
# See `brew info gnu-sed` for more details on how to add it to PATH.
11+
# /^import ($$/: starts with "import ("
12+
# /^)/: ends with ")"
13+
# /^[[:space:]]*$$/: empty lines
814
cmds:
9-
- find . -type f -name '*.go' -exec sed -zi 's/(?<== `\s+)"\n\+\t"/"\n"/g' {} +
15+
- find . -type f -name '*.go' -exec sed -i '/^import ($$/,/^)/ {/^[[:space:]]*$$/d}' {} +
1016
- goimports -local "github.com/aiven/go-client-codegen" -w .
1117
get-openapi-spec:
1218
cmds:
@@ -22,4 +28,4 @@ tasks:
2228
- task: go-generate
2329
test:
2430
cmds:
25-
- go test -v ./...
31+
- go test -v

client_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,7 @@ func TestServiceCreateErrorsRetries(t *testing.T) {
187187
}
188188

189189
ctx := context.Background()
190-
project := "aiven-project"
191-
out, err := c.ServiceCreate(ctx, project, in)
190+
out, err := c.ServiceCreate(ctx, "aiven-project", in)
192191
assert.Nil(t, out)
193192
assert.Equal(t, err.Error(), tt.ErrorExpect)
194193

config.yaml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,6 @@ Organization:
183183
- OrganizationAuthenticationConfigGet
184184
- OrganizationAuthenticationConfigUpdate
185185
- OrganizationGet
186-
- OrganizationProjectsList
187186
- OrganizationUpdate
188187
- PermissionsGet
189188
- PermissionsUpdate
@@ -200,7 +199,6 @@ OrganizationUser:
200199
- OrganizationUserList
201200
- OrganizationUserPasswordReset
202201
- OrganizationUserRevokeToken
203-
- OrganizationUserSet
204202
- OrganizationUserTokensList
205203
- OrganizationUserUpdate
206204
PostgreSQL:

generator/config_check.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"fmt"
5+
"strings"
56
)
67

78
func checkDuplicateEndpoints(config map[string][]string) error {
@@ -19,15 +20,7 @@ func checkDuplicateEndpoints(config map[string][]string) error {
1920
}
2021

2122
if len(duplicates) > 0 {
22-
return fmt.Errorf("Duplicate endpoints found in config: %v", keys(duplicates))
23+
return fmt.Errorf("duplicate endpoints found in config: %v", strings.Join(sortedKeys(duplicates), ", "))
2324
}
2425
return nil
2526
}
26-
27-
func keys(m map[string]struct{}) []string {
28-
keys := make([]string, 0, len(m))
29-
for k := range m {
30-
keys = append(keys, k)
31-
}
32-
return keys
33-
}

generator/main.go

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,15 @@ func exec() error {
102102
return err
103103
}
104104

105+
// To validate all operation ids in the config exist in the OpenAPI spec
106+
// OperationID => Package name
107+
configOperationIDs := make(map[string]string)
108+
for pkg, idList := range config {
109+
for _, id := range idList {
110+
configOperationIDs[id] = pkg
111+
}
112+
}
113+
105114
pkgs := make(map[string][]*Path)
106115
for path := range doc.Paths {
107116
v := doc.Paths[path]
@@ -114,23 +123,15 @@ func exec() error {
114123
p.Method = strings.ToUpper(meth)
115124
p.ID = p.OperationID
116125

117-
var pkg string
118-
outer:
119-
for k, idList := range config {
120-
for _, id := range idList {
121-
if p.ID == id {
122-
pkg = k
123-
124-
break outer
125-
}
126-
}
127-
}
128-
129-
if pkg == "" {
130-
log.Error().Msgf("%q id not found in config!", p.ID)
126+
pkg, ok := configOperationIDs[p.ID]
127+
if !ok {
128+
log.Warn().Msgf("%q id not found in config!", p.ID)
131129
continue
132130
}
133131

132+
// Removes the operation id from the map to see which are not used
133+
delete(configOperationIDs, p.ID)
134+
134135
pkgs[pkg] = append(pkgs[pkg], p)
135136
params := make([]*Parameter, 0)
136137

@@ -155,6 +156,10 @@ func exec() error {
155156
}
156157
}
157158

159+
if len(configOperationIDs) > 0 {
160+
return fmt.Errorf("config has unused operation ids: %s", strings.Join(sortedKeys(configOperationIDs), ", "))
161+
}
162+
158163
ctx := jen.Id("ctx").Qual("context", "Context")
159164
doer := jen.Comment(doerName + " http client").Line().Type().Id(doerName).Interface(
160165
jen.Id("Do").Params(

handler/account/account.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

handler/billinggroup/billinggroup.go

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

handler/kafka/kafka.go

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

handler/project/project.go

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

handler/user/user.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)