Skip to content

Commit 672f946

Browse files
authored
Merge pull request #740 from cerebris/warn_on_dup
Add warnings for duplicate attributes and relationship names
2 parents 715dbf3 + df614a9 commit 672f946

3 files changed

Lines changed: 32 additions & 2 deletions

File tree

lib/jsonapi/resource.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,8 @@ def attribute(attr, options = {})
404404
ActiveSupport::Deprecation.warn('Id without format is no longer supported. Please remove ids from attributes, or specify a format.')
405405
end
406406

407+
check_duplicate_attribute_name(attr) if options[:format].nil?
408+
407409
@_attributes ||= {}
408410
@_attributes[attr] = options
409411
define_method attr do
@@ -885,6 +887,8 @@ def _add_relationship(klass, *attrs)
885887

886888
check_reserved_relationship_name(relationship_name)
887889

890+
check_duplicate_relationship_name(relationship_name)
891+
888892
# Initialize from an ActiveRecord model's properties
889893
if _model_class && _model_class.ancestors.collect{|ancestor| ancestor.name}.include?('ActiveRecord::Base')
890894
model_association = _model_class.reflect_on_association(relationship_name)
@@ -1015,6 +1019,18 @@ def check_reserved_relationship_name(name)
10151019
warn "[NAME COLLISION] `#{name}` is a reserved relationship name in #{_resource_name_from_type(_type)}."
10161020
end
10171021
end
1022+
1023+
def check_duplicate_relationship_name(name)
1024+
if _relationships.include?(name.to_sym)
1025+
warn "[DUPLICATE RELATIONSHIP] `#{name}` has already been defined in #{_resource_name_from_type(_type)}."
1026+
end
1027+
end
1028+
1029+
def check_duplicate_attribute_name(name)
1030+
if _attributes.include?(name.to_sym)
1031+
warn "[DUPLICATE ATTRIBUTE] `#{name}` has already been defined in #{_resource_name_from_type(_type)}."
1032+
end
1033+
end
10181034
end
10191035
end
10201036
end

test/fixtures/active_record.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1505,8 +1505,6 @@ class LineItemResource < V6::LineItemResource; end
15051505

15061506
class CustomerResource < V6::CustomerResource
15071507
model_name 'Api::V7::Customer'
1508-
attribute :name
1509-
has_many :purchase_orders
15101508
end
15111509

15121510
class ClientResource < JSONAPI::Resource

test/unit/resource/resource_test.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,22 @@ def test_class_relationships
202202
assert_equal(relationships.size, 2)
203203
end
204204

205+
def test_duplicate_relationship_name
206+
assert_output nil, "[DUPLICATE RELATIONSHIP] `mother` has already been defined in CatResource.\n" do
207+
CatResource.instance_eval do
208+
has_one :mother, class_name: 'Cat'
209+
end
210+
end
211+
end
212+
213+
def test_duplicate_attribute_name
214+
assert_output nil, "[DUPLICATE ATTRIBUTE] `name` has already been defined in CatResource.\n" do
215+
CatResource.instance_eval do
216+
attribute :name
217+
end
218+
end
219+
end
220+
205221
def test_find_with_customized_base_records
206222
author = Person.find(1)
207223
posts = ArticleResource.find([], context: author).map(&:_model)

0 commit comments

Comments
 (0)