diff --git a/app/controllers/api/v1/inventory_items_controller.rb b/app/controllers/api/v1/inventory_items_controller.rb index 4ed13d37e..4f6815fcf 100644 --- a/app/controllers/api/v1/inventory_items_controller.rb +++ b/app/controllers/api/v1/inventory_items_controller.rb @@ -16,9 +16,10 @@ module Api repository_cells: Extends::REPOSITORY_SEARCH_INCLUDES ).page(params.dig(:page, :number)) .per(params.dig(:page, :size)) + incl = params[:include] == 'inventory_cells' ? :inventory_cells : nil render jsonapi: items, each_serializer: InventoryItemSerializer, - include: :inventory_cells + include: incl end def create diff --git a/app/serializers/api/v1/inventory_cell_serializer.rb b/app/serializers/api/v1/inventory_cell_serializer.rb index b615f456c..dc89e1454 100644 --- a/app/serializers/api/v1/inventory_cell_serializer.rb +++ b/app/serializers/api/v1/inventory_cell_serializer.rb @@ -7,12 +7,6 @@ module Api attributes :id, :value_type, :value attribute :repository_column_id, key: :column_id - def value_type - type_id = RepositoryColumn - .data_types[object.repository_column.data_type] - I18n.t("api.v1.inventory_data_types.t#{type_id}") - end - def value value = case object.value_type diff --git a/app/serializers/api/v1/inventory_item_serializer.rb b/app/serializers/api/v1/inventory_item_serializer.rb index 3df5dc3f8..e980096fb 100644 --- a/app/serializers/api/v1/inventory_item_serializer.rb +++ b/app/serializers/api/v1/inventory_item_serializer.rb @@ -7,7 +7,8 @@ module Api attributes :name has_many :repository_cells, key: :inventory_cells, serializer: InventoryCellSerializer, - class_name: 'RepositoryCell' + class_name: 'RepositoryCell', + unless: -> { object.repository_cells.empty? } end end end diff --git a/spec/requests/api/v1/inventory_items_controller_spec.rb b/spec/requests/api/v1/inventory_items_controller_spec.rb index ce1d969c7..5d569fbcc 100644 --- a/spec/requests/api/v1/inventory_items_controller_spec.rb +++ b/spec/requests/api/v1/inventory_items_controller_spec.rb @@ -75,6 +75,24 @@ RSpec.describe 'Api::V1::InventoryItemsController', type: :request do include: :inventory_cells) .as_json[:data] ) + expect(hash_body).not_to include('included') + end + + it 'Response with correct inventory items, included cells' do + hash_body = nil + get api_v1_team_inventory_items_path( + team_id: @teams.first.id, + inventory_id: @teams.first.repositories.first.id, + include: 'inventory_cells' + ), headers: @valid_headers + expect { hash_body = json }.not_to raise_exception + expect(hash_body[:data]).to match( + ActiveModelSerializers::SerializableResource + .new(@valid_inventory.repository_rows.limit(10), + each_serializer: Api::V1::InventoryItemSerializer, + include: :inventory_cells) + .as_json[:data] + ) expect(hash_body[:included]).to match( ActiveModelSerializers::SerializableResource .new(@valid_inventory.repository_rows.limit(10), @@ -98,13 +116,7 @@ RSpec.describe 'Api::V1::InventoryItemsController', type: :request do include: :inventory_cells) .as_json[:data] ) - expect(hash_body[:included]).to match( - ActiveModelSerializers::SerializableResource - .new(@valid_inventory.repository_rows.limit(100), - each_serializer: Api::V1::InventoryItemSerializer, - include: :inventory_cells) - .as_json[:included] - ) + expect(hash_body).not_to include('included') end it 'When invalid request, user in not member of the team' do