Skip to content
This repository has been archived by the owner on Mar 15, 2019. It is now read-only.

Commit

Permalink
fix missing response example bug in swagger export
Browse files Browse the repository at this point in the history
  • Loading branch information
rainum committed Dec 13, 2016
1 parent 1d912e5 commit b03ac3c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 11 deletions.
26 changes: 15 additions & 11 deletions lib/exporters/swagger.js
Original file line number Diff line number Diff line change
Expand Up @@ -321,21 +321,25 @@ function mapResponseBody(res, mimeType) {
return item;
}

// In Stoplight models endpoint default produces is `[]`. We need to allow it
// to be `undefined` and we will be able to detect whenever endpoint produces
// does not inherit environment value or just not exists
Swagger.prototype._mapResponseBody = function(endpoint, env) {
var slResponses = endpoint.Responses;
var mimeType = (endpoint.Produces && endpoint.Produces.length) ?
endpoint.Produces[0] : null;

var result = {};
for(var i in slResponses) {
var res = slResponses[i];
var mimeType = (endpoint.Produces && endpoint.Produces.length) ? endpoint.Produces[0]:null;
// if (!mimeType && env.Produces && env.Produces.length) {
// mimeType = env.Produces[0];
// }
var item = mapResponseBody(res, mimeType);
result[(res.codes && res.codes.length > 0 && parseInt(res.codes[0]) ? res.codes[0] : 'default')] = item;
if (!mimeType && env.Produces && env.Produces.length) {
mimeType = env.Produces[0];
}

return result;
return endpoint.Responses.reduce(function(result, response) {
var item = mapResponseBody(response, mimeType);

result[(response.codes && response.codes.length > 0 &&
parseInt(response.codes[0]) ? response.codes[0] : 'default')] = item;

return result;
}, {});
};

Swagger.prototype._mapRequestBody = function(slRequestBody, requestTypes) {
Expand Down
24 changes: 24 additions & 0 deletions test/lib/exporters/swagger.js
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,30 @@ describe('Swagger Exporter', function(){
var res = swaggerExporter._mapResponseBody(endpoint);
expect(res).to.have.keys('200', '404');
expect(res).to.have.deep.property('404.schema.$ref', '#/definitions/global:ErrorResponse');
expect(res['404'].examples).to.be.deep.equal({
'application/json': JSON.parse(endpoint.Responses[1].example)
});
});

it('should inherit produces from environment', function() {
var endpoint = new Endpoint('test');
var environment = new Environment();

environment.Produces = ['application/json'];

endpoint.Produces = [];
endpoint.Responses = [
{
codes: ['200'],
example: '{"id": 17, "name": "new item"}',
description: ''
}
];
var res = swaggerExporter._mapResponseBody(endpoint, environment);
expect(res).to.have.keys('200');
expect(res['200'].examples).to.be.deep.equal({
'application/json': JSON.parse(endpoint.Responses[0].example)
});
});
});

Expand Down

0 comments on commit b03ac3c

Please sign in to comment.