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 support #31

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions examples/petstore/api/petstore.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,6 @@ components:
example: doggie
photoUrls:
type: array
xml:
name: photoUrl
wrapped: true
items:
type: string
status:
Expand Down
24 changes: 24 additions & 0 deletions examples/petstore/example.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

var path = require('path');
var http = require('http');
var xml2js = require('xml2js');

var oas3Tools = require('oas3-tools');
var serverPort = 8080;
Expand All @@ -11,6 +12,24 @@ function validate(request, scopes, schema) {
return true;
}

function doNothing(value, name){
//console.log(name, value);
return value;
}

function sanitiser(req, res, next){
//console.log(req.body);
let body = req.body;
if (body.photoUrls){
if (!Array.isArray(body.photoUrls) && typeof body.photoUrls === 'string'){
body.photoUrls = [body.photoUrls];
}
}
//console.log(body);
req.body = body;
next();
}

// swaggerRouter configuration
var options = {
routing: {
Expand All @@ -28,6 +47,11 @@ var options = {
api_key: validate
}
}
},
xml:{
tagNameProcessors: [xml2js.processors.stripPrefix],
valueProcessors: [xml2js.processors.parseNumbers, xml2js.processors.parseBooleans, doNothing],
sanitiseProcessors: sanitiser
}
};

