Skip to content

Fix dirty attribute changes on update#1149

Merged
nelsonwittwer merged 7 commits intomainfrom
nelsonwittwer/fix_save_resources
May 2, 2023
Merged

Fix dirty attribute changes on update#1149
nelsonwittwer merged 7 commits intomainfrom
nelsonwittwer/fix_save_resources

Conversation

@nelsonwittwer
Copy link
Contributor

@nelsonwittwer nelsonwittwer commented Apr 28, 2023

Description

Fixes #1133 and #1037 which both report a substantial issue with tracking what attributes were actually modified by the user since the rest resources were initialized.

The root cause of this issue, as pointed out by @kaarelss, was symbols vs hashes when comparing drift on attributes 🤦 . I also needed to 1) set associations in original_state since our API allows us to modify associated resources and 2) needed to ignore read only attributes in original_state.

How has this been tested?

  • Wrote a unit test that reproduced the issue. The fix was then applied to the base rest resource which passed the test.
  • In a local environment using this branch of the ruby API library, I found an existing product and updated a single attribute. I compared all other attributes and verified only the single attribute was modified.

Checklist:

  • My commit message follow the pattern described in here
  • I have performed a self-review of my own code.
  • I have added tests that prove my fix is effective or that my feature works.
  • I have updated the project documentation.
  • I have added a changelog line.

@nelsonwittwer nelsonwittwer changed the title Fix dirty attribute changes on update WIP Fix dirty attribute changes on update Apr 28, 2023
Copy link

@wayne692 wayne692 left a comment

Choose a reason for hiding this comment

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

It's easy

@nelsonwittwer nelsonwittwer changed the title WIP Fix dirty attribute changes on update Fix dirty attribute changes on update May 1, 2023
Copy link
Contributor

@paulomarg paulomarg left a comment

Choose a reason for hiding this comment

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

Makes sense to me!

T.unsafe(@has_one[attr_sym]).create_instance(data: data_hash, session: session))
child = T.unsafe(@has_one[attr_sym]).create_instance(data: data_hash, session: session)
instance.public_send("#{attribute}=", child)
instance.original_state[attr_sym] = child.to_hash(true)
Copy link

@DuncanUszkay1 DuncanUszkay1 Jun 6, 2023

Choose a reason for hiding this comment

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

This was a breaking change for our application that went unreported in the changelog. We were creating an instance and immediately calling save on it, which with this change resulted in an empty hash being sent to Shopify (since the original state matched the current state after instantiation)

I.E. ShopifyAPI::ObjectName.build(attrs).save! <- this fails now AFAICT

Copy link

@DuncanUszkay1 DuncanUszkay1 Jun 6, 2023

Choose a reason for hiding this comment

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

Not sure if we were using the library improperly since I didn't see mention of this is the docs, but if not I can go update the changelog for others who might run into this. cc @nelsonwittwer

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.

Model save erroneously sends unchanged attributes to API

4 participants