Skip to content

Commit

Permalink
Fix deeply nested unresolvable entry filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
dlitvakb committed Aug 16, 2018
1 parent 0255698 commit 60caa53
Show file tree
Hide file tree
Showing 6 changed files with 194 additions and 15 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# Change Log

## Unreleased
### Fixed
* Fixed deeply nested resources now also filter unresolvable entries. [#177](https://github.com/contentful/contentful.rb/issues/177)

## 2.8.0
### Added
* Add support for `sync` on environments other than `master`.
* Added support for `sync` on environments other than `master`.

## 2.7.0
### Added
Expand Down
5 changes: 2 additions & 3 deletions lib/contentful/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -364,10 +364,9 @@ def do_build_resource(response)
logger.debug(response: response) if logger
configuration[:resource_builder].new(
response.object,
configuration,
configuration.merge(endpoint: response.request.endpoint),
(response.request.query || {}).fetch(:locale, nil) == '*',
0,
response.request.endpoint
0
).run
end

Expand Down
13 changes: 7 additions & 6 deletions lib/contentful/entry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def entry?

def coerce(field_id, value, includes, errors, entries = {})
if Support.link?(value) && !Support.unresolvable?(value, errors)
return build_nested_resource(value, includes, entries)
return build_nested_resource(value, includes, entries, errors)
end
return coerce_link_array(value, includes, errors, entries) if Support.link_array?(value)

Expand All @@ -35,7 +35,8 @@ def coerce(field_id, value, includes, errors, entries = {})
def coerce_link_array(value, includes, errors, entries)
items = []
value.each do |link|
items << build_nested_resource(link, includes, entries) unless Support.unresolvable?(link, errors)
nested_resource = build_nested_resource(link, includes, entries, errors) unless Support.unresolvable?(link, errors)
items << nested_resource unless nested_resource.nil?
end

items
Expand All @@ -45,16 +46,16 @@ def coerce_link_array(value, includes, errors, entries)
# in case one of the included items has a reference in an upper level,
# so we can keep the include chain for that object as well
# Any included object after the maximum include resolution depth will be just a Link
def build_nested_resource(value, includes, entries)
def build_nested_resource(value, includes, entries, errors)
if @depth < @configuration.fetch(:max_include_resolution_depth, 20)
resource = Support.resource_for_link(value, includes)
return resolve_include(resource, includes, entries) unless resource.nil?
return resolve_include(resource, includes, entries, errors) unless resource.nil?
end

build_link(value)
end

def resolve_include(resource, includes, entries)
def resolve_include(resource, includes, entries, errors)
require_relative 'resource_builder'

ResourceBuilder.new(
Expand All @@ -66,7 +67,7 @@ def resolve_include(resource, includes, entries)
),
localized,
@depth + 1,
includes
errors
).run
end

Expand Down
12 changes: 7 additions & 5 deletions lib/contentful/resource_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,18 @@ class ResourceBuilder

attr_reader :json, :default_locale, :endpoint, :depth, :localized, :resource_mapping, :entry_mapping, :resource

def initialize(json, configuration = {}, localized = false, depth = 0, endpoint = nil)
def initialize(json, configuration = {}, localized = false, depth = 0, errors = [])
@json = json
@default_locale = configuration.fetch(:default_locale, ::Contentful::Client::DEFAULT_CONFIGURATION[:default_locale])
@resource_mapping = default_resource_mapping.merge(configuration.fetch(:resource_mapping, {}))
@entry_mapping = default_entry_mapping.merge(configuration.fetch(:entry_mapping, {}))
@includes_for_single = configuration.fetch(:includes_for_single, [])
@localized = localized
@depth = depth
@endpoint = endpoint
@endpoint = configuration.fetch(:endpoint, nil)
@configuration = configuration
@resource_cache = configuration[:_entries_cache] || {}
@errors = errors
end

# Starts the parsing process.
Expand All @@ -58,8 +59,8 @@ def run
private

def build_array
includes = fetch_includes
errors = fetch_errors
includes = fetch_includes || @includes_for_single
errors = fetch_errors || @errors

result = json['items'].map do |item|
next if Support.unresolvable?(item, errors)
Expand All @@ -70,8 +71,9 @@ def build_array
end

def build_single
return if Support.unresolvable?(json, @errors)
includes = @includes_for_single
build_item(json, includes)
build_item(json, includes, @errors)
end

def build_item(item, includes = [], errors = [])
Expand Down
8 changes: 8 additions & 0 deletions spec/entry_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,14 @@ def test_dump(nyancat)
expect(entry.modules.size).to eq 2
}
end

it 'unresolvable entries get filtered from results in deeply nested objects - #177', focus: true do
vcr('entries/unresolvable_filter_deeply_nested') {
client = create_client(space: 'z471hdso7l1a', access_token: '8a0e09fe71f1cb41e8788ace86a8c8d9d084599fe43a40070f232045014d2585', dynamic_entries: :auto)
entry = client.entry('1hb8sipClkQ8ggeGaeSQWm', include: 3)
expect(entry.should_published.first.should_unpublished.size).to eq 0
}
end
end

describe 'camel case' do
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 60caa53

Please sign in to comment.