forked from apigee-127/swagger-tools
-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
read and assign parameters to controllers automatically
- Loading branch information
Showing
5 changed files
with
88 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
'use strict'; | ||
|
||
import {isEmpty} from 'lodash'; | ||
|
||
export class SwaggerParameters { | ||
checkParameters() { | ||
const concatArrays = (array, arrayToAttach) => { | ||
if (isEmpty(arrayToAttach)) { | ||
return array; | ||
} | ||
return array.concat(arrayToAttach); | ||
}; | ||
|
||
return (req, res, next) => { | ||
if (!req.openapi || !req.openapi.schema || !req.openapi.schema.parameters) { | ||
next(); | ||
return; | ||
} | ||
let swaggerParameters = Array<any>(); | ||
const queryParams = Array<any>(); | ||
const headerParams = Array<any>(); | ||
const cookieParams = Array<any>(); | ||
const pathParams = Array<any>(); | ||
|
||
swaggerParameters.push(req); | ||
swaggerParameters.push(res); | ||
swaggerParameters.push(next); | ||
|
||
if (!isEmpty(req.body)) { | ||
swaggerParameters.push(req.body); | ||
} | ||
|
||
if (!isEmpty(req.openapi.pathParams)) { | ||
Object.keys(req.openapi.pathParams).forEach(function(key) { | ||
pathParams.push(req.openapi.pathParams[key]); | ||
}); | ||
} | ||
|
||
const parameters = req.openapi.schema.parameters; | ||
for (let i = 0; i < parameters.length; i++) { | ||
const parameter = parameters[i]; | ||
if (parameter.in === 'query') { | ||
queryParams.push(req.query[parameter.name]); | ||
} else if (parameter.in === 'cookie') { | ||
cookieParams.push(req.cookies[parameter.name]); | ||
} else if (parameter.in === 'header') { | ||
headerParams.push(req.headers[parameter.name]); | ||
} | ||
} | ||
|
||
swaggerParameters = concatArrays(swaggerParameters, queryParams); | ||
swaggerParameters = concatArrays(swaggerParameters, pathParams); | ||
swaggerParameters = concatArrays(swaggerParameters, headerParams); | ||
swaggerParameters = concatArrays(swaggerParameters, cookieParams); | ||
|
||
req.openapi.swaggerParameters = swaggerParameters; | ||
|
||
next(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters