How to create / consume XML using swagger-node

I'm new to using swagger-node (swagger-spec 2.0) and I need my API to consume and produce both XML and JSON (because that's what the client wants). I am currently only focusing on the "production" part.

While building the answer, I know I can turn my js object to XML using tools like jstoxml or easyxml . So the question is, is this necessary when using swagger-node, or are the tools supposed to handle it? I think I need help on what the controller code should return.

For example create a new project using swagger swagger project create myproject (choose express framework)

Change the yaml file for the /hello

api to get:

return both json and xml

paths:
  /hello:
    # binds a127 app logic to a route
    x-swagger-router-controller: hello_world
    get:
      description: Returns 'Hello' to the caller
      # used as the method name of the controller
      operationId: hello
      produces:
        - application/json
        - application/xml

      

Then change the hello_world.js controller to return a json object instead of a string

  // variables defined in the Swagger document can be referenced using req.swagger.params.{parameter_name}
  var name = req.swagger.params.name.value || 'stranger';
  var hello = util.format('Hello, %s!', name);

  // this sends back a JSON response which is a single string
  res.json({message:hello});
}

      

When I start the project and use Postman with Accept = application / json header I get the response:

{
    "message": "Hello, stranger!"
}

      

If I change the header Accept application/xml

, I still get a JSON response, not XML. I was hoping to see the following:

<object>
<message>Hello, stranger!</message>
</object>

      

I know my code is wrong to use res.json()

because I believe it sets Content-Type to application/json

.

I don't know what else to use to generate the XML response. When I change res.json () to use easyxml

var xml = easyxml.render({message:hello});
res.type('xml').end(xml);

      

Then I get a validation error from swagger:

[
  {
    "status": 500,
    "message": "Response validation failed: value expected to be an array/object but is not"
  }
]

      

So how should my controller format the response to return XML or JSON?

+3


source to share


2 answers


I'm not really sure what easyxml does or why it doesn't work, but it jstoxml

works great:

var jstoxml = require('jstoxml');
var express = require('express');
var util = require('util');
var app = express();

app.get('/', function(req, res) {
    var name = 'stranger';
    var hello = {
        object: {
            message: util.format('Hello, %s!', name)
        }
    };
    if (req.headers.accept === 'application/xml') {
        res.type('xml')
        res.end(jstoxml.toXML(hello));
    } else {
        res.json(hello);
    }
});

app.listen(process.env.PORT || 8100);

      

Accept: application / json



{
    "object": 
    {
        "message": "Hello, stranger!"
    }
}

      

Accept: application / xml

<object>
    <message>Hello, stranger!</message>
</object>

      

+1


source


Apparently there is no correct way . remus' ignores Expand XML Swagger support . In any case, the controller should definitely not interfere with the API I / O formats, but simply execute the business logic.



0


source







All Articles