From 4253707fd04ea6768f55a9f338ecd53d8a05e4f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Busqu=C3=A9?= Date: Wed, 8 Jun 2022 13:19:33 +0200 Subject: [PATCH 1/2] Fix working with nested option values through option types in the API As the Rails documentation makes clear [1], nested attributes must be explicitly defined. Otherwise, they default to an empty Hash. [1] - https://api.rubyonrails.org/classes/ActionController/StrongParameters.html --- api/spec/requests/spree/api/option_types_spec.rb | 16 ++++++++++++++++ core/lib/spree/permitted_attributes.rb | 4 ++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/api/spec/requests/spree/api/option_types_spec.rb b/api/spec/requests/spree/api/option_types_spec.rb index a3e00970c0f..d2db2630490 100644 --- a/api/spec/requests/spree/api/option_types_spec.rb +++ b/api/spec/requests/spree/api/option_types_spec.rb @@ -89,6 +89,22 @@ def check_option_values(option_values) expect(response.status).to eq(201) end + it "can create an option type with nested option values" do + post spree.api_option_types_path, params: { + option_type: { + name: "Option Type", + presentation: "Option Type", + option_values_attributes: [ + name: "foo", presentation: "Foo" + ] + } + } + + option_value = Spree::OptionType.find(json_response["id"]).option_values.first + expect(option_value).not_to be(nil) + expect(option_value.name).to eq("foo") + end + it "cannot create an option type with invalid attributes" do post spree.api_option_types_path, params: { option_type: {} } expect(response.status).to eq(422) diff --git a/core/lib/spree/permitted_attributes.rb b/core/lib/spree/permitted_attributes.rb index d62e6bef0b1..19c54a10545 100644 --- a/core/lib/spree/permitted_attributes.rb +++ b/core/lib/spree/permitted_attributes.rb @@ -65,10 +65,10 @@ module PermittedAttributes @@line_item_attributes = [:id, :variant_id, :quantity] - @@option_type_attributes = [:name, :presentation, :option_values_attributes] - @@option_value_attributes = [:name, :presentation] + @@option_type_attributes = [:name, :presentation, option_values_attributes: option_value_attributes] + @@payment_attributes = [:amount, :payment_method_id, :payment_method] @@product_properties_attributes = [:property_name, :value, :position] From c14578b8c6b838111ef8cb7eb3adddfefdf808db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Busqu=C3=A9?= Date: Wed, 8 Jun 2022 13:22:12 +0200 Subject: [PATCH 2/2] Remove wrong definition of :option_values_attributes for variants There's not a nested attributes definition for option values in variants. On top of that, having `:option_values_attributes` declared as a plain Symbol would have no effect. Rails requires that nested attributes are explicitly declared [1]. Variants already support `:option_value_ids` array [2] which cover the most common use case (adding option values to an already created variant), so there's no need to provide such a nested hierarchy. 1 - https://api.rubyonrails.org/classes/ActionController/StrongParameters.html 2 - https://github.com/solidusio/solidus/blob/07c88ddd1603699939ac0343965fa88dc2e1851a/core/lib/spree/permitted_attributes.rb#L135 --- api/openapi/solidus-api.oas.yml | 4 ---- core/lib/spree/permitted_attributes.rb | 2 +- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/api/openapi/solidus-api.oas.yml b/api/openapi/solidus-api.oas.yml index 7f30147873a..25ce0d3cc97 100644 --- a/api/openapi/solidus-api.oas.yml +++ b/api/openapi/solidus-api.oas.yml @@ -7185,10 +7185,6 @@ components: type: array items: type: integer - option_values_attributes: - type: array - items: - $ref: '#/components/schemas/option-value-input' options: type: object description: '`Name` will be the name/presentation of the option type and `Value` will be the name/presentation of the option value. They will be created if not exist.' diff --git a/core/lib/spree/permitted_attributes.rb b/core/lib/spree/permitted_attributes.rb index 19c54a10545..763ed2148a8 100644 --- a/core/lib/spree/permitted_attributes.rb +++ b/core/lib/spree/permitted_attributes.rb @@ -131,7 +131,7 @@ module PermittedAttributes @@variant_attributes = [ :name, :presentation, :cost_price, :lock_version, :position, :track_inventory, - :product_id, :product, :option_values_attributes, :price, + :product_id, :product, :price, :weight, :height, :width, :depth, :sku, :cost_currency, option_value_ids: [], options: [:name, :value] ]