Skip to content

Commit

Permalink
fix: detect unknown operation ids (#201)
Browse files Browse the repository at this point in the history
  • Loading branch information
byashimov authored Dec 11, 2024
1 parent 5655a6d commit cd7820d
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 43 deletions.
10 changes: 8 additions & 2 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,14 @@ vars:

tasks:
fmt-imports:
# macOS requires to install GNU sed first. Use `brew install gnu-sed` to install it.
# It has to be added to PATH as `sed` command, to replace default BSD sed.
# See `brew info gnu-sed` for more details on how to add it to PATH.
# /^import ($$/: starts with "import ("
# /^)/: ends with ")"
# /^[[:space:]]*$$/: empty lines
cmds:
- find . -type f -name '*.go' -exec sed -zi 's/(?<== `\s+)"\n\+\t"/"\n"/g' {} +
- find . -type f -name '*.go' -exec sed -i '/^import ($$/,/^)/ {/^[[:space:]]*$$/d}' {} +
- goimports -local "github.com/aiven/go-client-codegen" -w .
get-openapi-spec:
cmds:
Expand All @@ -22,4 +28,4 @@ tasks:
- task: go-generate
test:
cmds:
- go test -v ./...
- go test -v
3 changes: 1 addition & 2 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,7 @@ func TestServiceCreateErrorsRetries(t *testing.T) {
}

ctx := context.Background()
project := "aiven-project"
out, err := c.ServiceCreate(ctx, project, in)
out, err := c.ServiceCreate(ctx, "aiven-project", in)
assert.Nil(t, out)
assert.Equal(t, err.Error(), tt.ErrorExpect)

Expand Down
2 changes: 0 additions & 2 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,6 @@ Organization:
- OrganizationAuthenticationConfigGet
- OrganizationAuthenticationConfigUpdate
- OrganizationGet
- OrganizationProjectsList
- OrganizationUpdate
- PermissionsGet
- PermissionsUpdate
Expand All @@ -200,7 +199,6 @@ OrganizationUser:
- OrganizationUserList
- OrganizationUserPasswordReset
- OrganizationUserRevokeToken
- OrganizationUserSet
- OrganizationUserTokensList
- OrganizationUserUpdate
PostgreSQL:
Expand Down
11 changes: 2 additions & 9 deletions generator/config_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"strings"
)

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

if len(duplicates) > 0 {
return fmt.Errorf("Duplicate endpoints found in config: %v", keys(duplicates))
return fmt.Errorf("duplicate endpoints found in config: %v", strings.Join(sortedKeys(duplicates), ", "))
}
return nil
}

func keys(m map[string]struct{}) []string {
keys := make([]string, 0, len(m))
for k := range m {
keys = append(keys, k)
}
return keys
}
33 changes: 19 additions & 14 deletions generator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,15 @@ func exec() error {
return err
}

// To validate all operation ids in the config exist in the OpenAPI spec
// OperationID => Package name
configOperationIDs := make(map[string]string)
for pkg, idList := range config {
for _, id := range idList {
configOperationIDs[id] = pkg
}
}

pkgs := make(map[string][]*Path)
for path := range doc.Paths {
v := doc.Paths[path]
Expand All @@ -114,23 +123,15 @@ func exec() error {
p.Method = strings.ToUpper(meth)
p.ID = p.OperationID

var pkg string
outer:
for k, idList := range config {
for _, id := range idList {
if p.ID == id {
pkg = k

break outer
}
}
}

if pkg == "" {
log.Error().Msgf("%q id not found in config!", p.ID)
pkg, ok := configOperationIDs[p.ID]
if !ok {
log.Warn().Msgf("%q id not found in config!", p.ID)
continue
}

// Removes the operation id from the map to see which are not used
delete(configOperationIDs, p.ID)

pkgs[pkg] = append(pkgs[pkg], p)
params := make([]*Parameter, 0)

Expand All @@ -155,6 +156,10 @@ func exec() error {
}
}

if len(configOperationIDs) > 0 {
return fmt.Errorf("config has unused operation ids: %s", strings.Join(sortedKeys(configOperationIDs), ", "))
}

ctx := jen.Id("ctx").Qual("context", "Context")
doer := jen.Comment(doerName + " http client").Line().Type().Id(doerName).Interface(
jen.Id("Do").Params(
Expand Down
4 changes: 2 additions & 2 deletions handler/account/account.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions handler/billinggroup/billinggroup.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion handler/kafka/kafka.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions handler/project/project.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion handler/user/user.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit cd7820d

Please sign in to comment.