Permalink
Browse files

fix for #28, #29

  • Loading branch information...
1 parent b088e84 commit be43553d315c1b5a64c0027bd5ed3bc3d92c04a1 @fehguy fehguy committed Jan 9, 2013
Showing with 55 additions and 8 deletions.
  1. +32 −5 Common/node/swagger.js
  2. +22 −2 README.md
  3. +1 −1 package.json
View
37 Common/node/swagger.js
@@ -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) {
@@ -148,6 +148,7 @@ function filterApiListing(req, res, r) {
}
else {
clonedApi.operations.push(JSON.parse(JSON.stringify(operation)));
+ addModelsFromPost(operation, requiredModels);
addModelsFromResponse(operation, requiredModels);
}
}
@@ -207,6 +208,31 @@ function filterApiListing(req, res, r) {
}
// 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;
if (responseModel) {
@@ -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();
}
@@ -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 {
@@ -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
@@ -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;
View
24 README.md
@@ -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) {
@@ -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
@@ -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
@@ -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.
View
2 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",

0 comments on commit be43553

Please sign in to comment.