Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make it easier to deserialize a request body as a Model #109

thosakwe opened this issue Jun 12, 2019 · 3 comments


Copy link

@thosakwe thosakwe commented Jun 12, 2019

Currently, to read the contents of the request as a model, one must either:

  • Manually deserialize it
  • Read it via dependency injection

Neither is honestly all that ideal.

Thread below...


This comment has been minimized.

Copy link
Member Author

@thosakwe thosakwe commented Jun 12, 2019

Idea 1 - Support a new annotation for auto-deserializing bodies

Take this example:

class BlogController extends Controller {
  Future<List<Blog>> search(@body Search search) => ...

Questions that arise:

  • How would Angel know how to make a Search? It wouldn't:
    • Maybe it can look for a fromJson constructor. angel_serialize would then need to be modified to support this.
    • The @Body() constructor can take a Codec - these are generated by Angel. It might be easier to just take a FutureOr<T> Function(Map), though.
  • What if I want a List?

This is verbose:

Future<List<Blog>> search(@Body(SearchSerializer.fromMap) Search search) => ...
Future<List<Blog>> search(@Body(searchSerializer) Search search) => ...

However, you could actually use the Codec itself as an annotation instead (still a bit verbose):

Future<List<Blog>> search(@searchSerializer Search search) => ...

Yet unmentioned is this, the cleanest, without any annotation:

Future<List<Blog>> search(Search search) => ...

This would be simpler to implement within the current dependency injection system, without users having to register a factory, etc.

The semantics/requirements:

  • Force the body to be a Map. If you are injecting List<Search>, force it to be a List<Map>.
  • angel_serialize will generate a T.fromJson constructor.

This is maybe the best solution I can think of right now, without breaking things.


This comment has been minimized.

Copy link
Member Author

@thosakwe thosakwe commented Jun 12, 2019

Alternatively, a solution without relying on any sort of reflection:

var post = await req.decodeBody(postSerializer); // Post
var post = await req.deserializeBody(PostSerializer.fromMap); // Post

The thing, though, is that this is basically the same as manually deserializing it already.


This comment has been minimized.

Copy link
Member Author

@thosakwe thosakwe commented Jul 17, 2019

Just pending some unit tests.

@thosakwe thosakwe closed this Jul 17, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
1 participant
You can’t perform that action at this time.