Forum Discussion

johndorazio's avatar
johndorazio
New Contributor
4 years ago
Solved

nesting in XML representation

I can't figure out how to correctly represent nested arrays in XML. My API's current JSON response is an object with three properties, of which the first is an array of objects with properties, the second an array of strings, and the third an object with properties like this:

 

{
  "results": [
    {
      "property1": 1,
      "property2": 4,
      "property3": "stringvalue"
    },
    {
"property1": 0,
"property2": 16,
"property3": "stringvalue2"
},
{
"property1": 1,
"property2": 8,
"property3": "stringvalue3"
}
], "errors": [ "string" ], "info": { "ENDPOINT_VERSION": "string" } }

I would like to represent the XML in one of two ways, I have yet to define it, I could either use attributes or elements.

Example using attributes:

 

 

<RootElement>
  <results>
    <result property1="1" property2="4" property3="stringvalue"/>
    <result property1="0" property2="16" property3="stringvalue2"/>
    <result property1="1" property2="8" property3="stringvalue3"/>
  </results>
  <errors/>
  <info ENDPOINT_VERSION="2.8"/>
</RootElement>

 

 

Or I could represent using elements:

 

 

<RootElement>
  <results>
    <result> 
      <property1>1</property1>
      <property2>4</property2>
      <property3>stringvalue</property3>
    </result>
    <result> 
      <property1>0</property1>
      <property2>16</property2>
      <property3>stringvalue2</property3>
    </result>
    <result> 
      <property1>1</property1>
      <property2>8</property2>
      <property3>stringvalue3</property3>
    </result>
  </results>
  <errors/>
  <info ENDPOINT_VERSION="2.8"/>
</RootElement>

 

 

I would prefer the first case, where properties become attributes. But I can't figure out how to represent the nested `results` -> `result` in either case! It seems like the outer `results` and each inner `result` either wind up both showing as `results` or both showing as `result`, I can't get the outer wrapper to show as `results` and the inner elements to show as `result`.

  • You need to define the "results" property as a wrapped array (xml.wrapped = true) and also specify xml.name for both the "results" array and the array items. See the Representing XML guide for details.

    components:
      schemas:
        RootElement:
          type: object
          properties:
            results:
              type: array
              items:
                $ref: '#/components/schemas/result'
              xml:
                name: results
                wrapped: true
              # Optional array example
              example:
                - property1: 1
                  property2: 4
                  property3: stringvalue
                - property1: 0
                  property2: 16
                  property3: stringvalue2
        result:
          type: object
          xml:
            name: result
          properties:
            property1:
              type: integer
              xml:
                attribute: true
            property2:
              type: integer
              xml:
                attribute: true
            property3:
              type: string
              xml:
                attribute: true

1 Reply

  • HKosova's avatar
    HKosova
    SmartBear Alumni (Retired)

    You need to define the "results" property as a wrapped array (xml.wrapped = true) and also specify xml.name for both the "results" array and the array items. See the Representing XML guide for details.

    components:
      schemas:
        RootElement:
          type: object
          properties:
            results:
              type: array
              items:
                $ref: '#/components/schemas/result'
              xml:
                name: results
                wrapped: true
              # Optional array example
              example:
                - property1: 1
                  property2: 4
                  property3: stringvalue
                - property1: 0
                  property2: 16
                  property3: stringvalue2
        result:
          type: object
          xml:
            name: result
          properties:
            property1:
              type: integer
              xml:
                attribute: true
            property2:
              type: integer
              xml:
                attribute: true
            property3:
              type: string
              xml:
                attribute: true