Expand Down
18 changes: 14 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@
"description": "Swagger-UI and API Rest Routing for Open API v3.",
"main": "dist/index.js",
"scripts": {
"build": "rm -rf dist/ && tsc && cp -R src/middleware/swagger-ui dist/middleware/swagger-ui"
"clean:dist": "rimraf dist",
"setup:dist": "make-dir dist",
"copy:middleware": "copyfiles -u 1 \"src/middleware/swagger-ui/**\" ./dist",
"build:tsc": "tsc",
"build:prod": "run-s build:tsc copy:middleware",
"build": "run-s clean:dist setup:dist build:prod"
},
"author": {
"name": "Hugo Mercado",
Expand Down Expand Up @@ -33,18 +38,19 @@
"dependencies": {
"async": "^2.6.3",
"body-parser": "1.19.0",
"body-parser-xml": "2.0.1",
"cookie-parser": "^1.4.4",
"debug": "^4.1.1",
"serve-static": "^1.14.1",
"express": "^4.17.1",
"express": "4.17.1",
"express-openapi-validator": "^4.4.3",
"js-yaml": "^3.13.1",
"lodash": "^4.17.15",
"morgan": "^1.9.1",
"multer": "^1.4.2",
"parseurl": "^1.3.3",
"path-to-regexp": "^3.2.0",
"qs": "^6.9.1"
"qs": "^6.9.1",
"serve-static": "^1.14.1"
},
"devDependencies": {
"@types/express": "^4.17.0",
Expand All @@ -55,6 +61,10 @@
"@types/js-yaml": "^3.12.1",
"@types/serve-static": "^1.13.3",
"awesome-typescript-loader": "^5.2.1",
"copyfiles": "2.4.0",
"make-dir-cli": "^2.0.0",
"npm-run-all": "^4.1.5",
"rimraf": "^3.0.2",
"source-map-loader": "^0.2.4",
"ts-node": "^8.3.0",
"tsc": "^1.20150623.0",
Expand Down
48 changes: 48 additions & 0 deletions src/middleware/express.app.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import * as express from 'express';
import cookieParser = require('cookie-parser');
import bodyParser = require('body-parser');
import * as bodyParserXml from 'body-parser-xml';
import * as xml2js from 'xml2js';
import { SwaggerUI } from './swagger.ui';
import { SwaggerRouter } from './swagger.router';
import { SwaggerParameters } from './swagger.parameters';
Expand Down Expand Up @@ -31,6 +33,11 @@ export class ExpressAppConfig {
this.app.use(bodyParser.text());
this.app.use(bodyParser.json());

this.app.use(this.configureXmlParser(appOptions));
this.app.use(this.configureXmlSanitiser(appOptions));

// this.app.use(function (req, res, next){console.log(req.body);next();});

this.app.use(this.configureLogger(appOptions.logging));
this.app.use(express.json());
this.app.use(express.urlencoded({ extended: false }));
Expand All @@ -46,6 +53,47 @@ export class ExpressAppConfig {
this.app.use(this.errorHandler);
}

private configureXmlParser(appOptions: Oas3AppOptions){
bodyParserXml(bodyParser);
const bodyParserPlusXMLParser: any = bodyParser;
let xmlTagNameProcessors = [xml2js.processors.stripPrefix];
let xmlValueProcessors = [xml2js.processors.parseNumbers, xml2js.processors.parseBooleans];
let xmlOptions = appOptions.xml;
if (xmlOptions != undefined) {
if(xmlOptions.tagNameProcessors != undefined
&& Array.isArray(xmlOptions.tagNameProcessors)){
xmlTagNameProcessors = xmlOptions.tagNameProcessors;
}
if(xmlOptions.valueProcessors != undefined
&& Array.isArray(xmlOptions.valueProcessors)){
xmlValueProcessors = xmlOptions.valueProcessors;
}

}
return bodyParserPlusXMLParser.xml({
xmlParseOptions: {
mergeAttrs :true,
normalize: true,
normalizeTags: false,
explicitRoot :false,
explicitArray: false,
tagNameProcessors:xmlTagNameProcessors,
valueProcessors: xmlValueProcessors
}
});
}

private configureXmlSanitiser(appOptions: Oas3AppOptions){
let xmlSanitizerProcessor = function(req, res, next){next()};
let xmlOptions = appOptions.xml;
if (xmlOptions != undefined) {
if(xmlOptions.sanitiseProcessors != undefined){
xmlSanitizerProcessor = xmlOptions.sanitiseProcessors;
}
}
return xmlSanitizerProcessor;
}

private setOpenApiValidatorOptions(definitionPath: string, appOptions: Oas3AppOptions) {
//If no options or no openApiValidator Options given, create empty options with api definition path
if (!appOptions || !appOptions.openApiValidator) {
Expand Down
7 changes: 6 additions & 1 deletion src/middleware/oas3.options.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
import { OpenApiValidatorOpts } from 'express-openapi-validator/dist/framework/types';
import { LoggingOptions } from './logging.options'
import { SwaggerUiOptions } from './swagger.ui.options';
import { XmlOptions } from './xml.options';

export class Oas3AppOptions {
public routing: any;
public openApiValidator: OpenApiValidatorOpts;
public logging: LoggingOptions;
public swaggerUI: SwaggerUiOptions;
public xml: XmlOptions;

constructor(routingOpts: any, openApiValidatorOpts: OpenApiValidatorOpts, logging: LoggingOptions, swaggerUI: SwaggerUiOptions) {
constructor(routingOpts: any, openApiValidatorOpts: OpenApiValidatorOpts, logging: LoggingOptions, swaggerUI: SwaggerUiOptions, xml: XmlOptions) {
this.routing = routingOpts;
this.openApiValidator = openApiValidatorOpts;
this.swaggerUI = swaggerUI;
if (!logging)
logging = new LoggingOptions(null, null);
this.logging = logging;
if (!xml)
xml = new XmlOptions(null, null, null);
this.xml = xml;
}
}
12 changes: 12 additions & 0 deletions src/middleware/xml.options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export class XmlOptions {
public tagNameProcessors: 'array';
public valueProcessors: 'array';
public sanitiseProcessors: (req, res, next) => any;

constructor(tagNameProcessors: 'array', valueProcessors: 'array', sanitiseProcessors: (req, res, next) => any)
{
this.tagNameProcessors = tagNameProcessors;
this.valueProcessors = valueProcessors;
this.sanitiseProcessors = sanitiseProcessors;
}
}