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

no examples of FromRequestUnmarshaller in docs or codebase #1325

Open
ktoso opened this issue Aug 4, 2017 · 5 comments
Open

no examples of FromRequestUnmarshaller in docs or codebase #1325

ktoso opened this issue Aug 4, 2017 · 5 comments
Labels
1 - triaged Tickets that are safe to pick up for contributing in terms of likeliness of being accepted help wanted Identifies issues that the core team will likely not have time to work on small t:docs Issues related to the documentation t:http:marshalling

Comments

@ktoso
Copy link
Member

ktoso commented Aug 4, 2017

Issue by kornelc
Saturday Jul 16, 2016 at 22:56 GMT
Originally opened as akka/akka#20972


I'm trying to extract a case class with json4s from the entity (form submission) and realized that there are no examples anywhere on how to create these unmarshallers. The doc just has 'implicit val orderUM: FromRequestUnmarshaller[Order] = ???' as an example which doesn't help much unless somebody is already familiar with unmarshallers, which I am not at this point. It would be great to include a case class unmarshalling example in the docs.

@ktoso ktoso added 1 - triaged Tickets that are safe to pick up for contributing in terms of likeliness of being accepted help wanted Identifies issues that the core team will likely not have time to work on small t:docs Issues related to the documentation t:http:marshalling labels Aug 4, 2017
@ktoso
Copy link
Member Author

ktoso commented Aug 4, 2017

Comment by levkhomich
Sunday Jul 17, 2016 at 04:38 GMT


If request entity is all you need to extract Order, you are probably looking for FromEntityUnmarshaller.

Consider this example:

  def parseWithJson4s[T](jsonStr: String): T = {
    // here goes your json4s logic
    parse(jsonStr).camelizeKeys.extract[T]
  }
  implicit def json4sEntityUnmarshaller[T: Manifest]: FromEntityUnmarshaller[T] = {
    Unmarshaller.stringUnmarshaller.map(parseWithJson4s)
  }

In most cases, boilerplate provided by Unmarshaller object is enough.

If you really need to create unmarshaller from HttpRequest, you can do following:

  case class CustomFromRequestUnmarshaller[T](parse: String => T) extends FromRequestUnmarshaller[T] {
    override def apply(request: HttpRequest)(implicit ec: ExecutionContext, m: Materializer): Future[T] = {
      // here you have access to all parts of request, not just entity
      Unmarshaller.stringUnmarshaller(request.entity).map(parse)
    }
  }
  implicit def json4sRequestUnmarshaller = CustomFromRequestUnmarshaller(parseWithJson4s)

@ktoso
Copy link
Member Author

ktoso commented Aug 4, 2017

Comment by kornelc
Sunday Jul 17, 2016 at 13:26 GMT


Thanks for the quick response. That worked. I only needed the string as you said.

@ktoso
Copy link
Member Author

ktoso commented Aug 4, 2017

Comment by johanandren
Monday Jul 18, 2016 at 15:44 GMT


For reference this is the docs for custom unmarshallers:
http://doc.akka.io/docs/akka/2.4.8/scala/http/common/unmarshalling.html#custom-unmarshallers

Would be great with a PR with a case class unmarshaller as you say, probably the most common use case when needing a custom unmarshaller.

@ktoso
Copy link
Member Author

ktoso commented Aug 4, 2017

Comment by kornelc
Monday Jul 18, 2016 at 22:08 GMT


Thanks!

@ktoso
Copy link
Member Author

ktoso commented Aug 4, 2017

Comment by kornelc
Tuesday Jul 19, 2016 at 12:36 GMT


I reopened this because I think I'm supposed to keep it open based on the last comment. Are we supposed to keep labeled issues open?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
1 - triaged Tickets that are safe to pick up for contributing in terms of likeliness of being accepted help wanted Identifies issues that the core team will likely not have time to work on small t:docs Issues related to the documentation t:http:marshalling
Projects
None yet
Development

No branches or pull requests

1 participant