Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix option_values nested attributes behavior on the API #4409

Conversation

waiting-for-dev
Copy link
Contributor

@waiting-for-dev waiting-for-dev commented Jun 8, 2022

Description

Both for option types and variants, :option_values_attributes was defined as a plain Symbol. As the Rails documentation makes clear, nested attributes must be explicitly defined. Otherwise, they default to an empty Hash. On top of that, there's not a nested attributes definition for option values in variants.

We fix it for option types and remove it altogether for variants. Variants already support :option_value_ids array, which covers the most common use case (adding option values to an already created variant), so there's no need to provide such a nested hierarchy.

Checklist:

  • I have followed Pull Request guidelines
  • I have added a detailed description into each commit message
  • I have updated Guides and README accordingly to this change (if needed)
  • I have added tests to cover this change (if needed)
  • I have attached screenshots to this PR for visual changes (if needed)

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
Copy link
Member

@kennyadsl kennyadsl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, Marc. If I got it correctly, all existing stores won't be affected because we are adding more attributes to the permitted list and not removing any of the existing ones.

But, if someone was using option_values_attributes on the variant endpoints, would their requests break? Also, please take a look at the API docs here. If I recall correctly that variant_attributes is also used to define the API available params.

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
@waiting-for-dev waiting-for-dev force-pushed the waiting-for-dev/variant_option_values_api branch from 3b30854 to c14578b Compare June 10, 2022 03:59
@waiting-for-dev
Copy link
Contributor Author

But, if someone was using option_values_attributes on the variant endpoints, would their requests break?

No. The attribute will be ignored (not permitted). The difference is that both keys and values will now be ignored, while until now, the given value reached the model layer but was ignored once there. Notice that there's no definition of nested option values within variants:

irb(main):005:0> Spree::Variant.new.option_values_attributes=[]
/home/solidus_user/gems/gems/activemodel-7.0.3/lib/active_model/attribute_methods.rb:458:in `method_missing': undefined method `option_values_attributes=' for #<Spree::Variant id: nil, sku: "", weight: 0.0, height: nil, width: nil, depth: nil, deleted_at: nil, is_master: false, product_id: nil, cost_price: nil, position: nil, cost_currency: nil, track_inventory: true, tax_category_id: nil, updated_at: nil, created_at: nil> (NoMethodError)     
Did you mean?  option_values_variants=                                                               
               option_values_variants

Also, please take a look at the API docs here. If I recall correctly that variant_attributes is also used to define the API available params.

I'm not sure I follow. I removed it from the docs right now. Is that what you meant?

@kennyadsl
Copy link
Member

The difference is that both keys and values will now be ignored, while until now, the given value reached the model layer but was ignored once there.

Ah, now it's clear, thanks.

I'm not sure I follow. I removed it from the docs right now. Is that what you meant?

Yes, exactly. Thanks Marc!

Copy link

@luiz-m-affonso luiz-m-affonso left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💯

@waiting-for-dev waiting-for-dev merged commit b3d6f3d into solidusio:master Jun 14, 2022
@waiting-for-dev waiting-for-dev deleted the waiting-for-dev/variant_option_values_api branch June 14, 2022 03:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants