Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/jsonapi/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ def parse_single_replace_operation(data, keys)
end

type = data[:type]
if type.nil? || type != @resource_klass._type.to_s
if type.nil? || type != format_key(@resource_klass._type).to_s
raise JSONAPI::Exceptions::ParameterMissing.new(:type)
end

Expand Down
16 changes: 5 additions & 11 deletions lib/jsonapi/resource_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,23 +114,17 @@ def process_primary(source, requested_associations)

resource = source
id = resource.id
# ToDo: See if this is actually needed
# if already_serialized?(@primary_class_name, id)
# set_primary(@primary_class_name, id)
# end

add_included_object(@primary_class_name, id, object_hash(source, requested_associations), true)
end
end

# Returns a serialized hash for the source model, with
# Returns a serialized hash for the source model
def object_hash(source, requested_associations)
obj_hash = attribute_hash(source)
links = links_hash(source, requested_associations)

# ToDo: Do we format these required keys
obj_hash[format_key('type')] = format_value(source.class._type.to_s, :default, source)
obj_hash[format_key('id')] ||= format_value(source.id, :id, source)
obj_hash['type'] = format_key(source.class._type.to_s)
obj_hash['id'] ||= format_value(source.id, :id, source)
obj_hash.merge!({links: links}) unless links.empty?
return obj_hash
end
Expand Down Expand Up @@ -250,7 +244,7 @@ def has_one_linkage(source, association)
linkage = {}
linkage_id = foreign_key_value(source, association)
if linkage_id
linkage[:type] = format_route(association.type)
linkage[:type] = format_key(association.type)
linkage[:id] = linkage_id
else
linkage = nil
Expand All @@ -262,7 +256,7 @@ def has_many_linkage(source, association)
linkage = []
linkage_ids = foreign_key_value(source, association)
linkage_ids.each do |linkage_id|
linkage.append({type: format_route(association.type), id: linkage_id})
linkage.append({type: format_key(association.type), id: linkage_id})
end
linkage
end
Expand Down
101 changes: 101 additions & 0 deletions test/fixtures/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,35 @@
t.integer :author_id
t.timestamps null: false
end

create_table :customers, force: true do |t|
t.string :name
end

create_table :purchase_orders, force: true do |t|
t.date :order_date
t.date :requested_delivery_date
t.date :delivery_date
t.integer :customer_id
t.string :delivery_name
t.string :delivery_address_1
t.string :delivery_address_2
t.string :delivery_city
t.string :delivery_state
t.string :delivery_postal_code
t.float :delivery_fee
t.float :tax
t.float :total
t.timestamps null: false
end

create_table :line_items, force: true do |t|
t.integer :purchase_order_id
t.string :part_number
t.string :quantity
t.float :item_cost
t.timestamps null: false
end
end

### MODELS
Expand Down Expand Up @@ -239,7 +268,15 @@ def add(breed)
def remove(id)
@breeds.delete(id)
end
end

class CustomerOrder < ActiveRecord::Base
end

class PurchaseOrder < ActiveRecord::Base
end

class LineItem < ActiveRecord::Base
end

### PORO Data - don't do this in a production app
Expand Down Expand Up @@ -369,6 +406,28 @@ class ExpenseEntriesController < JSONAPI::ResourceController
class IsoCurrenciesController < JSONAPI::ResourceController
end
end

module V6
class CustomersController < JSONAPI::ResourceController
end

class PurchaseOrdersController < JSONAPI::ResourceController
end

class LineItemsController < JSONAPI::ResourceController
end
end

module V7
class CustomersController < JSONAPI::ResourceController
end

class PurchaseOrdersController < JSONAPI::ResourceController
end

class LineItemsController < JSONAPI::ResourceController
end
end
end

### RESOURCES
Expand Down Expand Up @@ -721,6 +780,48 @@ def fetchable_fields
end
end

module Api
module V6
class CustomerResource < JSONAPI::Resource
attribute :name

has_many :purchase_orders
end

class PurchaseOrderResource < JSONAPI::Resource
attribute :order_date
attribute :requested_delivery_date
attribute :delivery_date
attribute :delivery_name
attribute :delivery_address_1
attribute :delivery_address_2
attribute :delivery_city
attribute :delivery_state
attribute :delivery_postal_code
attribute :delivery_fee
attribute :tax
attribute :total

has_one :customer
has_many :line_items
end

class LineItemResource < JSONAPI::Resource
attribute :part_number
attribute :quantity
attribute :item_cost

has_one :purchase_order
end
end

module V7
CustomerResource = V6::CustomerResource.dup
PurchaseOrderResource = V6::PurchaseOrderResource.dup
LineItemResource = V6::LineItemResource.dup
end
end

warn 'start testing Name Collisions'
# The name collisions only emmit warnings. Exceptions would change the flow of the tests

Expand Down
7 changes: 7 additions & 0 deletions test/fixtures/customers.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
xyz_corp:
id: 1
name: XYZ Corporation

