ruby-grape / ruby-grape/grape-entity

Inconsistent XML element names when returning an array versus an object

Open
#235 0 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug? question
Dominant language
Ruby
Stars
729
Forks
154
PR merge metrics
No merged PRs in 30d

Description

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?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with item.rb and compare the XML serialization entry points for an array of Items and a single Item. Review issues #38 and #23, including the formatter and root approaches mentioned there. Done means establishing consistent element names without making the JSON keys inconsistent.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
api
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.