Skip to content

Commit

Permalink
normalize hash without type to return valid hrefs
Browse files Browse the repository at this point in the history
  • Loading branch information
Jillian Tullo committed Oct 30, 2017
1 parent 3f787de commit ce89f6e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
14 changes: 8 additions & 6 deletions app/controllers/api/base_controller/normalizer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ def normalize_hash(type, obj, opts = {})
attrs = normalize_select_attributes(obj, opts)
result = {}

key_id = collection_config.resource_identifier(type)
href = new_href(type, obj[key_id], obj["href"])
if href.present?
result["href"] = href
attrs -= ["href"]
if type
key_id = collection_config.resource_identifier(type)
href = new_href(type, obj[key_id], obj["href"])
if href.present?
result["href"] = href
attrs -= ["href"]
end
end

attrs.each do |k|
Expand All @@ -30,7 +32,7 @@ def normalize_attr(attr, value)
return if value.nil?
if value.kind_of?(Array) || value.kind_of?(ActiveRecord::Relation)
normalize_array(value)
elsif !attr.nil? && (value.respond_to?(:attributes) || value.respond_to?(:keys))
elsif value.respond_to?(:attributes) || value.respond_to?(:keys)
normalize_hash(attr, value)
elsif attr == "id" || attr.to_s.ends_with?("_id")
value.to_s
Expand Down
15 changes: 15 additions & 0 deletions spec/requests/authentication_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,21 @@
}
expect(response.parsed_body).to include(expected)
end

it "returns a correctly formatted collection hrefs" do
api_basic_authorize

get api_entrypoint_url

collection_names = Api::ApiConfig.collections.to_h.select { |_, v| v.options.include?(:collection) }.keys
hrefs = collection_names.collect { |name| url_for(:controller => name, :action => "index") }
expected = {
"collections" => a_collection_containing_exactly(
*hrefs.collect { |href| a_hash_including("href" => href) }
)
}
expect(response.parsed_body).to include(expected)
end
end

context "Basic Authentication with Group Authorization" do
Expand Down

0 comments on commit ce89f6e

Please sign in to comment.