Help with RouteParam parsing

270 views
Skip to first unread message

achap

unread,
Sep 19, 2016, 6:12:47 PM9/19/16
to finatra-users
Hi All,

I have route param in one of my routes.  When I try to use a custom case class to parse this request I get bad request error stating that the field is required depending on how I name the route param.  Adding the field to my query params seems to fix this.  The problem changes depending on what I name my route param.  

Here is my route declaration:

get(s"/status/:statusId") { request: GetStatusRequest =>
 
// handle my request
}



Here is my request case class:

import javax.inject.Inject

import com.twitter.finagle.http.Request
import com.twitter.finatra.request.RouteParam

case class GetStatusRequest(
 
@RouteParam statusId: String,
 
@Inject request: Request
)




Here is the error from curling my endpoint:
< HTTP/1.1 400 Bad Request
< Content-Type: application/json; charset=utf-8
< Server: Finatra
< Date: Mon, 19 Sep 2016 21:44:48 GMT
< Content-Length: 43
<
* Connection #0 to host localhost left intact

{"errors":["status_id: field is required"]}


If I change "statusId" in both places to "id" or "pickle" the route param is parsed fine... if it's named "clientId" or "statusId" (probably others) I get bad request errors.  

What am I missing here? seems like strange behavior.  

I'm using 
scalaVersion=2.11.8
finatraVersion=2.4.0



Taylor Leese

unread,
Sep 19, 2016, 6:29:14 PM9/19/16
to achap, finatra-users
Take a look at http://twitter.github.io/finatra/user-guide/build-new-http-server/controller.html#controllers-and-routing. I think this is what you're looking for:

The case class field names should match the request parameters or use the @JsonProperty annotation to specify the JSON field name in the case class (see: example). APropertyNamingStrategy can be configured to handle common name substitutions (e.g. snake_case or camelCase). By default, snake_case is used (defaults are set inFinatraJacksonModule).

--
You received this message because you are subscribed to the Google Groups "finatra-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to finatra-users+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

achap

unread,
Sep 19, 2016, 6:34:40 PM9/19/16
to finatra-users, achap...@gmail.com

The case class field names should match the request parameters
Didn't I satisfy this part???  I named my route param ":statusId" and the same in my case class.  If I change the name in my route param to ":pickle" and "pickle" in my case class it works fine.






 
To unsubscribe from this group and stop receiving emails from it, send an email to finatra-user...@googlegroups.com.

achap

unread,
Sep 19, 2016, 6:44:54 PM9/19/16
to finatra-users, achap...@gmail.com
strange... ok... so if my route param is named ":status_id" and my case class param is named "statusId" it works.  Also if both are named "status_id" it works... but not if they are both camel case.

achap

unread,
Sep 20, 2016, 6:36:54 PM9/20/16
to finatra-users
bottom line is default behavior is route params need to be snake case.. and finatra is generous (and confusing) that it can map snake case route params into camel case params of your request case class.

move along... nothing to see here :)

Christopher Coco

unread,
Sep 20, 2016, 8:47:09 PM9/20/16
to achap, finatra-users
Hi,

As Taylor pointed out in the documentation, this behavior is controlled by the FinatraJacksonModule which if you do not change/modify defaults to snake_case for parsing the route param (jackson is used for route param parsing). Finatra is not doing anything confusing as this is how standard object mapping works. JSON or path params sent in snake_case are mapped to a camelCase field in code.


The case class field names should match the request parameters or use the @JsonProperty annotation to specify the JSON field name in the case class (see: example). A PropertyNamingStrategy can be configured to handle common name substitutions (e.g. snake_case or camelCase). By default, snake_case is used (defaults are set inFinatraJacksonModule).

 If you want camelCase <=> camelCase you would provide your own jacksonModule with a different property naming strategy in your HttpServer by overriding the def jacksonModule

Thanks,
-c

--
You received this message because you are subscribed to the Google Groups "finatra-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to finatra-users+unsubscribe@googlegroups.com.

achap

unread,
Sep 20, 2016, 9:45:53 PM9/20/16
to finatra-users, achap...@gmail.com
yup... thanks!!! took a look at all of that... makes sense. I think I was just tired when I ran into this and expected the naming style in the route definition to be respected in the case class params.  

Interestingly it will map your snake_case route param into either snake_case or camelCase param in your case class.




To unsubscribe from this group and stop receiving emails from it, send an email to finatra-user...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages