redstone-dart / redstone

A metadata driven microframework for Dart.
http://redstone-dart.github.io/redstone
MIT License
342 stars 42 forks source link

v0.6 Error decoding body form to objects that contain integer fields #135

Closed cgarciae closed 8 years ago

cgarciae commented 9 years ago

I have this test class and route that tries to parse a form containing a integer value

class TestClass {
  @Field() int number;
}

@Group('/test')
class TestRoutes {
  @Route('/int-post-param')
  testIntPostParam(@Decode(from: const[app.FORM]) TestClass test) {
    return test.number += 1;
  }
}

But this error arises as a result

.TestRoutes.testIntPostParam: Error parsing 'test' parameter: MapperException: TestClass#number: type 'String' is not a subtype of type 'int' of 'value'.

It look like its trying to decode a Map to a TestClass but fails because the maps "number" field is a String.

cgarciae commented 9 years ago

Looking at the code it seems that request.bodys values are always Strings.

cgarciae commented 9 years ago

How about this?

try {
        return decode(data, paramType);
      } catch (e) {
        try {
          return mirrors.reflectClass(paramType).newInstance(#fromStringMap, [data]).reflectee;
        }
        catch (e2) {
          throw new ErrorResponse(
              400, "$handlerName: Error parsing '$paramName' parameter: $e\n\nError using 'fromJson' constructor: $e2");
        }
      }
cgarciae commented 9 years ago

It might be nicer if the decoder tried to parse String to int before failing in general.

Pacane commented 8 years ago

Is this still relevant? Also, shouldn't this issue be opened on the plugin's repo directly [or is it a bug with redstone specifically?]