Skip to content

Instantly share code, notes, and snippets.

@bajtos
Last active June 8, 2016 11:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bajtos/8afaa9d1ed7d7b410c76ec2095a8a992 to your computer and use it in GitHub Desktop.
Save bajtos/8afaa9d1ed7d7b410c76ec2095a8a992 to your computer and use it in GitHub Desktop.
Coercion in strong-remoting

Coercion in strong-remoting - issues & changes

Changes introduced by v2.16.1

json body - array of booleans: non-boolean values (null, 0, 1, 2) are coerced to booleans. String items "true"/"false" are converted to true/false.

json body - arrays: scalar values are coerced to arrays, e.g. request body 1 is converted to [1].

form data - arrays: malformed JSON-like value is treated as verbatim string. E.g. ?arg={malformed} is converted to ['{malformed}'].

json form - array values are wrongly coerced when the argument is of scalar type:

  • booleans:

-json form - boolean - optional {"arg":[]} {"value":true} +json form - boolean - optional {"arg":[]} {"value":[]} -json form - boolean - optional {"arg":[1,2]} {"value":true} +json form - boolean - optional {"arg":[1,2]} {"value":[true,true]}


 - numbers:

    ```diff
-json form - number - optional	{"arg":[]}	{"value":0}
+json form - number - optional	{"arg":[]}	{"value":[]}
-json form - number - optional	{"arg":[1,2]}	{"error":400}
+json form - number - optional	{"arg":[1,2]}	{"value":[1,2]}

json form - object type no longer accepts an array

-json form - object - optional	{"arg":["text"]}	{"value":["text"]}
+json form - object - optional	{"arg":["text"]}	{"error":400}

Changes introduced by v2.16.2

Object type accepts an array again

-json form - object - optional	{"arg":["text"]}	{"error":400}
+json form - object - optional	{"arg":["text"]}	{"value":["text"]}

Changes introduced by v2.16.3

Arrays coerce items

-json body - array of booleans - optional       [[]]    {"value":[[]]}
+json body - array of booleans - optional       [[]]    {"value":[true]}

-json body - array of numbers - optional        null    {"value":[null]}
+json body - array of numbers - optional        null    {"value":[0]}
-json body - array of numbers - optional        false   {"value":[false]}
+json body - array of numbers - optional        false   {"value":[0]}
-json body - array of numbers - optional        [null]  {"value":[null]}
+json body - array of numbers - optional        [null]  {"value":[0]}

-json body - array of strings - optional        0       {"value":[0]}
+json body - array of strings - optional        0       {"value":["0"]}
-json body - array of strings - optional        1       {"value":[1]}
+json body - array of strings - optional        1       {"value":["1"]}

-json body - array of dates - optional  [0]     {"value":[0]}
+json body - array of dates - optional  [0]     {"value":["1970-01-01T00:00:00.000Z"]}

etc.

Null values are rejected for object arguments

-json body - object - required  null    {"value":null}
+json body - object - required  null    {"error":400}
-json body - object - optional  null    {"value":null}
+json body - object - optional  null    {"error":400}

For scalar types, values of incorrect type (array, other scalar type) are coerced to scalar for scalar types

-json form - boolean - optional	{"arg":[]}	{"value":[]}
+json form - boolean - optional	{"arg":[]}	{"value":true}
-json form - boolean - optional	{"arg":[1,2]}	{"value":[true,true]}
+json form - boolean - optional	{"arg":[1,2]}	{"value":true}

-json form - number - optional  {"arg":false}   {"value":false}
+json form - number - optional  {"arg":false}   {"value":0}

-json form - string - optional  {"arg":null}    {"value":null}
+json form - string - optional  {"arg":null}    {"value":"null"}
-json form - string - optional  {"arg":false}   {"value":false}
+json form - string - optional  {"arg":false}   {"value":"false"}

Date coerces numbers as timestamps

-json form - date - required    {"arg":0}       0
+json form - date - required    {"arg":0}       <Date: 1970-01-01T00:00:00.000Z>

but also 

-json form - date - optional    {"arg":1.234e+30}       1.234e+30
+json form - date - optional    {"arg":1.234e+30}       <Invalid Date>

Scalar values are rejected for object type

-json form - object - optional  {"arg":0}       0
+json form - object - optional  {"arg":0}       <HTTP Error 400>
etc.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment