0
votes

Is there some way to exclude the namespace part of the model name in the root key?

I used Active Model Serializer version 0.9 before and my JSON representation of a User at localhost:3000/api/v1/users/1 was something like:

{ "user": { "id": 1, "first_name": "Foo", "last_name": "Bar" } }

I recently upgraded to version 0.10 and now I get the namespace included in the root key:

{ "api/v1/user": { "id": 1,"first_name": "Foo", "last_name": "Bar" } }

I want the root key to be like before, that is "user" instead of "api/v1/user".

Edit 1

I am using the :json adapter.

Edit 2

I opened an issue at the repository on GitHub. Let's see what they say about it.

Edit 3

The documentation for Active Model Serializers have been updated. It explains how to override the root key.

1

1 Answers

1
votes

You must be using the JSON adapter and from looking at the code you have a couple of options:

  1. Specify the root during rendering of your resource as

    render json: @user, root: "user"
    
  2. It seems like the root is fetched by calling the serializer's json_key method so if you override that method you should be able to define the root at the serializer level.

  3. You may be able to override initialize on your serializer to call super with adding the root option since it's assigned on initialize of the Serializer base class https://github.com/rails-api/active_model_serializers/blob/b4e2ac300cdb4424549660a175c8cb777194b065/lib/active_model/serializer.rb#L109