Skip to content

Commit 2eb7fe8

Browse files
authored
Allow fields option to be passed with string keys. (#7)
1 parent 6ea87f5 commit 2eb7fe8

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

lib/jsonapi/renderer.rb

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@ module JSONAPI
77
#
88
# @param [Hash] params
99
# @option [(#jsonapi_id, #jsonapi_type, #jsonapi_related, #as_jsonapi),
10-
# Array<(#jsonapi_id, #jsonapi_type, #jsonapi_related, #as_jsonapi)>,
10+
# Array<(#jsonapi_id, #jsonapi_type, #jsonapi_related,
11+
# #as_jsonapi)>,
1112
# nil] data Primary resource(s) to be rendered.
1213
# @option [Array<#jsonapi_id>] errors Errors to be rendered.
13-
# @option [String, Hash{Symbol => Hash}] include Relationships to be
14-
# included.
15-
# @option [Hash{Symbol, Array<Symbol>}] fields List of requested fields
16-
# for some or all of the resource types.
14+
# @option include Relationships to be included. See
15+
# JSONAPI::IncludeDirective.
16+
# @option [Hash{Symbol, Array<Symbol>}, Hash{String, Array<String>}] fields
17+
# List of requested fields for some or all of the resource types.
1718
# @option [Hash] meta Non-standard top-level meta information to be
1819
# included.
1920
# @option [Hash] links Top-level links to be included.

lib/jsonapi/renderer/document.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ def initialize(params = {})
99
@errors = params.fetch(:errors, [])
1010
@meta = params.fetch(:meta, nil)
1111
@links = params.fetch(:links, {})
12-
@fields = params.fetch(:fields, {})
12+
@fields = _symbolize_fields(params.fetch(:fields, {}))
1313
@jsonapi = params.fetch(:jsonapi, nil)
1414
@include = JSONAPI::IncludeDirective.new(params.fetch(:include, {}))
1515
end
@@ -48,6 +48,12 @@ def errors_hash
4848
hash[:errors] = @errors.map(&:as_jsonapi)
4949
end
5050
end
51+
52+
def _symbolize_fields(fields)
53+
fields.each_with_object({}) do |(k, v), h|
54+
h[k.to_sym] = v.map(&:to_sym)
55+
end
56+
end
5157
end
5258
end
5359
end

0 commit comments

Comments
 (0)