Skip to content

Commit c058816

Browse files
committed
Default id to being a read only (not creatable) attribute
Fixes #960 Potentially a breaking change for apps with guids (cherry picked from commit 4f51331)
1 parent fd535b3 commit c058816

2 files changed

Lines changed: 9 additions & 3 deletions

File tree

lib/jsonapi/resource.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,9 @@ def inherited(subclass)
430430
type = subclass.name.demodulize.sub(/Resource$/, '').underscore
431431
subclass._type = type.pluralize.to_sym
432432

433-
subclass.attribute :id, format: :id
433+
unless subclass._attributes[:id]
434+
subclass.attribute :id, format: :id
435+
end
434436

435437
check_reserved_resource_name(subclass._type, subclass.name)
436438
end
@@ -598,7 +600,7 @@ def updatable_fields(_context = nil)
598600

599601
# Override in your resource to filter the creatable keys
600602
def creatable_fields(_context = nil)
601-
_updatable_relationships | _attributes.keys
603+
_updatable_relationships | _attributes.keys - [:id]
602604
end
603605

604606
# Override in your resource to filter the sortable keys

test/fixtures/active_record.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1120,7 +1120,7 @@ def self.updatable_fields(context)
11201120
end
11211121

11221122
def self.creatable_fields(context)
1123-
super(context) - [:subject, :id]
1123+
super(context) - [:subject]
11241124
end
11251125

11261126
def self.sortable_fields(context)
@@ -1142,6 +1142,10 @@ class HairCutResource < JSONAPI::Resource
11421142
class IsoCurrencyResource < JSONAPI::Resource
11431143
attributes :name, :country_name, :minor_unit
11441144

1145+
def self.creatable_fields(_context = nil)
1146+
super + [:id]
1147+
end
1148+
11451149
filter :country_name
11461150

11471151
key_type :string

0 commit comments

Comments
 (0)