diff --git a/lib/jsonapi/exceptions.rb b/lib/jsonapi/exceptions.rb index e589f08e0..58dbab5b8 100644 --- a/lib/jsonapi/exceptions.rb +++ b/lib/jsonapi/exceptions.rb @@ -141,26 +141,6 @@ def errors end end - - class HasManyRelationExists < Error - attr_accessor :id - - def initialize(id, error_object_overrides = {}) - @id = id - super(error_object_overrides) - end - - def errors - [create_error_object(code: JSONAPI::RELATION_EXISTS, - status: :bad_request, - title: I18n.translate('jsonapi-resources.exceptions.has_many_relation.title', - default: 'Relation exists'), - detail: I18n.translate('jsonapi-resources.exceptions.has_many_relation.detail', - default: "The relation to #{id} already exists.", - id: id))] - end - end - class BadRequest < Error def initialize(exception, error_object_overrides = {}) @exception = exception diff --git a/lib/jsonapi/resource.rb b/lib/jsonapi/resource.rb index bd1c6e9f4..1cc081d25 100644 --- a/lib/jsonapi/resource.rb +++ b/lib/jsonapi/resource.rb @@ -238,14 +238,7 @@ def reflect_relationship?(relationship, options) def _create_to_many_links(relationship_type, relationship_key_values, options) relationship = self.class._relationships[relationship_type] - - # check if relationship_key_values are already members of this relationship relation_name = relationship.relation_name(context: @context) - existing_relations = @model.public_send(relation_name).where(relationship.primary_key => relationship_key_values) - if existing_relations.count > 0 - # todo: obscure id so not to leak info - fail JSONAPI::Exceptions::HasManyRelationExists.new(existing_relations.first.id) - end if options[:reflected_source] @model.public_send(relation_name) << options[:reflected_source]._model @@ -273,7 +266,9 @@ def _create_to_many_links(relationship_type, relationship_key_values, options) end @reload_needed = true else - @model.public_send(relation_name) << related_resource._model + unless @model.public_send(relation_name).include?(related_resource._model) + @model.public_send(relation_name) << related_resource._model + end end end diff --git a/locales/en.yml b/locales/en.yml index d1dfccc01..7425f89ef 100644 --- a/locales/en.yml +++ b/locales/en.yml @@ -16,9 +16,6 @@ en: unsupported_media_type: title: 'Unsupported media type' detail: "All requests that create or update must use the '%{needed_media_type}' Content-Type. This request specified '%{media_type}.'" - has_many_relation: - title: 'Relation exists' - detail: "The relation to %{id} already exists." to_many_set_replacement_forbidden: title: 'Complete replacement forbidden' detail: 'Complete replacement forbidden for this relationship' diff --git a/test/controllers/controller_test.rb b/test/controllers/controller_test.rb index 1b19e7efb..a2bddffe2 100644 --- a/test/controllers/controller_test.rb +++ b/test/controllers/controller_test.rb @@ -1508,8 +1508,9 @@ def test_create_relationship_to_many_join_table_record_exists post :create_relationship, params: {post_id: 3, relationship: 'tags', data: [{type: 'tags', id: 502}, {type: 'tags', id: 505}]} - assert_response :bad_request - assert_match /The relation to 502 already exists./, response.body + assert_response :no_content + post_object.reload + assert_equal [502,503,505], post_object.tag_ids end def test_update_relationship_to_many_missing_tags