Skip to content

Commit ed08dc9

Browse files
Merge pull request #6 from drGrove/master
Adds object merging to allow for swagger docs of same endpoint with different http methods
2 parents 94dcb57 + 7b88757 commit ed08dc9

File tree

4 files changed

+99
-5
lines changed

4 files changed

+99
-5
lines changed

example/routes.js

+40-1
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,43 @@ module.exports.setup = function(app) {
4444
app.post('/login', function(req, res) {
4545
res.json(req.body);
4646
});
47-
};
47+
48+
/**
49+
* @swagger
50+
* /users:
51+
* get:
52+
* description: Returns users
53+
* produces:
54+
* - application/json
55+
* responses:
56+
* 200:
57+
* description: users
58+
*/
59+
app.get('/users', function(req, res) {
60+
res.json({
61+
username: 'jsmith',
62+
});
63+
});
64+
65+
/**
66+
* @swagger
67+
* /users:
68+
* post:
69+
* description: Returns users
70+
* produces:
71+
* - application/json
72+
* parameters:
73+
* - name: username
74+
* description: username for user
75+
* in: formData
76+
* required: true
77+
* type: string
78+
* responses:
79+
* 200:
80+
* description: users
81+
*/
82+
app.post('/users', function(req, res) {
83+
res.json(req.body);
84+
});
85+
86+
};

lib/index.js

+24-2
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,27 @@ function filterJsDocComments(jsDocComments) {
6464
return swaggerJsDocComments;
6565
}
6666

67+
/**
68+
* Merges two objects
69+
* @function
70+
* @param {object} obj1 - Object 1
71+
* @param {object} obj2 - Object 2
72+
* @returns {object} Merged Object
73+
*/
74+
function objectMerge(obj1, obj2) {
75+
var obj3 = {};
76+
for (var attr in obj1) {
77+
if (obj1.hasOwnProperty(attr)) {
78+
obj3[attr] = obj1[attr];
79+
}
80+
}
81+
for (var name in obj2) {
82+
if (obj2.hasOwnProperty(name)) {
83+
obj3[name] = obj2[name];
84+
}
85+
}
86+
return obj3;
87+
}
6788

6889
/**
6990
* Adds the data in the Swagger JSDoc comments to the swagger object.
@@ -78,12 +99,13 @@ function addDataToSwaggerObject(swaggerObject, swaggerJsDocComments) {
7899

79100
for (var j = 0; j < propertyNames.length; j = j + 1) {
80101
var propertyName = propertyNames[j];
81-
swaggerObject.paths[propertyName] = pathObject[propertyName];
102+
swaggerObject.paths[propertyName] = objectMerge(
103+
swaggerObject.paths[propertyName], pathObject[propertyName]
104+
);
82105
}
83106
}
84107
}
85108

86-
87109
/**
88110
* Generates the swagger spec
89111
* @function

test/swagger-spec.json

+34-1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,39 @@
4646
}
4747
}
4848
}
49+
},
50+
"/users": {
51+
"get": {
52+
"description": "Returns users",
53+
"produces": [
54+
"application/json"
55+
],
56+
"responses": {
57+
"200": {
58+
"description": "users"
59+
}
60+
}
61+
},
62+
"post": {
63+
"description": "Returns users",
64+
"produces": [
65+
"application/json"
66+
],
67+
"parameters": [
68+
{
69+
"name": "username",
70+
"description": "username for user",
71+
"in": "formData",
72+
"required": true,
73+
"type": "string"
74+
}
75+
],
76+
"responses": {
77+
"200": {
78+
"description": "users"
79+
}
80+
}
81+
}
4982
}
5083
}
51-
}
84+
}

test/test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,4 @@ describe('swagger spec', function() {
6666
});
6767
});
6868

69-
});
69+
});

0 commit comments

Comments
 (0)