From 339e0e75a09f5728e20fd570463b50295ed08f2e Mon Sep 17 00:00:00 2001 From: tyler <48813565+technicallyty@users.noreply.github.com> Date: Sun, 12 Jan 2025 16:20:32 -0800 Subject: [PATCH] improve mapping function --- server/v2/api/grpcgateway/interceptor.go | 2 +- server/v2/api/grpcgateway/interceptor_test.go | 26 +++++++++++++++---- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/server/v2/api/grpcgateway/interceptor.go b/server/v2/api/grpcgateway/interceptor.go index 5cc620b2ef46..18fa3185f25c 100644 --- a/server/v2/api/grpcgateway/interceptor.go +++ b/server/v2/api/grpcgateway/interceptor.go @@ -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) diff --git a/server/v2/api/grpcgateway/interceptor_test.go b/server/v2/api/grpcgateway/interceptor_test.go index b33dc1e5054a..4e8b23c513e2 100644 --- a/server/v2/api/grpcgateway/interceptor_test.go +++ b/server/v2/api/grpcgateway/interceptor_test.go @@ -20,19 +20,32 @@ 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", @@ -40,19 +53,22 @@ func Test_createRegexMapping(t *testing.T) { "/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)) }) } }