abc_corp:
id: 2
name: ABC Corporation
11 changes: 11 additions & 0 deletions test/fixtures/line_items.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
po_1_li_1:
purchase_order_id: 1
part_number: 556324
quantity: 1
item_cost: 45.67

po_1_li_2:
purchase_order_id: 1
part_number: 79324231A
quantity: 3
item_cost: 19.99
11 changes: 11 additions & 0 deletions test/fixtures/purchase_orders.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
po_1:
id: 1
requested_delivery_date:
delivery_date: nil
customer_id: 1

po_2:
id: 2
requested_delivery_date:
delivery_date: nil
customer_id: 1
113 changes: 113 additions & 0 deletions test/integration/requests/request_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class RequestTest < ActionDispatch::IntegrationTest

def setup
JSONAPI.configuration.json_key_format = :underscored_key
JSONAPI.configuration.route_format = :underscored_route
end

def after_teardown
Expand Down Expand Up @@ -451,4 +452,116 @@ def test_flow_link_has_many_self_link_put
})
end

def test_flow_self_formatted_route_1
JSONAPI.configuration.route_format = :dasherized_route
JSONAPI.configuration.json_key_format = :dasherized_key
get '/api/v6/purchase-orders'
assert_equal 200, status
po_1 = json_response['data'][0]
assert_equal 'purchase-orders', json_response['data'][0]['type']

get po_1['links']['self']
assert_equal 200, status
assert_hash_equals po_1, json_response['data']
end

def test_flow_self_formatted_route_2
JSONAPI.configuration.route_format = :underscored_route
JSONAPI.configuration.json_key_format = :dasherized_key
get '/api/v7/purchase_orders'
assert_equal 200, status
assert_equal 'purchase-orders', json_response['data'][0]['type']

po_1 = json_response['data'][0]

get po_1['links']['self']
assert_equal 200, status
assert_hash_equals po_1, json_response['data']
end

def test_flow_self_formatted_route_3
JSONAPI.configuration.route_format = :underscored_route
JSONAPI.configuration.json_key_format = :underscored_key
get '/api/v7/purchase_orders'
assert_equal 200, status
assert_equal 'purchase_orders', json_response['data'][0]['type']

po_1 = json_response['data'][0]

get po_1['links']['self']
assert_equal 200, status
assert_hash_equals po_1, json_response['data']
end

def test_post_formatted_keys
JSONAPI.configuration.route_format = :dasherized_route
JSONAPI.configuration.json_key_format = :dasherized_key
post '/api/v6/purchase-orders',
{
'data' => {
'delivery-name' => 'ASDFG Corp',
'type' => 'purchase-orders'
}
}.to_json, "CONTENT_TYPE" => JSONAPI::MEDIA_TYPE

assert_equal 201, status
end

def test_post_formatted_keys_different_route_key_1
JSONAPI.configuration.route_format = :dasherized_route
JSONAPI.configuration.json_key_format = :underscored_key
post '/api/v6/purchase-orders',
{
'data' => {
'delivery_name' => 'ASDFG Corp',
'type' => 'purchase_orders'
}
}.to_json, "CONTENT_TYPE" => JSONAPI::MEDIA_TYPE

assert_equal 201, status
end

def test_post_formatted_keys_different_route_key_2
JSONAPI.configuration.route_format = :underscored_route
JSONAPI.configuration.json_key_format = :dasherized_key
post '/api/v7/purchase_orders',
{
'data' => {
'delivery-name' => 'ASDFG Corp',
'type' => 'purchase-orders'
}
}.to_json, "CONTENT_TYPE" => JSONAPI::MEDIA_TYPE

assert_equal 201, status
end

def test_post_formatted_keys_wrong_format
JSONAPI.configuration.route_format = :dasherized_route
JSONAPI.configuration.json_key_format = :dasherized_key
post '/api/v6/purchase-orders',
{
'data' => {
'delivery_name' => 'ASDFG Corp',
'type' => 'purchase-orders'
}
}.to_json, "CONTENT_TYPE" => JSONAPI::MEDIA_TYPE

assert_equal 400, status
end

def test_patch_formatted_dasherized
JSONAPI.configuration.route_format = :dasherized_route
JSONAPI.configuration.json_key_format = :dasherized_key
patch '/api/v6/purchase-orders/1',
{
'data' => {
'id' => '1',
'delivery-name' => 'ASDFG Corp',
'type' => 'purchase-orders'
}
}.to_json, "CONTENT_TYPE" => JSONAPI::MEDIA_TYPE

assert_equal 200, status
end

end
14 changes: 14 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,20 @@ def as_json(options = nil)

end
JSONAPI.configuration.route_format = :underscored_route

JSONAPI.configuration.route_format = :dasherized_route
namespace :v6 do
jsonapi_resources :customers
jsonapi_resources :purchase_orders
jsonapi_resources :line_items
end
JSONAPI.configuration.route_format = :underscored_route

namespace :v7 do
jsonapi_resources :customers
jsonapi_resources :purchase_orders
jsonapi_resources :line_items
end
end
end

Expand Down
Loading