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

Add save deny list to base REST resource #919

Merged
merged 2 commits into from
Apr 7, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# Changelog

## Unreleased

- [#919](https://github.com/Shopify/shopify_api/pull/919) Allow REST resources to configure a deny list of attributes to be excluded when saving

## Version 10.0.0

- Major update to the library to provide _all_ essential functions needed for a Shopify app, supporting embedded apps with session tokens. See the [full list of changes](https://github.com/Shopify/shopify_api#breaking-change-notice-for-version-1000) here
Expand Down
7 changes: 7 additions & 0 deletions lib/shopify_api/rest/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class Base
@has_many = T.let({}, T::Hash[Symbol, Class])
@paths = T.let([], T::Array[T::Hash[Symbol, T.any(T::Array[Symbol], String, Symbol)]])
@custom_prefix = T.let(nil, T.nilable(String))
@attribute_save_deny_list = T.let([], T.nilable(T::Array[Symbol]))

sig { returns(T::Hash[Symbol, T.untyped]) }
attr_accessor :original_state
Expand Down Expand Up @@ -118,6 +119,11 @@ def has_one?(attribute)
@has_one.include?(attribute)
end

sig { returns(T.nilable(T::Array[Symbol])) }
def attribute_save_deny_list
@attribute_save_deny_list&.map { |a| :"@#{a}" }
end

sig do
params(
http_method: Symbol,
Expand Down Expand Up @@ -259,6 +265,7 @@ def to_hash
hash = {}
instance_variables.each do |var|
next if [:"@original_state", :"@session", :"@client", :"@forced_nils", :"@errors"].include?(var)
next if self.class.attribute_save_deny_list&.include?(var)

attribute = var.to_s.delete("@").to_sym
if self.class.has_many?(attribute)
Expand Down
17 changes: 17 additions & 0 deletions test/clients/base_rest_resource_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,23 @@ def test_saves_with_children
assert_requested(stubbed_request)
end

def test_save_ignores_unsaveable_attributes
request_body = { fake_resource: { attribute: "attribute" } }.to_json
response_body = { fake_resource: { id: 1, attribute: "attribute" } }.to_json

stubbed_request = stub_request(:post, "#{@prefix}/fake_resources.json")
.with(body: request_body)
.to_return(status: 201, body: response_body)

resource = TestHelpers::FakeResource.new(session: @session)
resource.attribute = "attribute"
resource.unsaveable_attribute = "this is an attribute"

resource.save
assert_requested(stubbed_request)
assert_nil(resource.id)
end

def test_deletes_existing_resource_and_fails_on_deleting_nonexistent_resource
resource = TestHelpers::FakeResource.new(session: @session)
resource.id = 1
Expand Down
6 changes: 6 additions & 0 deletions test/test_helpers/fake_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class FakeResource < ShopifyAPI::Rest::Base
@prev_page_info = T.let(Concurrent::ThreadLocalVar.new { nil }, Concurrent::ThreadLocalVar)
@next_page_info = T.let(Concurrent::ThreadLocalVar.new { nil }, Concurrent::ThreadLocalVar)

@attribute_save_deny_list = T.let([:unsaveable_attribute], T::Array[Symbol])

@paths = T.let([
{ http_method: :get, operation: :get, ids: [], path: "fake_resources.json" },
{ http_method: :post, operation: :post, ids: [], path: "fake_resources.json" },
Expand All @@ -36,6 +38,7 @@ def initialize(session: nil)
@has_one_attribute = T.let(nil, T.nilable(FakeResource))
@has_many_attribute = T.let(nil, T.nilable(T::Array[FakeResource]))
@other_resource_id = T.let(nil, T.nilable(Integer))
@unsaveable_attribute = T.let(nil, T.nilable(String))
end

sig { returns(T.nilable(Integer)) }
Expand All @@ -53,6 +56,9 @@ def initialize(session: nil)
sig { returns(T.nilable(Integer)) }
attr_reader :other_resource_id

sig { returns(T.nilable(String)) }
attr_reader :unsaveable_attribute

class << self
sig do
params(id: T.any(Integer, String), session: ShopifyAPI::Auth::Session, param: T.untyped,
Expand Down