api2go icon indicating copy to clipboard operation
api2go copied to clipboard

Model routes should be dasherized, not camelized

Open drauschenbach opened this issue 6 years ago • 5 comments

When the NewAPI().AddResource() mechanism is used to register a model with a name such as MyStruct, the route that is registered should be the dasherized my-struct. Instead, the route that is registered is the camelized myStruct. This isn't compatible with Ember Data. And in fact, Ember does not even have a convenient pathForType widget to camelize and pluralize a dasherized route, which makes this a pesky interop issue that requires hacking.

drauschenbach avatar Dec 23 '17 00:12 drauschenbach

The JSONAPISerializer expects the backend to return a JSON API Document that follows the JSON API specification and the conventions of the examples found on http://jsonapi.org/format. This means all type names should be pluralized and attribute and relationship names should be dash-cased.

(from Ember 2.17.0 customizing-serializers)

Reading through https://github.com/manyminds/api2go/issues/15 and trying to figure out where this discrepancy crept in.

drauschenbach avatar Dec 23 '17 00:12 drauschenbach

You're right. But in the meantime, it is still possible to give all the fields in your structs custom names. So you could do it like in our example and give dasherized names to your fields, see:

https://github.com/manyminds/api2go/blob/e7b693844a6ff1798ca2a84d8481884ca81d191a/examples/model/model_user.go#L10-L18

Maybe we should make this a configurable option whether people want to use the standard dasherized or camel version.

wwwdata avatar Dec 24 '17 12:12 wwwdata

Agreed -- I'm already using dasherized field names and that is working great. This issue is specifically about the parent struct name and how it's converted to a URL/route name.

drauschenbach avatar Dec 24 '17 16:12 drauschenbach

My colleague just pointed out EntityNamer, which looks like a way to work around this on the server side.

drauschenbach avatar Jan 08 '18 14:01 drauschenbach

If you are using ember-data, I would suggest not to dasherize your fieldNames and types, as you are adding more work for ember-data serializer - specifically to normalize your response. Ember data internally stores data in json:api format, where fieldNames are camelCase and types are singular. Ember-data Serializer was meant to be a stop gap for you to align your API, if you have control over it, which sounds like you do.

xomaczar avatar Aug 30 '18 14:08 xomaczar