Skip to content

Commit ac41185

Browse files
tgengachew22
authored andcommitted
Fallback to JSON name when matching URL parameter. (#450)
* Fallback to JSON name when matching URL parameter. * Add tests using JSON name for URL param parsing.
1 parent 8bec008 commit ac41185

File tree

2 files changed

+87
-24
lines changed

2 files changed

+87
-24
lines changed

runtime/query.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@ func fieldByProtoName(m reflect.Value, name string) (reflect.Value, *proto.Prope
113113
if p.OrigName == name {
114114
return m.FieldByName(p.Name), p, nil
115115
}
116+
if p.JSONName == name {
117+
return m.FieldByName(p.Name), p, nil
118+
}
116119
}
117120
return reflect.Value{}, nil, nil
118121
}

runtime/query_test.go

Lines changed: 84 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,37 @@ func TestPopulateParameters(t *testing.T) {
6161
TimestampValue: timePb,
6262
},
6363
},
64+
{
65+
values: url.Values{
66+
"floatValue": {"1.5"},
67+
"doubleValue": {"2.5"},
68+
"int64Value": {"-1"},
69+
"int32Value": {"-2"},
70+
"uint64Value": {"3"},
71+
"uint32Value": {"4"},
72+
"boolValue": {"true"},
73+
"stringValue": {"str"},
74+
"repeatedValue": {"a", "b", "c"},
75+
"enumValue": {"1"},
76+
"repeatedEnum": {"1", "2", "0"},
77+
"timestampValue": {timeStr},
78+
},
79+
filter: utilities.NewDoubleArray(nil),
80+
want: &proto3Message{
81+
FloatValue: 1.5,
82+
DoubleValue: 2.5,
83+
Int64Value: -1,
84+
Int32Value: -2,
85+
Uint64Value: 3,
86+
Uint32Value: 4,
87+
BoolValue: true,
88+
StringValue: "str",
89+
RepeatedValue: []string{"a", "b", "c"},
90+
EnumValue: EnumValue_Y,
91+
RepeatedEnum: []EnumValue{EnumValue_Y, EnumValue_Z, EnumValue_X},
92+
TimestampValue: timePb,
93+
},
94+
},
6495
{
6596
values: url.Values{
6697
"enum_value": {"EnumValue_Z"},
@@ -101,6 +132,35 @@ func TestPopulateParameters(t *testing.T) {
101132
RepeatedEnum: []EnumValue{EnumValue_Y, EnumValue_Z, EnumValue_X},
102133
},
103134
},
135+
{
136+
values: url.Values{
137+
"floatValue": {"1.5"},
138+
"doubleValue": {"2.5"},
139+
"int64Value": {"-1"},
140+
"int32Value": {"-2"},
141+
"uint64Value": {"3"},
142+
"uint32Value": {"4"},
143+
"boolValue": {"true"},
144+
"stringValue": {"str"},
145+
"repeatedValue": {"a", "b", "c"},
146+
"enumValue": {"1"},
147+
"repeatedEnum": {"1", "2", "0"},
148+
},
149+
filter: utilities.NewDoubleArray(nil),
150+
want: &proto2Message{
151+
FloatValue: proto.Float32(1.5),
152+
DoubleValue: proto.Float64(2.5),
153+
Int64Value: proto.Int64(-1),
154+
Int32Value: proto.Int32(-2),
155+
Uint64Value: proto.Uint64(3),
156+
Uint32Value: proto.Uint32(4),
157+
BoolValue: proto.Bool(true),
158+
StringValue: proto.String("str"),
159+
RepeatedValue: []string{"a", "b", "c"},
160+
EnumValue: EnumValue_Y,
161+
RepeatedEnum: []EnumValue{EnumValue_Y, EnumValue_Z, EnumValue_X},
162+
},
163+
},
104164
{
105165
values: url.Values{
106166
"nested.nested.nested.repeated_value": {"a", "b", "c"},
@@ -368,20 +428,20 @@ func TestPopulateQueryParametersWithInvalidNestedParameters(t *testing.T) {
368428
}
369429

370430
type proto3Message struct {
371-
Nested *proto2Message `protobuf:"bytes,1,opt,name=nested" json:"nested,omitempty"`
372-
NestedNonNull proto2Message `protobuf:"bytes,15,opt,name=nested_non_null" json:"nested_non_null,omitempty"`
373-
FloatValue float32 `protobuf:"fixed32,2,opt,name=float_value" json:"float_value,omitempty"`
374-
DoubleValue float64 `protobuf:"fixed64,3,opt,name=double_value" json:"double_value,omitempty"`
375-
Int64Value int64 `protobuf:"varint,4,opt,name=int64_value" json:"int64_value,omitempty"`
376-
Int32Value int32 `protobuf:"varint,5,opt,name=int32_value" json:"int32_value,omitempty"`
377-
Uint64Value uint64 `protobuf:"varint,6,opt,name=uint64_value" json:"uint64_value,omitempty"`
378-
Uint32Value uint32 `protobuf:"varint,7,opt,name=uint32_value" json:"uint32_value,omitempty"`
379-
BoolValue bool `protobuf:"varint,8,opt,name=bool_value" json:"bool_value,omitempty"`
380-
StringValue string `protobuf:"bytes,9,opt,name=string_value" json:"string_value,omitempty"`
381-
RepeatedValue []string `protobuf:"bytes,10,rep,name=repeated_value" json:"repeated_value,omitempty"`
431+
Nested *proto2Message `protobuf:"bytes,1,opt,name=nested,json=nested" json:"nested,omitempty"`
432+
NestedNonNull proto2Message `protobuf:"bytes,15,opt,name=nested_non_null,json=nestedNonNull" json:"nested_non_null,omitempty"`
433+
FloatValue float32 `protobuf:"fixed32,2,opt,name=float_value,json=floatValue" json:"float_value,omitempty"`
434+
DoubleValue float64 `protobuf:"fixed64,3,opt,name=double_value,json=doubleValue" json:"double_value,omitempty"`
435+
Int64Value int64 `protobuf:"varint,4,opt,name=int64_value,json=int64Value" json:"int64_value,omitempty"`
436+
Int32Value int32 `protobuf:"varint,5,opt,name=int32_value,json=int32Value" json:"int32_value,omitempty"`
437+
Uint64Value uint64 `protobuf:"varint,6,opt,name=uint64_value,json=uint64Value" json:"uint64_value,omitempty"`
438+
Uint32Value uint32 `protobuf:"varint,7,opt,name=uint32_value,json=uint32Value" json:"uint32_value,omitempty"`
439+
BoolValue bool `protobuf:"varint,8,opt,name=bool_value,json=boolValue" json:"bool_value,omitempty"`
440+
StringValue string `protobuf:"bytes,9,opt,name=string_value,json=stringValue" json:"string_value,omitempty"`
441+
RepeatedValue []string `protobuf:"bytes,10,rep,name=repeated_value,json=repeatedValue" json:"repeated_value,omitempty"`
382442
EnumValue EnumValue `protobuf:"varint,11,opt,name=enum_value,json=enumValue,enum=runtime_test_api.EnumValue" json:"enum_value,omitempty"`
383-
RepeatedEnum []EnumValue `protobuf:"varint,12,rep,packed,name=repeated_enum,json=repeated_enum,enum=runtime_test_api.EnumValue" json:"repeated_enum,omitempty"`
384-
TimestampValue *timestamp.Timestamp `protobuf:"bytes,16,opt,name=timestamp_value" json:"timestamp_value,omitempty"`
443+
RepeatedEnum []EnumValue `protobuf:"varint,12,rep,packed,name=repeated_enum,json=repeatedEnum,enum=runtime_test_api.EnumValue" json:"repeated_enum,omitempty"`
444+
TimestampValue *timestamp.Timestamp `protobuf:"bytes,16,opt,name=timestamp_value,json=timestampValue" json:"timestamp_value,omitempty"`
385445
OneofValue proto3Message_OneofValue `protobuf_oneof:"oneof_value"`
386446
}
387447

@@ -501,18 +561,18 @@ func _proto3Message_OneofSizer(msg proto.Message) (n int) {
501561
}
502562

503563
type proto2Message struct {
504-
Nested *proto3Message `protobuf:"bytes,1,opt,name=nested" json:"nested,omitempty"`
505-
FloatValue *float32 `protobuf:"fixed32,2,opt,name=float_value" json:"float_value,omitempty"`
506-
DoubleValue *float64 `protobuf:"fixed64,3,opt,name=double_value" json:"double_value,omitempty"`
507-
Int64Value *int64 `protobuf:"varint,4,opt,name=int64_value" json:"int64_value,omitempty"`
508-
Int32Value *int32 `protobuf:"varint,5,opt,name=int32_value" json:"int32_value,omitempty"`
509-
Uint64Value *uint64 `protobuf:"varint,6,opt,name=uint64_value" json:"uint64_value,omitempty"`
510-
Uint32Value *uint32 `protobuf:"varint,7,opt,name=uint32_value" json:"uint32_value,omitempty"`
511-
BoolValue *bool `protobuf:"varint,8,opt,name=bool_value" json:"bool_value,omitempty"`
512-
StringValue *string `protobuf:"bytes,9,opt,name=string_value" json:"string_value,omitempty"`
513-
RepeatedValue []string `protobuf:"bytes,10,rep,name=repeated_value" json:"repeated_value,omitempty"`
564+
Nested *proto3Message `protobuf:"bytes,1,opt,name=nested,json=nested" json:"nested,omitempty"`
565+
FloatValue *float32 `protobuf:"fixed32,2,opt,name=float_value,json=floatValue" json:"float_value,omitempty"`
566+
DoubleValue *float64 `protobuf:"fixed64,3,opt,name=double_value,json=doubleValue" json:"double_value,omitempty"`
567+
Int64Value *int64 `protobuf:"varint,4,opt,name=int64_value,json=int64Value" json:"int64_value,omitempty"`
568+
Int32Value *int32 `protobuf:"varint,5,opt,name=int32_value,json=int32Value" json:"int32_value,omitempty"`
569+
Uint64Value *uint64 `protobuf:"varint,6,opt,name=uint64_value,json=uint64Value" json:"uint64_value,omitempty"`
570+
Uint32Value *uint32 `protobuf:"varint,7,opt,name=uint32_value,json=uint32Value" json:"uint32_value,omitempty"`
571+
BoolValue *bool `protobuf:"varint,8,opt,name=bool_value,json=boolValue" json:"bool_value,omitempty"`
572+
StringValue *string `protobuf:"bytes,9,opt,name=string_value,json=stringValue" json:"string_value,omitempty"`
573+
RepeatedValue []string `protobuf:"bytes,10,rep,name=repeated_value,json=repeatedValue" json:"repeated_value,omitempty"`
514574
EnumValue EnumValue `protobuf:"varint,11,opt,name=enum_value,json=enumValue,enum=runtime_test_api.EnumValue" json:"enum_value,omitempty"`
515-
RepeatedEnum []EnumValue `protobuf:"varint,12,rep,packed,name=repeated_enum,json=repeated_enum,enum=runtime_test_api.EnumValue" json:"repeated_enum,omitempty"`
575+
RepeatedEnum []EnumValue `protobuf:"varint,12,rep,packed,name=repeated_enum,json=repeatedEnum,enum=runtime_test_api.EnumValue" json:"repeated_enum,omitempty"`
516576
XXX_unrecognized []byte `json:"-"`
517577
}
518578

0 commit comments

Comments
 (0)