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

Shorten UUID for relationship in include request. #985

Closed
gmarquet opened this issue Feb 21, 2017 · 6 comments
Closed

Shorten UUID for relationship in include request. #985

gmarquet opened this issue Feb 21, 2017 · 6 comments

Comments

@gmarquet
Copy link

gmarquet commented Feb 21, 2017

I got the current set up on 0.9.0:

class Api::V1::UserResource < JSONAPI::Resource
  has_many :addresses, class_name: "Address"
  attributes :name
end

class Api::V1::AddressResource < JSONAPI::Resource
  has_one :user , class_name: "User"
  attributes :street
end

# config/initializers/jsonapi_resources.rb
JSONAPI.configure do |config|
  config.resource_key_type = :uuid
end

And my request with the include parameter look like that :

 // https://docker.dev/api/v1/addresses?include=user
{
  "data": [
    {
      "id": "002f0ce3-57fc-459c-9d7c-2dfbb56a7f4d",
      "type": "addresses",
      "links": {
        "self": "https://docker.dev/api/v1/addresses/002f0ce3-57fc-459c-9d7c-2dfbb56a7f4d"
      },
      "attributes": {
        "street": "621 QUATER, impasse Aurélie Weiss"
      },
      "relationships": {
        "user": {
          "links": {
            "self": "https://docker.dev/api/v1/addresses/002f0ce3-57fc-459c-9d7c-2dfbb56a7f4d/relationships/user",
            "related": "https://docker.dev/api/v1/addresses/002f0ce3-57fc-459c-9d7c-2dfbb56a7f4d/user"
          },
          "data": {
            "type": "users",
            "id": "1" // the real ID/UUID is 1af0bdc6-078f-465e-a6d2-f615b412eda7
          }
        }
      }
    },
  ],
  "links": {
    "first": "https://docker.dev/api/v1/addresses?include=user&page%5Bnumber%5D=1&page%5Bsize%5D=10",
    "next": "https://docker.dev/api/v1/addresses?include=user&page%5Bnumber%5D=2&page%5Bsize%5D=10",
    "last": "https://docker.dev/api/v1/addresses?include=user&page%5Bnumber%5D=32&page%5Bsize%5D=10"
  }
}

As you can see the UUID is not properly set. It seems to be cut before the first letter.
90c544c0-1f43-4a71-971e-0fdfc2dae054 => 90
4eb84daf-2134-4598-9635-ffda7e780e90 => 4
ff38d15b-0dc0-484b-a38a-327d4977fddc => 0

And the root include property is missing.

Does anyone have experience the same problems?

@gmarquet gmarquet changed the title Shorten UUID in include resquest. Shorten UUID for relationship in include request. Feb 21, 2017
@senid231
Copy link
Contributor

@gmarquet, exmaple according to your description works fine in my bug report template.
Can you reproduce the issue using it?

@awmichel
Copy link

I came across a similar issue last week, related to rails/rails#28044. Not sure when a fix will come on the Rails side, but until then I made a branch of 0.9.x with a small patch here: awmichel/jsonapi-resources@c6aa622.

I'll open a PR against jsonapi-resources with the fix as well. 👍

@gmarquet
Copy link
Author

i replaced t.references :user by t.uuid :user_id in the address migration file and it work as expected now. Odd?

@senid231
Copy link
Contributor

senid231 commented Feb 21, 2017

reproduces by changing t.uuid :user_id to t.references :user
seems that references creates column with incorrect data type

address = Address.take
# D, [2017-02-21T17:30:54.710066 #31387] DEBUG -- :   Address Load (0.2ms)  SELECT  "addresses".* FROM "addresses" LIMIT $1  [["LIMIT", 1]]
# => #<Address id: "c6ecb4f7-e004-437c-b7f6-3d0e048b55ea", user_id: 9984831, street: "main st">

address.class.columns.detect { |c| c.name == 'user_id' }
# => #<ActiveRecord::ConnectionAdapters::PostgreSQLColumn:0x000000047530d8 @name="user_id", @table_name="addresses", @sql_type_metadata=#<ActiveRecord::ConnectionAdapters::SqlTypeMetadata:0x00000004753510 @sql_type="integer", @type=:integer, @limit=nil, @precision=nil, @scale=nil>, @null=true, @default=nil, @default_function=nil, @collation=nil, @comment=nil>

@senid231
Copy link
Contributor

senid231 commented Feb 23, 2017

So that its ActiveRecord bug. I think issue can be closed

@lgebhardt
Copy link
Member

Closing for now as it does look to be an ActiveRecord bug. Let's reopen if this turns out to be a bug in JR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants