-
Notifications
You must be signed in to change notification settings - Fork 238
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Vendor extension to control scoped middleware inheritance.
* The extension name is x-express-openapi-inherit-additional-middleware and it's value is false.
- Loading branch information
Showing
14 changed files
with
482 additions
and
9 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
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
35 changes: 35 additions & 0 deletions
35
test/sample-projects/with-inherit-additional-middleware-false-at-methodDoc/api-doc.js
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,35 @@ | ||
// args.apiDoc needs to be a js object. This file could be a json file, but we can't add | ||
// comments in json files. | ||
module.exports = { | ||
'x-express-openapi-additional-middleware': [/* generate a warning */ null, | ||
function(req, res, next) { | ||
req.apiDocAdded = true; | ||
next(); | ||
}], | ||
|
||
swagger: '2.0', | ||
|
||
// all routes will now have /v3 prefixed. | ||
basePath: '/v3', | ||
|
||
info: { | ||
title: 'express-openapi sample project', | ||
version: '3.0.0' | ||
}, | ||
|
||
definitions: {}, | ||
|
||
// paths are derived from args.routes. These are filled in by fs-routes. | ||
paths: { | ||
'/users/{id}': { | ||
'x-express-openapi-additional-middleware': [function(req, res, next) { | ||
req.pathDocAdded = true; | ||
next(); | ||
}] | ||
} | ||
}, | ||
|
||
tags: [ | ||
{name: 'users'} | ||
] | ||
}; |
76 changes: 76 additions & 0 deletions
76
...e-projects/with-inherit-additional-middleware-false-at-methodDoc/api-routes/users/{id}.js
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,76 @@ | ||
module.exports = { | ||
'x-express-openapi-additional-middleware': [function(req, res, next) { | ||
req.pathModuleAdded = true; | ||
next(); | ||
}], | ||
|
||
// parameters for all operations in this path | ||
parameters: [ | ||
{ | ||
name: 'id', | ||
in: 'path', | ||
type: 'string', | ||
required: true, | ||
description: 'Fred\'s age.' | ||
} | ||
], | ||
get: get | ||
}; | ||
|
||
function get(req, res) { | ||
res.status(200).json({ | ||
apiDocAdded: req.apiDocAdded || null, | ||
pathDocAdded: req.pathDocAdded || null, | ||
pathModuleAdded: req.pathModuleAdded || null | ||
}); | ||
} | ||
|
||
get.apiDoc = { | ||
'x-express-openapi-inherit-additional-middleware': false, | ||
description: 'Retrieve a user.', | ||
operationId: 'getUser', | ||
tags: ['users'], | ||
parameters: [ | ||
{ | ||
name: 'name', | ||
in: 'query', | ||
type: 'string', | ||
pattern: '^fred$', | ||
description: 'The name of this person. It may only be "fred".' | ||
}, | ||
// showing that operation parameters override path parameters | ||
{ | ||
name: 'id', | ||
in: 'path', | ||
type: 'integer', | ||
required: true, | ||
description: 'Fred\'s age.' | ||
}, | ||
{ | ||
name: 'age', | ||
in: 'query', | ||
type: 'integer', | ||
description: 'Fred\'s age.', | ||
default: 80 | ||
} | ||
], | ||
|
||
responses: { | ||
default: { | ||
description: 'showing that additional middleware should have been added at all levels.', | ||
schema: { | ||
properties: { | ||
apiDocAdded: { | ||
type: 'boolean' | ||
}, | ||
pathDocAdded: { | ||
type: 'boolean' | ||
}, | ||
pathModuleAdded: { | ||
type: 'boolean' | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}; |
26 changes: 26 additions & 0 deletions
26
test/sample-projects/with-inherit-additional-middleware-false-at-methodDoc/app.js
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,26 @@ | ||
var app = require('express')(); | ||
var bodyParser = require('body-parser'); | ||
// normally you'd just do require('express-openapi'), but this is for test purposes. | ||
var openapi = require('../../../'); | ||
var path = require('path'); | ||
var cors = require('cors'); | ||
|
||
app.use(cors()); | ||
app.use(bodyParser.json()); | ||
|
||
openapi.initialize({ | ||
apiDoc: require('./api-doc.js'), | ||
app: app, | ||
routes: path.resolve(__dirname, 'api-routes') | ||
}); | ||
|
||
app.use(function(err, req, res, next) { | ||
res.status(err.status).json(err); | ||
}); | ||
|
||
module.exports = app; | ||
|
||
var port = parseInt(process.argv[2]); | ||
if (port) { | ||
app.listen(port); | ||
} |
36 changes: 36 additions & 0 deletions
36
test/sample-projects/with-inherit-additional-middleware-false-at-pathDoc/api-doc.js
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,36 @@ | ||
// args.apiDoc needs to be a js object. This file could be a json file, but we can't add | ||
// comments in json files. | ||
module.exports = { | ||
'x-express-openapi-additional-middleware': [/* generate a warning */ null, | ||
function(req, res, next) { | ||
req.apiDocAdded = true; | ||
next(); | ||
}], | ||
|
||
swagger: '2.0', | ||
|
||
// all routes will now have /v3 prefixed. | ||
basePath: '/v3', | ||
|
||
info: { | ||
title: 'express-openapi sample project', | ||
version: '3.0.0' | ||
}, | ||
|
||
definitions: {}, | ||
|
||
// paths are derived from args.routes. These are filled in by fs-routes. | ||
paths: { | ||
'/users/{id}': { | ||
'x-express-openapi-inherit-additional-middleware': false, | ||
'x-express-openapi-additional-middleware': [function(req, res, next) { | ||
req.pathDocAdded = true; | ||
next(); | ||
}] | ||
} | ||
}, | ||
|
||
tags: [ | ||
{name: 'users'} | ||
] | ||
}; |
Oops, something went wrong.