swagger-ui: xml example cannot be generated for response type of array

Q&A (please complete the following information)

  • OS: Windows 10
  • Browser: Firefox, Chrome, Edge
  • Method of installation: npm
  • Swagger-UI version: 3.11.0
  • Swagger/OpenAPI version: Swagger 2.0

Content & configuration

I’ve reproduced this locally, but even easier to demonstrate, petstore has the same issue.

Example Swagger/OpenAPI definition: https://generator.swagger.io/api/swagger.json

snippet from swagger.json

      responses:
        200:
          description: "successful operation"
          schema:
            type: "array"
            items:
              $ref: "#/definitions/Pet"

Describe the bug you’re encountering

In swagger ui, when you have a GET that has a response that is a list, and you selected content type of xml, the Example Value has an error “XML example cannot be generated”.

To reproduce…

Steps to reproduce the behavior:

  1. Go to https://editor.swagger.io
  2. Expand /pet/findByStatus
  3. Make sure Response content type is application/xml
  4. Example Value for status code 200 has “XML example cannot be generated”

Expected behavior

I would expect to see the example xml for the response.

Screenshots

image

About this issue

  • Original URL
  • State: open
  • Created 6 years ago
  • Reactions: 6
  • Comments: 35

Most upvoted comments

I found a work-around for this https://app.swaggerhub.com/apis/whateverxforever/SampleApi/v1-oas3

 get:
      summary: List of all orders
      operationId: getOrderList
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                type: object
                properties:
                  data:
                    type: array
                    items:
                      $ref: '#/components/schemas/Order'
            application/xml:
              schema:
                type: object
                xml:
                  name: Data
                properties:
                  data:
                    type: array
                    items:
                      $ref: '#/components/schemas/Order'

I hope this helped! 😉

It’s 2023, the pandemic is over but this bug is not? 🤔

4 year as past since today and still no fix?

Whats up with this project it’s dead or what?

I found out that if you include an xml name AND wrapped: true it wil generate a correct xml example, and even arrays of string can be controlled how the xml examle is shown for the element tag.

For example in the Pet example code if you add these 3 lines to the response code and/or these 2 lines to the items section in de definitions:

responses:
        "200":
          description: "successful operation"
          schema:
            type: "array"
            xml:             # <<
              name: "Pets"   # << root tag name
              wrapped: true  # << mandatory
            items:
              $ref: "#/definitions/Pet"
...
definitions:
  Pet:
    type: "object"
    required:
    - "name"
    - "photoUrls"
    properties:
      id:
        type: "integer"
        format: "int64"
        xml:
          attribute: true
      category:
        $ref: "#/definitions/Category"
      name:
        type: "string"
        example: "doggie"
      photoUrls:
        type: "array"
        xml:
          name: "PhotoUrls"
          wrapped: true
        items:
          xml:                # <<
            name: "PhotoUrl"  # <<  
          type: "string"
          example: "(uuencoded string)"
      tags:
        type: "array"
        xml:
          name: "Tags"
          wrapped: true
        items:
          $ref: "#/definitions/Tag"
      status:
        type: "string"
        description: "pet status in the store"
        enum:
        - "available"
        - "pending"
        - "sold"
    xml:
      name: "Pet"

it wil correctly make an example with a root tag <Pets>:

<?xml version="1.0" encoding="UTF-8"?>
<Pets>
	<Pet id="0">
		<Category id="0">
			<name>shepherd</name>
		</Category>
		<name>doggie</name>
		<PhotoUrls>
			<PhotoUrl>(uuencoded string)</PhotoUrl>
		</PhotoUrls>
		<Tags>
			<Tag id="0">
				<name>playfull</name>
			</Tag>
		</Tags>
		<status>available</status>
	</Pet>
</Pets>