Skip to content

Commit 4eb0653

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 c058816)
1 parent 1bd6c82 commit 4eb0653

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

lib/jsonapi/resource.rb

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

420-
subclass.attribute :id, format: :id
420+
unless subclass._attributes[:id]
421+
subclass.attribute :id, format: :id
422+
end
421423

422424
check_reserved_resource_name(subclass._type, subclass.name)
423425
end
@@ -579,7 +581,7 @@ def updatable_fields(_context = nil)
579581

580582
# Override in your resource to filter the creatable keys
581583
def creatable_fields(_context = nil)
582-
_updatable_relationships | _attributes.keys
584+
_updatable_relationships | _attributes.keys - [:id]
583585
end
584586

585587
# 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
@@ -1031,7 +1031,7 @@ def self.updatable_fields(context)
10311031
end
10321032

10331033
def self.creatable_fields(context)
1034-
super(context) - [:subject, :id]
1034+
super(context) - [:subject]
10351035
end
10361036

10371037
def self.sortable_fields(context)
@@ -1053,6 +1053,10 @@ class HairCutResource < JSONAPI::Resource
10531053
class IsoCurrencyResource < JSONAPI::Resource
10541054
attributes :name, :country_name, :minor_unit
10551055

1056+
def self.creatable_fields(_context = nil)
1057+
super + [:id]
1058+
end
1059+
10561060
filter :country_name
10571061

10581062
key_type :string

0 commit comments

Comments
 (0)