Swagger-ui: XML example cannot be generated with 'allOf'

Created on 9 Apr 2018  路  9Comments  路  Source: swagger-api/swagger-ui

Look an example:

openapi: '3.0.0'
info:
  version: 1.0.0
  title: example
servers:
  - url: http://localhost:80

paths:
  /books:
    post:
      summary: add a book
      requestBody:
        content:
          application/xml:
            schema:
              description: book to be added
              allOf:
                - $ref: '#/components/schemas/book'
      responses:
        '200':
          description: OK
components:
  schemas:
    book:
      description: a book
      type: object
      properties:
        id:
          type: string
        author:
          type: string
        year:
          type: integer
          format: int32

Then if I click the 'Example Value' in swagger ui, it gives this info:

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

However, if I remove 'allOf':

openapi: '3.0.0'
info:
  version: 1.0.0
  title: example
servers:
  - url: http://localhost:80

paths:
  /books:
    post:
      summary: add a book
      requestBody:
        content:
          application/xml:
            schema:
              $ref: '#/components/schemas/book'
      responses:
        '200':
          description: OK
components:
  schemas:
    book:
      description: a book
      type: object
      properties:
        id:
          type: string
        author:
          type: string
        year:
          type: integer
          format: int32

It shows example value correctly:

<?xml version="1.0" encoding="UTF-8"?>
<book>
    <id>string</id>
    <author>string</author>
    <year>0</year>
</book>

And if I change application format to json, it shows json example value correctly whether there is allOf or not.

The reason I want to use allOf is for this.

So is this a bug of allOf or an expected behaviour?
If it's an expected behaviour, then how to solve this two problems?

Currently, I'm using Swagger Editor online version in Chrome browser.

This issue seems related.

rendering xml sample generation bug

All 9 comments

I found that this can be solved by adding an xml.name property from this issue.

openapi: '3.0.0'
info:
  version: 1.0.0
  title: example
servers:
  - url: http://localhost:80

paths:
  /books:
    post:
      summary: add a book
      requestBody:
        content:
          application/xml:
            schema:
              description: book to be added
              allOf:
                - $ref: '#/components/schemas/book'
              xml:
                name: book
      responses:
        '200':
          description: OK
components:
  schemas:
    book:
      description: a book
      type: object
      properties:
        id:
          type: string
        author:
          type: string
        year:
          type: integer
          format: int32

Or change to this:

openapi: '3.0.0'
info:
  version: 1.0.0
  title: example
servers:
  - url: http://localhost:80

paths:
  /books:
    post:
      summary: add a book
      requestBody:
        content:
          application/xml:
            schema:
              description: book to be added
              allOf:
                - $ref: '#/components/schemas/book'
      responses:
        '200':
          description: OK
components:
  schemas:
    book:
      description: a book
      xml:
        name: book
      type: object
      properties:
        id:
          type: string
        author:
          type: string
        year:
          type: integer
          format: int32

But if this is because of lackness of element name to construct xml, why not just use the type name book by default? I think this default value can deal with most cases.

Sounds like a good time to kick XML out your API !

Sorry I know is not helpful, I just could not resist...

@heldersepu
But the problem is that I was told to support both JSON and XML format. It's decided by my boss, not me!

So you mean this is a bug?

Convince your boss that XML is not as efficient as JSON and your job is done!

Ye, it looks like a bug & smells like a bug...
But seems that you already have a workaround so go with that
it might be a while before there is a fix for this.

@heldersepu
Not that easy! But I will have a try.
And it's an interesting question to compare JSON and XML. It seems JSON is on the winner side.

And I can tell you're a funny person.

Anyway, thx a lot!

Same issue :-(
Enterprise APIs relay on XML. JSON is primary for web development. XML can be transformed with XSLT, data can be found with XPath and defined with XSD. JSON ... OMG! Do you know any bank or health system, which works with JSON? I mean in core of systems, not web site integrations? Common, XML is here to stay for a long time.

@shockey We're facing issue with XML sample generation in case of collection is part of response object. Kindly let us know about ETA on a fix.

Added the demo project to reproduce the issue with Swashbuckle for Xml sample generation issue. Demo project
Kindly suggest the solution to view correct Xml sample generation.

Added the demo project to reproduce the issue with Swashbuckle for Xml sample generation issue. Demo project
Kindly suggest the solution to view correct Xml sample generation.

Any update on this?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

prabhat1790 picture prabhat1790  路  3Comments

MartinMuzatko picture MartinMuzatko  路  4Comments

ilnurshax picture ilnurshax  路  3Comments

fehguy picture fehguy  路  3Comments

andrecarlucci picture andrecarlucci  路  3Comments