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

Include aliases in attribute lists #1278

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions app/controllers/api/base_controller/normalizer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,9 @@ def normalize_select_attributes(obj, opts)
if opts[:render_attributes].present?
opts[:render_attributes]
elsif obj.respond_to?(:attributes) && obj.class.respond_to?(:virtual_attribute_names)
obj.attributes.keys - obj.class.virtual_attribute_names
(obj.attributes.keys | obj.class.try(:attribute_aliases)&.keys) - obj.class.virtual_attribute_names
elsif obj.respond_to?(:attributes)
obj.attributes.keys
(obj.attributes.keys | obj.class.try(:attribute_aliases)&.keys)
else
obj.keys
end
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/api/pictures_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def attribute_selection
if !@req.attributes.empty? || @additional_attributes
@req.attributes | Array(@additional_attributes) | ID_ATTRS
else
Picture.attribute_names - %w(content)
Picture.attribute_names | Picture.try(:attribute_aliases)&.keys - %w(content)
Copy link
Member

Choose a reason for hiding this comment

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

end
end
end
Expand Down
5 changes: 4 additions & 1 deletion lib/services/api/options_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,24 @@ def serialize

def attributes
return [] unless klass
options_attribute_list(klass.attribute_names - klass.virtual_attribute_names)
options_attribute_list((klass.attribute_names | model.try(:attribute_aliases)&.keys) - klass.virtual_attribute_names)
end

def virtual_attributes
return [] unless klass

options_attribute_list(klass.virtual_attribute_names)
end

def relationships
return [] unless klass

(klass.reflections.keys | klass.virtual_reflections.keys.collect(&:to_s)).sort
end

def subcollections
return [] unless klass

Array(config[config.name_for_klass(klass)].subcollections).sort
end

Expand Down
7 changes: 5 additions & 2 deletions spec/requests/flavors_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,16 @@
it "can show a provider's flavor" do
api_basic_authorize(action_identifier(:flavors, :read, :subresource_actions, :get))
ems = FactoryBot.create(:ems_cloud)
flavor = FactoryBot.create(:flavor, :ext_management_system => ems)
# declaring cpus to ensure alias_attribute works
flavor = FactoryBot.create(:flavor, :ext_management_system => ems, :cpus => 5)

get(api_provider_flavor_url(nil, ems, flavor))

expected = {
"href" => api_provider_flavor_url(nil, ems, flavor),
"id" => flavor.id.to_s
"id" => flavor.id.to_s,
"cpus" => 5,
"cpu_total_cores" => 5
}
expect(response.parsed_body).to include(expected)
expect(response).to have_http_status(:ok)
Expand Down
Loading