Skip to content

Commit

Permalink
openapi3filter: parse integers with strconv.ParseInt instead of Parse…
Browse files Browse the repository at this point in the history
…Float (#711)

Co-authored-by: Steve Lessard <steve.lessard@teradata.com>
  • Loading branch information
slessard and Steve Lessard committed Dec 16, 2022
1 parent 7413c27 commit 6cbc1b0
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 11 deletions.
2 changes: 1 addition & 1 deletion openapi3filter/issue625_test.go
Expand Up @@ -72,7 +72,7 @@ paths:
name: "failed allof object array",
spec: allOfArraySpec,
req: `/items?test=1.2,3.1`,
errStr: `parameter "test" in query has an error: Error at "/0": value "1.2" must be an integer`,
errStr: `parameter "test" in query has an error: path 0: value 1.2: an invalid integer: invalid syntax`,
},
{
name: "success oneof object array",
Expand Down
9 changes: 8 additions & 1 deletion openapi3filter/req_resp_decoder.go
Expand Up @@ -895,7 +895,14 @@ func parsePrimitive(raw string, schema *openapi3.SchemaRef) (interface{}, error)
}
switch schema.Value.Type {
case "integer":
v, err := strconv.ParseFloat(raw, 64)
if schema.Value.Format == "int32" {
v, err := strconv.ParseInt(raw, 0, 32)
if err != nil {
return nil, &ParseError{Kind: KindInvalidFormat, Value: raw, Reason: "an invalid " + schema.Value.Type, Cause: err.(*strconv.NumError).Err}
}
return int32(v), nil
}
v, err := strconv.ParseInt(raw, 0, 64)
if err != nil {
return nil, &ParseError{Kind: KindInvalidFormat, Value: raw, Reason: "an invalid " + schema.Value.Type, Cause: err.(*strconv.NumError).Err}
}
Expand Down
25 changes: 16 additions & 9 deletions openapi3filter/req_resp_decoder_test.go
Expand Up @@ -175,7 +175,7 @@ func TestDecodeParameter(t *testing.T) {
name: "integer",
param: &openapi3.Parameter{Name: "param", In: "path", Schema: integerSchema},
path: "/1",
want: float64(1),
want: int64(1),
found: true,
},
{
Expand Down Expand Up @@ -456,7 +456,7 @@ func TestDecodeParameter(t *testing.T) {
name: "integer",
param: &openapi3.Parameter{Name: "param", In: "query", Schema: integerSchema},
query: "param=1",
want: float64(1),
want: int64(1),
found: true,
},
{
Expand Down Expand Up @@ -522,7 +522,7 @@ func TestDecodeParameter(t *testing.T) {
name: "anyofSchema integer",
param: &openapi3.Parameter{Name: "param", In: "query", Schema: anyofSchema},
query: "param=1",
want: float64(1),
want: int64(1),
found: true,
},
{
Expand All @@ -548,7 +548,7 @@ func TestDecodeParameter(t *testing.T) {
name: "oneofSchema int",
param: &openapi3.Parameter{Name: "param", In: "query", Schema: oneofSchema},
query: "param=1122",
want: float64(1122),
want: int64(1122),
found: true,
},
{
Expand Down Expand Up @@ -724,7 +724,7 @@ func TestDecodeParameter(t *testing.T) {
name: "integer",
param: &openapi3.Parameter{Name: "X-Param", In: "header", Schema: integerSchema},
header: "X-Param:1",
want: float64(1),
want: int64(1),
found: true,
},
{
Expand Down Expand Up @@ -835,6 +835,13 @@ func TestDecodeParameter(t *testing.T) {
want: map[string]interface{}{"id": "foo", "name": "bar"},
found: true,
},
{
name: "valid integer prop",
param: &openapi3.Parameter{Name: "X-Param", In: "header", Schema: integerSchema},
header: "X-Param:88",
found: true,
want: int64(88),
},
{
name: "invalid integer prop",
param: &openapi3.Parameter{Name: "X-Param", In: "header", Schema: objectOf("foo", integerSchema)},
Expand Down Expand Up @@ -893,7 +900,7 @@ func TestDecodeParameter(t *testing.T) {
name: "integer",
param: &openapi3.Parameter{Name: "X-Param", In: "cookie", Schema: integerSchema},
cookie: "X-Param:1",
want: float64(1),
want: int64(1),
found: true,
},
{
Expand Down Expand Up @@ -1180,7 +1187,7 @@ func TestDecodeBody(t *testing.T) {
WithProperty("a", openapi3.NewStringSchema()).
WithProperty("b", openapi3.NewIntegerSchema()).
WithProperty("c", openapi3.NewArraySchema().WithItems(openapi3.NewStringSchema())),
want: map[string]interface{}{"a": "a1", "b": float64(10), "c": []interface{}{"c1", "c2"}},
want: map[string]interface{}{"a": "a1", "b": int64(10), "c": []interface{}{"c1", "c2"}},
},
{
name: "urlencoded space delimited",
Expand All @@ -1193,7 +1200,7 @@ func TestDecodeBody(t *testing.T) {
encoding: map[string]*openapi3.Encoding{
"c": {Style: openapi3.SerializationSpaceDelimited, Explode: boolPtr(false)},
},
want: map[string]interface{}{"a": "a1", "b": float64(10), "c": []interface{}{"c1", "c2"}},
want: map[string]interface{}{"a": "a1", "b": int64(10), "c": []interface{}{"c1", "c2"}},
},
{
name: "urlencoded pipe delimited",
Expand All @@ -1206,7 +1213,7 @@ func TestDecodeBody(t *testing.T) {
encoding: map[string]*openapi3.Encoding{
"c": {Style: openapi3.SerializationPipeDelimited, Explode: boolPtr(false)},
},
want: map[string]interface{}{"a": "a1", "b": float64(10), "c": []interface{}{"c1", "c2"}},
want: map[string]interface{}{"a": "a1", "b": int64(10), "c": []interface{}{"c1", "c2"}},
},
{
name: "multipart",
Expand Down

0 comments on commit 6cbc1b0

Please sign in to comment.