Skip to content

Commit 78a2edd

Browse files
authored
Merge pull request #1167 from HQTrust/hotfix-master/allow-to-post-exisiting-relationships
Allow to post existing relationships
2 parents f2302ab + a51ea62 commit 78a2edd

File tree

4 files changed

+6
-33
lines changed

4 files changed

+6
-33
lines changed

lib/jsonapi/exceptions.rb

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -141,26 +141,6 @@ def errors
141141
end
142142
end
143143

144-
145-
class HasManyRelationExists < Error
146-
attr_accessor :id
147-
148-
def initialize(id, error_object_overrides = {})
149-
@id = id
150-
super(error_object_overrides)
151-
end
152-
153-
def errors
154-
[create_error_object(code: JSONAPI::RELATION_EXISTS,
155-
status: :bad_request,
156-
title: I18n.translate('jsonapi-resources.exceptions.has_many_relation.title',
157-
default: 'Relation exists'),
158-
detail: I18n.translate('jsonapi-resources.exceptions.has_many_relation.detail',
159-
default: "The relation to #{id} already exists.",
160-
id: id))]
161-
end
162-
end
163-
164144
class BadRequest < Error
165145
def initialize(exception, error_object_overrides = {})
166146
@exception = exception

lib/jsonapi/resource.rb

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -238,14 +238,7 @@ def reflect_relationship?(relationship, options)
238238

239239
def _create_to_many_links(relationship_type, relationship_key_values, options)
240240
relationship = self.class._relationships[relationship_type]
241-
242-
# check if relationship_key_values are already members of this relationship
243241
relation_name = relationship.relation_name(context: @context)
244-
existing_relations = @model.public_send(relation_name).where(relationship.primary_key => relationship_key_values)
245-
if existing_relations.count > 0
246-
# todo: obscure id so not to leak info
247-
fail JSONAPI::Exceptions::HasManyRelationExists.new(existing_relations.first.id)
248-
end
249242

250243
if options[:reflected_source]
251244
@model.public_send(relation_name) << options[:reflected_source]._model
@@ -273,7 +266,9 @@ def _create_to_many_links(relationship_type, relationship_key_values, options)
273266
end
274267
@reload_needed = true
275268
else
276-
@model.public_send(relation_name) << related_resource._model
269+
unless @model.public_send(relation_name).include?(related_resource._model)
270+
@model.public_send(relation_name) << related_resource._model
271+
end
277272
end
278273
end
279274

locales/en.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@ en:
1616
unsupported_media_type:
1717
title: 'Unsupported media type'
1818
detail: "All requests that create or update must use the '%{needed_media_type}' Content-Type. This request specified '%{media_type}.'"
19-
has_many_relation:
20-
title: 'Relation exists'
21-
detail: "The relation to %{id} already exists."
2219
to_many_set_replacement_forbidden:
2320
title: 'Complete replacement forbidden'
2421
detail: 'Complete replacement forbidden for this relationship'

test/controllers/controller_test.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1508,8 +1508,9 @@ def test_create_relationship_to_many_join_table_record_exists
15081508

15091509
post :create_relationship, params: {post_id: 3, relationship: 'tags', data: [{type: 'tags', id: 502}, {type: 'tags', id: 505}]}
15101510

1511-
assert_response :bad_request
1512-
assert_match /The relation to 502 already exists./, response.body
1511+
assert_response :no_content
1512+
post_object.reload
1513+
assert_equal [502,503,505], post_object.tag_ids
15131514
end
15141515

15151516
def test_update_relationship_to_many_missing_tags

0 commit comments

Comments
 (0)