Skip to content
This repository has been archived by the owner on Sep 14, 2022. It is now read-only.

Commit

Permalink
fix for #28, #29
Browse files Browse the repository at this point in the history
  • Loading branch information
fehguy committed Jan 9, 2013
1 parent b088e84 commit be43553
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 8 deletions.
37 changes: 32 additions & 5 deletions Common/node/swagger.js
Expand Up @@ -80,7 +80,7 @@ function setResourceListingPaths(app) {
return stopWithError(res, {'description': 'internal error', 'code': 500});
}
else {
setHeaders(res);
exports.setHeaders(res);
var data = filterApiListing(req, res, r);
data.basePath = basePath;
if (data.code) {
Expand Down Expand Up @@ -148,6 +148,7 @@ function filterApiListing(req, res, r) {
}
else {
clonedApi.operations.push(JSON.parse(JSON.stringify(operation)));
addModelsFromPost(operation, requiredModels);
addModelsFromResponse(operation, requiredModels);
}
}
Expand Down Expand Up @@ -206,6 +207,31 @@ function filterApiListing(req, res, r) {
return output;
}

// Add model to list and parse List[model] elements
function addModelsFromPost(operation, models){
if(operation.parameters) {
for(var i in operation.parameters) {
var param = operation.parameters[i];
if(param.paramType == "body" && param.dataType) {
var model = param.dataType.replace(/^List\[/,"").replace(/\]/,"");
if(models.indexOf(model) < 0) {
models.push(responseModel);
}
models.push(param.dataType);
}
}
}

var responseModel = operation.responseClass;
if (responseModel) {
responseModel = responseModel.replace(/^List\[/,"").replace(/\]/,"");
if (models.indexOf(responseModel) < 0) {
models.push(responseModel);
}
}
}


// Add model to list and parse List[model] elements
function addModelsFromResponse(operation, models){
var responseModel = operation.responseClass;
Expand Down Expand Up @@ -257,7 +283,7 @@ function resourceListing(req, res) {
r.apis.push({"path": p, "description": "none"});
}

setHeaders(res);
exports.setHeaders(res);
res.write(JSON.stringify(r));
res.end();
}
Expand Down Expand Up @@ -299,7 +325,7 @@ function addMethod(app, callback, spec) {
var currentMethod = spec.method.toLowerCase();
if (allowedMethods.indexOf(currentMethod)>-1) {
app[currentMethod](fullPath, function(req,res) {
setHeaders(res);
exports.setHeaders(res);
if (!canAccessResource(req, req.url.substr(1).split('?')[0].replace('.json', '.*'), req.method)) {
res.send(JSON.stringify({"description":"forbidden", "code":403}), 403);
} else {
Expand Down Expand Up @@ -477,7 +503,7 @@ function error(code, description) {

// Stop express ressource with error code
function stopWithError(res, error) {
setHeaders(res);
exports.setHeaders(res);
if (error && error.description && error.code)
res.send(JSON.stringify(error), error.code);
else
Expand Down Expand Up @@ -533,4 +559,5 @@ exports.addModels = addModels;
exports.setAppHandler = setAppHandler;
exports.discover = discover;
exports.discoverFile = discoverFile;
exports.configureSwaggerPaths = configureSwaggerPaths;
exports.configureSwaggerPaths = configureSwaggerPaths;
exports.setHeaders = setHeaders;
24 changes: 22 additions & 2 deletions README.md
Expand Up @@ -26,7 +26,9 @@ swagger.setAppHandler(app);
You can optionally add a validator function, which is used to filter the swagger json and request operations:

```js
/* This is a sample validator. It simply says that for _all_ POST, DELETE, PUT methods, the header `api_key` OR query param `api_key` must be equal to the string literal `special-key`. All other HTTP ops are A-OK */
// This is a sample validator. It simply says that for _all_ POST, DELETE, PUT methods,
// the header api_key OR query param api_key must be equal to the string literal
// special-key. All other HTTP ops are A-OK */

swagger.addValidator(
function validate(req, path, httpMethod) {
Expand Down Expand Up @@ -103,6 +105,8 @@ Now you can open up a [swagger-ui](https://github.com/wordnik/swagger-ui) and br

### Other Configurations

#### .{format} suffix removal

If you don't like the .{format} or .json suffix, you can override this before configuring swagger:

```js
Expand All @@ -121,6 +125,8 @@ var findById = {
...
```
#### Mapping swagger to subpaths
To add a subpath to the api (i.e. list your REST api under `/api` or `/v1`), you can configure express as follows:
```js
Expand All @@ -133,4 +139,18 @@ app.use("/v1", subpath);
swagger.setAppHandler(subpath);
```
Now swagger and all apis configured through it will live under the `/v1` path (i.e. `/v1/api-docs.json`).
Now swagger and all apis configured through it will live under the `/v1` path (i.e. `/v1/api-docs.json`).
#### Allows-origin and special headers
If you want to modify the default headers sent with every swagger-managed method, you can do so as follows:
```js
swagger.setHeaders = function setHeaders(res) {
res.header('Access-Control-Allow-Origin', "*");
res.header("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT");
res.header("Access-Control-Allow-Headers", "Content-Type, X-API-KEY");
res.header("Content-Type", "application/json; charset=utf-8");
};
```
If you have a special name for an api key (such as `X-API-KEY`, per above), this is where you can inject it.
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "swagger-node-express",
"version": "1.2.1",
"version": "1.2.2",
"author": {
"name": "Tony Tam",
"email": "fehguy@gmail.com",
Expand Down

0 comments on commit be43553

Please sign in to comment.