plexus / yaks

Ruby library for building hypermedia APIs
http://rubygems.org/gems/yaks
MIT License
236 stars 26 forks source link

`type` just wants to be pluralised #121

Open Odaeus opened 8 years ago

Odaeus commented 8 years ago

If I'm using JSON API as the formatter and I set a type:

class BookMapper < Yaks::Mapper
  type "book"
end

I'd expect the output to be:

{
  "type": "book",
  ...
}

But instead the type is pluralised to "books" and I can't see a way to stop it.

The JSON API spec says:

Note: This spec is agnostic about inflection rules, so the value of type can be either plural or singular. However, the same value should be used consistently throughout an implementation.

Though I'm not sure why, as not even Rails has ever pluralised the type of single objects.

plexus commented 8 years ago

Hi @Odaeus, nice to see you here.

The JSON-API implementation is mostly contributed by @janko-m and @groyoh. I don't immediately have time to work on it, but I'll happily take contributions.

It seems the pluralization is indeed hard-coded.

      # @param [Yaks::Resource] resource
      # @return [Hash]
      def serialize_resource(resource)
        result = {}
        result[:type] = pluralize(resource.type)
        result[:id]   = resource[:id].to_s if resource[:id]

I'm guessing pluralize calls dup internally, hence your other issue. (this is really just a guess)

This could be made configurable, you can have a look at the HAL serializer for how to use format_options

     yaks = Yaks.new do
       format_options :json_api, {pluralize_resource_names: false}
     end