Skip to content

Commit

Permalink
improve mapping function
Browse files Browse the repository at this point in the history
  • Loading branch information
technicallyty committed Jan 13, 2025
1 parent c103c74 commit 339e0e7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
2 changes: 1 addition & 1 deletion server/v2/api/grpcgateway/interceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ func createRegexMapping(logger log.Logger, annotationMapping map[string]string)
for annotation, queryInputName := range annotationMapping {
pattern, wildcardNames := patternToRegex(annotation)
if len(wildcardNames) == 0 {
if otherAnnotation, ok := annotationMapping[annotation]; ok {
if otherAnnotation, ok := seenPatterns[annotation]; ok {
// TODO: eventually we want this to error, but there is currently a duplicate in the protobuf.
// see: https://github.com/cosmos/cosmos-sdk/issues/23281
logger.Warn("duplicate HTTP annotation found", "annotation1", annotation, "annotation2", otherAnnotation, "query_input_name", queryInputName)
Expand Down
26 changes: 21 additions & 5 deletions server/v2/api/grpcgateway/interceptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,39 +20,55 @@ import (

func Test_createRegexMapping(t *testing.T) {
tests := []struct {
name string
annotations map[string]string
wantWarn bool
name string
annotations map[string]string
expectedRegex int
expectedSimple int
wantWarn bool
}{
{
name: "no annotations should not warn",
},
{
name: "expected correct amount of regex and simple matchers",
annotations: map[string]string{
"/foo/bar/baz": "",
"/foo/{bar}/baz": "",
"/foo/bar/bell": "",
},
expectedRegex: 1,
expectedSimple: 2,
},
{
name: "different annotations should not warn",
annotations: map[string]string{
"/foo/bar/{baz}": "",
"/crypto/{currency}": "",
},
expectedRegex: 2,
},
{
name: "duplicate annotations should warn",
annotations: map[string]string{
"/hello/{world}": "",
"/hello/{developers}": "",
},
wantWarn: true,
expectedRegex: 2,
wantWarn: true,
},
}
buf := bytes.NewBuffer(nil)
logger := log.NewLogger(buf)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
createRegexMapping(logger, tt.annotations)
regex, simple := createRegexMapping(logger, tt.annotations)
if tt.wantWarn {
require.NotEmpty(t, buf.String())
} else {
require.Empty(t, buf.String())
}
require.Equal(t, tt.expectedRegex, len(regex))
require.Equal(t, tt.expectedSimple, len(simple))
})
}
}
Expand Down

0 comments on commit 339e0e7

Please sign in to comment.