Я пытаюсь получить проект hello world "из коробки", сгенерированный swagger project create
, чтобы вернуть ответ xml вместо json.Вот шаги, которые я предпринял:
1) запустил swagger project create
и выбрал экспресс-вариант.
2) открыл swagger.yaml и сделал следующее изменение:
# format of the responses to the client (Accepts)
produces:
- application/xml
Вот весь swagger.yaml, если вам интересно:
swagger: "2.0"
info:
version: "0.0.1"
title: Hello World App
# during dev, should point to your local machine
host: localhost:10010
# basePath prefixes all resource paths
basePath: /
#
schemes:
# tip: remove http to make production-grade
- http
- https
# format of bodies a client can send (Content-Type)
consumes:
- application/json
# format of the responses to the client (Accepts)
produces:
- application/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
parameters:
- name: name
in: query
description: The name of the person to whom to say hello
required: false
type: string
responses:
"200":
description: Success
schema:
# a pointer to a definition
$ref: "#/definitions/HelloWorldResponse"
# responses may fall through to errors
default:
description: Error
schema:
$ref: "#/definitions/ErrorResponse"
/swagger:
x-swagger-pipe: swagger_raw
# complex objects have schema definitions
definitions:
HelloWorldResponse:
required:
- message
properties:
message:
type: string
ErrorResponse:
required:
- message
properties:
message:
type: string
3) открыл контроллер hello_world.js и изменил объект res, чтобы он не был json, и установите тип application/xml
.Я также добавил jstoxml в проект, чтобы превратить объект в xml.Вот код:
'use strict';
var util = require('util');
const { toXML } = require('jstoxml');
module.exports = {
hello: hello
};
function hello(req, res) {
// 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 = {message: 'hello'};
var xml = toXML(hello);
res.type('application/xml');
res.end(xml);
}
4) запустил swagger project start
, а затем запустил следующий запрос curl:
curl -X GET "http://127.0.0.1:10010/hello?name=Scott" -H "accept: application/xml"
5) Получил следующий ответ:
{"message":"Response validation failed: value expected to be an array/object but is not","failedValidation":true,"originalResponse":"<message>hello</message>"}
Сервер выдает следующую ошибку:
SyntaxError: Response validation failed: value expected to be an array/object but is not
at JSON.parse (<anonymous>)
at validateValue (/Users/seanroberts/projects/hello-xml/node_modules/swagger-tools/middleware/swagger-validator.js:125:20)
at ServerResponse.res.end (/Users/seanroberts/projects/hello-xml/node_modules/swagger-tools/middleware/swagger-validator.js:252:9)
at hello (/Users/seanroberts/projects/hello-xml/api/controllers/hello_world.js:45:7)
at swaggerRouter (/Users/seanroberts/projects/hello-xml/node_modules/swagger-tools/middleware/swagger-router.js:407:20)
at swagger_router (/Users/seanroberts/projects/hello-xml/node_modules/swagger-node-runner/fittings/swagger_router.js:31:5)
at Runner.<anonymous> (/Users/seanroberts/projects/hello-xml/node_modules/bagpipes/lib/bagpipes.js:171:7)
at bound (domain.js:301:14)
at Runner.runBound (domain.js:314:12)
at Runner.pipeline (/Users/seanroberts/projects/hello-xml/node_modules/pipeworks/pipeworks.js:72:17)
Что я делаю не так?