Skip to content

Inconsistent XML element names when returning an array versus an object #235

Open
@kelchm

Description

@kelchm

I have an entity that I am using both for presenting an array of Item objects as well as singular Item objects. I was surprised to see that when returning XML the element names are different in these two very similar situations.

item.rb

module Legacy
  module V1
    module Entities
      # Defines the combined item entity
      class Item < Grape::Entity
        expose :object, as: 'itemmast', using: Entities::Itemmast
        expose :object, as: 'loclvl', using: Entities::Loclvl
      end
    end
  end
end

Response Body: Array of Items

<legacy_v1_entities_items type="array">
    <legacy_v1_entities_item>
        <itemmast>
            <ALPHAPC>00001</ALPHAPC>
            <PC type="integer">1</PC>
        </itemmast>
        <loclvl>
            <UPC type="integer">1</UPC>
        </loclvl>
    </legacy_v1_entities_item>
    <legacy_v1_entities_item>
        <itemmast>
            <ALPHAPC>00002</ALPHAPC>
            <PC type="integer">2</PC>
        </itemmast>
        <loclvl>
            <UPC type="integer">2</UPC>
        </loclvl>
    </legacy_v1_entities_item>
</legacy_v1_entities_items>

Response Body: Single Item Object

<hash>
    <itemmast>
        <ALPHAPC>00001</ALPHAPC>
        <PC type="integer">1</PC>
    </itemmast>
    <loclvl>
        <UPC type="integer">1</UPC>
    </loclvl>
</hash>

I've looked at issues #38 and #23, and while I can add a formatter as suggested this can lead to issues with entities which don't follow the same pattern. Additionally, if I change the root in this particular entity, the keys in the resulting JSON response are then inconsistent. Can anyone point me in the right direction of how to approach solving this inconsistency?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions