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 cannot be generated with 'allOf' #4423

Open
iambingoNJU opened this issue Apr 9, 2018 · 9 comments
Open

XML example cannot be generated with 'allOf' #4423

iambingoNJU opened this issue Apr 9, 2018 · 9 comments

Comments

@iambingoNJU
Copy link

iambingoNJU commented Apr 9, 2018

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.

@iambingoNJU
Copy link
Author

iambingoNJU commented Apr 9, 2018

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.

@heldersepu
Copy link
Contributor

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

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

@iambingoNJU
Copy link
Author

@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?

@heldersepu
Copy link
Contributor

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.

@iambingoNJU
Copy link
Author

@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!

@kalatchev
Copy link

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.

@euroform-pa
Copy link

@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.

@euroform-pa
Copy link

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.

@euroform-pa
Copy link

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants