@@ -4,6 +4,8 @@ var debug = require('debug')('swagger:swagger_router');
44var path = require ( 'path' ) ;
55var assert = require ( 'assert' ) ;
66var SWAGGER_ROUTER_CONTROLLER = 'x-swagger-router-controller' ;
7+ var CONTROLLER_INTERFACE_TYPE = 'x-controller-interface' ;
8+ var allowedCtrlInterfaces = [ "middleware" , "pipe" , "auto-detect" ] ;
79var util = require ( 'util' ) ;
810
911module . exports = function create ( fittingDef , bagpipes ) {
@@ -12,8 +14,25 @@ module.exports = function create(fittingDef, bagpipes) {
1214
1315 assert ( Array . isArray ( fittingDef . controllersDirs ) , 'controllersDirs must be an array' ) ;
1416 assert ( Array . isArray ( fittingDef . mockControllersDirs ) , 'mockControllersDirs must be an array' ) ;
17+
18+ if ( ! fittingDef . controllersInterface ) fittingDef . controllersInterface = "middleware" ;
19+ assert ( ~ allowedCtrlInterfaces . indexOf ( fittingDef . controllersInterface ) ,
20+ 'value in swagger_router config.controllersInterface - can be one of ' + allowedCtrlInterfaces + ' but got: ' + fittingDef . controllersInterface
21+ ) ;
1522
1623 var swaggerNodeRunner = bagpipes . config . swaggerNodeRunner ;
24+ swaggerNodeRunner . api . getOperations ( ) . forEach ( function ( operation ) {
25+ var interfaceType =
26+ operation . controllerInterface =
27+ operation . definition [ CONTROLLER_INTERFACE_TYPE ] ||
28+ operation . pathObject . definition [ CONTROLLER_INTERFACE_TYPE ] ||
29+ swaggerNodeRunner . api . definition [ CONTROLLER_INTERFACE_TYPE ] ||
30+ fittingDef . controllersInterface ;
31+
32+ assert ( ~ allowedCtrlInterfaces . indexOf ( interfaceType ) ,
33+ 'whenever provided, value of ' + CONTROLLER_INTERFACE_TYPE + ' directive in openapi doc must be one of ' + allowedCtrlInterfaces + ' but got: ' + interfaceType ) ;
34+ } )
35+
1736 var appRoot = swaggerNodeRunner . config . swagger . appRoot ;
1837 var dependencies = swaggerNodeRunner . config . swagger . dependencies
1938
@@ -62,12 +81,27 @@ module.exports = function create(fittingDef, bagpipes) {
6281
6382 if ( controller ) {
6483
65- var operationId = operation . definition [ 'operationId' ] || context . request . method . toLowerCase ( ) ;
84+ var operationId = operation . definition . operationId || context . request . method . toLowerCase ( ) ;
85+ var ctrlType =
86+ operation . definition [ 'x-controller-type' ] ||
87+ operation . pathObject . definition [ 'x-controller-type' ] ||
88+ operation . pathObject . api . definition [ 'x-controller-type' ] ;
89+
6690 var controllerFunction = controller [ operationId ] ;
6791
6892 if ( controllerFunction && typeof controllerFunction === 'function' ) {
69- debug ( 'running controller' ) ;
70- return controllerFunction ( context . request , context . response , cb ) ;
93+ if ( operation . controllerInterface == 'auto-detect' ) {
94+ operation . controllerInterface =
95+ controllerFunction . length == 3
96+ ? 'middleware'
97+ : 'pipe' ;
98+ debug ( "auto-detected interface-type for operation '%s' at [%s] as '%s'" , operationId , operation . pathToDefinition , operation . controllerInterface )
99+ }
100+
101+ debug ( 'running controller, as %s' , operation . controllerInterface ) ;
102+ return operation . controllerInterface == 'pipe'
103+ ? controllerFunction ( context , cb )
104+ : controllerFunction ( context . request , context . response , cb ) ;
71105 }
72106
73107 var msg = util . format ( 'Controller %s doesn\'t export handler function %s' , controllerName , operationId ) ;
0 commit comments