Skip to content

Commit 6cbc1b0

Browse files
slessardsl255051
andauthored
openapi3filter: parse integers with strconv.ParseInt instead of ParseFloat (#711)
Co-authored-by: Steve Lessard <steve.lessard@teradata.com>
1 parent 7413c27 commit 6cbc1b0

3 files changed

Lines changed: 25 additions & 11 deletions

File tree

openapi3filter/issue625_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ paths:
7272
name: "failed allof object array",
7373
spec: allOfArraySpec,
7474
req: `/items?test=1.2,3.1`,
75-
errStr: `parameter "test" in query has an error: Error at "/0": value "1.2" must be an integer`,
75+
errStr: `parameter "test" in query has an error: path 0: value 1.2: an invalid integer: invalid syntax`,
7676
},
7777
{
7878
name: "success oneof object array",

openapi3filter/req_resp_decoder.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -895,7 +895,14 @@ func parsePrimitive(raw string, schema *openapi3.SchemaRef) (interface{}, error)
895895
}
896896
switch schema.Value.Type {
897897
case "integer":
898-
v, err := strconv.ParseFloat(raw, 64)
898+
if schema.Value.Format == "int32" {
899+
v, err := strconv.ParseInt(raw, 0, 32)
900+
if err != nil {
901+
return nil, &ParseError{Kind: KindInvalidFormat, Value: raw, Reason: "an invalid " + schema.Value.Type, Cause: err.(*strconv.NumError).Err}
902+
}
903+
return int32(v), nil
904+
}
905+
v, err := strconv.ParseInt(raw, 0, 64)
899906
if err != nil {
900907
return nil, &ParseError{Kind: KindInvalidFormat, Value: raw, Reason: "an invalid " + schema.Value.Type, Cause: err.(*strconv.NumError).Err}
901908
}

openapi3filter/req_resp_decoder_test.go

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ func TestDecodeParameter(t *testing.T) {
175175
name: "integer",
176176
param: &openapi3.Parameter{Name: "param", In: "path", Schema: integerSchema},
177177
path: "/1",
178-
want: float64(1),
178+
want: int64(1),
179179
found: true,
180180
},
181181
{
@@ -456,7 +456,7 @@ func TestDecodeParameter(t *testing.T) {
456456
name: "integer",
457457
param: &openapi3.Parameter{Name: "param", In: "query", Schema: integerSchema},
458458
query: "param=1",
459-
want: float64(1),
459+
want: int64(1),
460460
found: true,
461461
},
462462
{
@@ -522,7 +522,7 @@ func TestDecodeParameter(t *testing.T) {
522522
name: "anyofSchema integer",
523523
param: &openapi3.Parameter{Name: "param", In: "query", Schema: anyofSchema},
524524
query: "param=1",
525-
want: float64(1),
525+
want: int64(1),
526526
found: true,
527527
},
528528
{
@@ -548,7 +548,7 @@ func TestDecodeParameter(t *testing.T) {
548548
name: "oneofSchema int",
549549
param: &openapi3.Parameter{Name: "param", In: "query", Schema: oneofSchema},
550550
query: "param=1122",
551-
want: float64(1122),
551+
want: int64(1122),
552552
found: true,
553553
},
554554
{
@@ -724,7 +724,7 @@ func TestDecodeParameter(t *testing.T) {
724724
name: "integer",
725725
param: &openapi3.Parameter{Name: "X-Param", In: "header", Schema: integerSchema},
726726
header: "X-Param:1",
727-
want: float64(1),
727+
want: int64(1),
728728
found: true,
729729
},
730730
{
@@ -835,6 +835,13 @@ func TestDecodeParameter(t *testing.T) {
835835
want: map[string]interface{}{"id": "foo", "name": "bar"},
836836
found: true,
837837
},
838+
{
839+
name: "valid integer prop",
840+
param: &openapi3.Parameter{Name: "X-Param", In: "header", Schema: integerSchema},
841+
header: "X-Param:88",
842+
found: true,
843+
want: int64(88),
844+
},
838845
{
839846
name: "invalid integer prop",
840847
param: &openapi3.Parameter{Name: "X-Param", In: "header", Schema: objectOf("foo", integerSchema)},
@@ -893,7 +900,7 @@ func TestDecodeParameter(t *testing.T) {
893900
name: "integer",
894901
param: &openapi3.Parameter{Name: "X-Param", In: "cookie", Schema: integerSchema},
895902
cookie: "X-Param:1",
896-
want: float64(1),
903+
want: int64(1),
897904
found: true,
898905
},
899906
{
@@ -1180,7 +1187,7 @@ func TestDecodeBody(t *testing.T) {
11801187
WithProperty("a", openapi3.NewStringSchema()).
11811188
WithProperty("b", openapi3.NewIntegerSchema()).
11821189
WithProperty("c", openapi3.NewArraySchema().WithItems(openapi3.NewStringSchema())),
1183-
want: map[string]interface{}{"a": "a1", "b": float64(10), "c": []interface{}{"c1", "c2"}},
1190+
want: map[string]interface{}{"a": "a1", "b": int64(10), "c": []interface{}{"c1", "c2"}},
11841191
},
11851192
{
11861193
name: "urlencoded space delimited",
@@ -1193,7 +1200,7 @@ func TestDecodeBody(t *testing.T) {
11931200
encoding: map[string]*openapi3.Encoding{
11941201
"c": {Style: openapi3.SerializationSpaceDelimited, Explode: boolPtr(false)},
11951202
},
1196-
want: map[string]interface{}{"a": "a1", "b": float64(10), "c": []interface{}{"c1", "c2"}},
1203+
want: map[string]interface{}{"a": "a1", "b": int64(10), "c": []interface{}{"c1", "c2"}},
11971204
},
11981205
{
11991206
name: "urlencoded pipe delimited",
@@ -1206,7 +1213,7 @@ func TestDecodeBody(t *testing.T) {
12061213
encoding: map[string]*openapi3.Encoding{
12071214
"c": {Style: openapi3.SerializationPipeDelimited, Explode: boolPtr(false)},
12081215
},
1209-
want: map[string]interface{}{"a": "a1", "b": float64(10), "c": []interface{}{"c1", "c2"}},
1216+
want: map[string]interface{}{"a": "a1", "b": int64(10), "c": []interface{}{"c1", "c2"}},
12101217
},
12111218
{
12121219
name: "multipart",

0 commit comments

Comments
 (0)