diff --git a/test/assets/project/api/controllers/hello_world.js b/test/assets/project/api/controllers/hello_world.js index 830070d..5fc9a51 100644 --- a/test/assets/project/api/controllers/hello_world.js +++ b/test/assets/project/api/controllers/hello_world.js @@ -4,6 +4,7 @@ var util = require('util'); module.exports = { hello: hello, + hello_array: hello_array, hello_body: hello_body, hello_file: hello_file, get: hello, @@ -17,6 +18,22 @@ function hello(req, res) { res.json(hello); } +function hello_array(req, res) { + var hello = 'Hello'; + var names = req.swagger.params.names.value + for (let n =0; n < names.length; n ++) { + if (names.hasOwnProperty(n)) { + if ((n + 1) === names.length){ + hello += " and " + }else { + hello += ", " + } + hello += names[n] + } + } + res.json(hello); +} + function hello_body(req, res) { var name = req.swagger.params.nameRequest.value.name || 'stranger'; var hello = util.format('Hello, %s!', name); @@ -40,4 +57,4 @@ function hello_text_body(req, res) { var name = req.swagger.params.name.value || 'stranger'; var hello = util.format('Hello, %s!', name); res.json(hello); -} \ No newline at end of file +} diff --git a/test/assets/project/api/swagger/swagger.yaml b/test/assets/project/api/swagger/swagger.yaml index 394a40f..2d76632 100644 --- a/test/assets/project/api/swagger/swagger.yaml +++ b/test/assets/project/api/swagger/swagger.yaml @@ -37,6 +37,61 @@ paths: description: Error schema: $ref: "#/definitions/ErrorResponse" + /hello_w_default: + x-swagger-router-controller: hello_world + get: + description: Returns 'Hello' to the caller + operationId: hello + parameters: + - name: name + in: query + description: The name of the person to whom to say hello + default: "no stranger" + type: string + responses: + 200: + description: Success + schema: + type: object + $ref: "#/definitions/HelloWorldResponse" + examples: + application/json: + message: 'An example message' + application/x-yaml: + message: 'A yaml example' + default: + description: Error + schema: + $ref: "#/definitions/ErrorResponse" + /hello_w_default_array: + x-swagger-router-controller: hello_world + get: + description: Returns 'Hello' to the caller + operationId: hello_array + parameters: + - name: names + in: query + description: The names of the people to whom to say hello + default: ["Tom", "Maria"] + type: array + items: + type: string + collectionFormat: csv + responses: + 200: + description: Success + schema: + type: object + $ref: "#/definitions/HelloWorldResponse" + examples: + application/json: + message: 'An example message' + application/x-yaml: + message: 'A yaml example' + default: + description: Error + schema: + $ref: "#/definitions/ErrorResponse" /multiple_writes: x-swagger-router-controller: hello_world get: diff --git a/test/lib/common.js b/test/lib/common.js index 04c051f..0d39ded 100644 --- a/test/lib/common.js +++ b/test/lib/common.js @@ -23,6 +23,58 @@ module.exports = function() { }); }); + it('should execute with a default name', function(done) { + request(this.app) + .get('/hello_w_default') + .set('Accept', 'application/json') + .expect(200) + .expect('Content-Type', /json/) + .end(function(err, res) { + should.not.exist(err); + res.body.should.eql('Hello, no stranger!'); + done(); + }); + }); + + it('should execute and ignore default name if name is provided', function(done) { + request(this.app) + .get('/hello_w_default?name=Peter') + .set('Accept', 'application/json') + .expect(200) + .expect('Content-Type', /json/) + .end(function(err, res) { + should.not.exist(err); + res.body.should.eql('Hello, Peter!'); + done(); + }); + }); + + it('default names should be replaced with new parameter if supplied', function(done) { + request(this.app) + .get('/hello_w_default_array?names[]=Tom&names[]=Peter') + .set('Accept', 'application/json') + .expect(200) + .expect('Content-Type', /json/) + .end(function(err, res) { + should.not.exist(err); + res.body.should.eql('Hello, Tom and Peter'); + done(); + }); + }); + + it('should execute with default names', function(done) { + request(this.app) + .get('/hello_w_default_array') + .set('Accept', 'application/json') + .expect(200) + .expect('Content-Type', /json/) + .end(function(err, res) { + should.not.exist(err); + res.body.should.eql('Hello, Tom and Maria'); + done(); + }); + }); + it('should execute without operationId', function(done) { request(this.app) .get('/hello_no_operationid')