4371

How to prevent xml-wrapper element in Swagger-ui

I am using swagger-ui version 2.2.8

Our existing API can produce application/json as well as application/xml. For a single record result in json it produces:

{ "person": { "id": 23, "name": "John" } }

and for XML it produces:

<person> <id>23</id> <name>John</name> </person>

My swagger-schema for this is:

{ "person": { "type": "object", "properties": { "person": { "$ref": "#/definitions/personfields" } } } }

When viewed in swagger-ui the json model is looking fine. However the XML-model becomes:

<person> <person> <id>1</id> <name>string</name> </person> </person>

Is there a way to prevent this double <person> but still get the correct JSON result?

Answer1:

In OpenAPI terms, your JSON and XML models are different – the JSON version of

<person> <id>23</id> <name>John</name> </person>

would be

{ "id": 23, "name": "John" }

without the wrapper property person.

You can't have a single schema for models that differ in this way.

What you can do is define your model as a free-form object (type: object without properties) and specify the JSON/XML response examples instead – but in this case you lose the ability to define the object properties.

definitions: person: type: object paths: /person: get: produces: - application/json - application/xml responses: 200: description: OK schema: $ref: '#/definitions/person' examples: application/json: | { "person": { "id": 23, "name": "John" } } application/xml: | <person> <id>23</id> <name>John</name> </person>

Note: If you use Swagger UI, use version 3.0.x becase 2.2.x does not display such examples correctly.

In the next version – OpenAPI 3.0 (which is RC at the moment of writing) – it will be possible to specify different schemas for different response MIME types. So in 3.0 your example will be described as:

paths: /person: get: responses: '200': description: OK content: application/json: $ref: '#/components/definitions/PersonJson' application/xml: $ref: '#/components/definitions/Person' components: definitions: # XML object schema Person: type: object properties: id: type: integer example: 23 name: type: string example: John xml: name: person # JSON object schema with a wrapper property "person" PersonJson: type: object properties: person: $ref: '#/components/definitions/Person'

Recommend

  • Greek letters in a GUI - PYTHON
  • What's the point of nonfinal singleton objects in scala?
  • Class implementation in a header file == bad style? [duplicate]
  • 'include' of functions in groovy scripts
  • Nested projects in multiproject visual studio templates
  • Retaining data after updating application
  • Examples of how to a STS in .Net 4.5 using WCF
  • Monotouch crashes with NullReferenceException on non nullable object
  • Access object instance inside an event handler
  • Extract All Possible Paths from Expression-Tree and evaluate them to hold TRUE
  • Cloud Code function running twice
  • How to get links to open in the native browser in iOS Meteor apps?
  • Image map in Flex
  • Pycharm: Marking a folder as 'sources root' is not recursive for subfolders
  • Implicit joins and Where in Doctrine - how?
  • Web.config system.webserver errors
  • gspread or such: help me get cell coordinates (not value)
  • How do I exclude a dependency in provided scope when running in Maven test scope?
  • ActiveRecord query for a count of new users by day
  • Q promise. Difference between .when and .then
  • Function pointer “assignment from incompatible pointer type” only when using vararg ellipsis
  • 0x202A in filename: Why?
  • AT Commands to Send SMS not working in Windows 8.1
  • How to format a variable of double type
  • VB.net deserialize, JSON Conversion from type 'Dictionary(Of String,Object)' to type '
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • C# - Getting references of reference
  • How do I configure my settings file to work with unit tests?
  • File not found error Google Drive API
  • How to get Windows thread pool to call class member function?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Is it possible to post an object from jquery to bottle.py?
  • costura.fody for a dll that references another dll
  • Binding checkboxes to object values in AngularJs
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal