From 489e623507d9d8683b6b6264fa4ee1b03f2d4f35 Mon Sep 17 00:00:00 2001 From: Keenan Brock Date: Thu, 30 Jan 2025 14:00:14 -0500 Subject: [PATCH] Include aliases in attribute lists --- app/controllers/api/base_controller/normalizer.rb | 4 ++-- app/controllers/api/pictures_controller.rb | 2 +- lib/services/api/options_serializer.rb | 5 ++++- spec/requests/flavors_spec.rb | 7 +++++-- 4 files changed, 12 insertions(+), 6 deletions(-) diff --git a/app/controllers/api/base_controller/normalizer.rb b/app/controllers/api/base_controller/normalizer.rb index f2585e2cd8..69fb92f630 100644 --- a/app/controllers/api/base_controller/normalizer.rb +++ b/app/controllers/api/base_controller/normalizer.rb @@ -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 diff --git a/app/controllers/api/pictures_controller.rb b/app/controllers/api/pictures_controller.rb index f44ee080a7..9bf9c1a696 100644 --- a/app/controllers/api/pictures_controller.rb +++ b/app/controllers/api/pictures_controller.rb @@ -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) end end end diff --git a/lib/services/api/options_serializer.rb b/lib/services/api/options_serializer.rb index c4e62a70f0..c486465599 100644 --- a/lib/services/api/options_serializer.rb +++ b/lib/services/api/options_serializer.rb @@ -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 diff --git a/spec/requests/flavors_spec.rb b/spec/requests/flavors_spec.rb index 2df6f659eb..38793c12bd 100644 --- a/spec/requests/flavors_spec.rb +++ b/spec/requests/flavors_spec.rb @@ -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)