Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

XML Example for Arrays does not display #3132

Closed
danpetitt opened this issue May 25, 2017 · 10 comments
Closed

XML Example for Arrays does not display #3132

danpetitt opened this issue May 25, 2017 · 10 comments

Comments

@danpetitt
Copy link

When a response is an array of objects, the XML example fails to display anything. The online PetStore sample exhibits this same problem for api: GET->/pet/findByStatus

Select "application/xml" and the XML example is just:

<?xml version="1.0" encoding="UTF-8"?>
<!-- XML example cannot be generated -->

When its a single object and not an array, like in Post->/pet/, it works fine.

Sorry, i dont know what version of SwaggerUI the online PetStore example uses but the swagger doc is correct:

"responses":{
    "200":{
        "description":"successful operation",
        "schema":{
            "type":"array",
            "items":{
                "$ref":"#/definitions/Pet"
            }
        }
    }
}
@hkosova
Copy link
Contributor

hkosova commented May 25, 2017

The problem seems to be not with arrays in general, but with inline array and object schemas without xml.name. It seems that the XML example builder cannot handle such schemas.

Here's a minimal example:

swagger: "2.0"
info:
  title: test
  version: "1.0"
produces:
  - application/xml
  - application/json
paths:
  /:
    get:
      responses:
        "200":
          description: OK
          schema:
            type: object
            properties:
              foo:
                type: string
            # Uncommenting these lines fixes the issue
            # xml:
            #   name: something

In case of Petstore, the workaround to see an example is to change the response as follows:

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

or

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

However, the generated example is still incorrect - it has a single pet and not an array. :(

@danpetitt
Copy link
Author

Yes, its still broken.

@webron
Copy link
Contributor

webron commented May 26, 2017

@bodnia I understand the potential issue here, but it looks like we had a solution for it in 2.x. Can we replicate the same logic here?

@danpetitt
Copy link
Author

I dont think you did; it doesnt work seem to work right in v2 either which is used in swashbuckle.

I have changed swashbuckle to emit "xml: name" properties for an object definition and array definition which removes the "Inline Model" problem, but still makes the XML a single 'broken' node and not an array of nodes. This occurs in v2 and v3:

          schema:
            type: array
            items:
              $ref: '#/definitions/Channel'
            xml:
              name: Channels

definitions:
  Channel:
    type: object
    xml:
      name: Channel
    properties:
      id:
        format: int32
        type: integer
      name:
        type: string
      active:
        format: int32
        type: integer
      defaultTerritoryID:
        format: int32
        type: integer

Emits the following correct JSON:

[
  {
    "id": 0,
    "name": "string",
    "active": 0,
    "defaultTerritoryID": 0
  }
]

But broken and potentially invalid XML:

<?xml version="1.0" encoding="UTF-8"?>
<Channel>
	<id>0</id>
	<name>string</name>
	<active>0</active>
	<defaultTerritoryID>0</defaultTerritoryID>
</Channel>

@hkosova
Copy link
Contributor

hkosova commented May 26, 2017

@coderangerdan, I just realized a single-item example is actually correct. To have array items wrapped into an extra tag, the array schema must have xml.wrapped=true.
https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#user-content-xmlWrapped

          schema:
            type: array
            items:
              $ref: '#/definitions/Channel'
            xml:
              name: Channels
              wrapped: true   # <--------

@bodnia
Copy link
Contributor

bodnia commented May 30, 2017

@coderangerdan are you still expiring any issues with XML rendering?

@webron all tests from 2.x version were moved to 3.x and also I added more tests, so all functionality present in 2.x is also added to 3.x

@danpetitt
Copy link
Author

Yes we are but its an issue with Swashbuckle not generating the appropriate swagger doc info; once i add that information in, the UI renders fine ... the Petstore example is also broken in the same way and needs to be amended to include the xml.name/wrapped elements

@bodnia
Copy link
Contributor

bodnia commented May 30, 2017

@coderangerdan can the issue be closed?

@danpetitt
Copy link
Author

Well the petstore sample needs fixing, not sure where that data comes from but i guess its not a UI issue so yes.

@lock
Copy link

lock bot commented Jul 1, 2019

Locking due to inactivity.
This is done to avoid resurrecting old issues and bumping long threads with new, possibly unrelated content.
If you think you're experiencing something similar to what you've found here: please open a new issue, follow the template, and reference this issue in your report.
Thanks!

@lock lock bot locked and limited conversation to collaborators Jul 1, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants