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

Commit

Permalink
Merge pull request #143 from apiaryio/kylef/produces-json
Browse files Browse the repository at this point in the history
Don't build request/response example pairs for all produces types
  • Loading branch information
kylef authored Nov 6, 2017
2 parents 6940b8d + c86d5bc commit 06ea645
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 10 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# Master
# 0.15.0

## Enhancements

- Sample values are now generated for data structures of object and array types.
- Request/Response pairs are now generated from explicit examples, or the first
JSON produces content-type.

# 0.14.3

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fury-adapter-swagger",
"version": "0.14.3",
"version": "0.15.0",
"description": "Swagger 2.0 parser for Fury.js",
"main": "./lib/adapter.js",
"tonicExampleFilename": "tonic-example.js",
Expand Down Expand Up @@ -37,7 +37,7 @@
"minim": "^0.19.0",
"minim-parse-result": "^0.8.0",
"peasant": "1.1.0",
"swagger-zoo": "2.8.2"
"swagger-zoo": "2.9.0"
},
"engines": {
"node": ">=4"
Expand Down
18 changes: 11 additions & 7 deletions src/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -815,16 +815,20 @@ export default class Parser {
}

// Returns all of the content types for a response
// Response content types include all produces types including any examples
// Response content types include all example types OR the first JSON content type
// Returns `[null]` when there are no content types
gatherResponseContentTypes(methodValue, examples) {
let contentTypes = (methodValue.produces || this.swagger.produces || []);
let contentTypes = [];

// Add any missing produces listed in examples
if (examples) {
const exampleContentTypes = Object.keys(examples)
.filter(example => !contentTypes.includes(example));
contentTypes = contentTypes.concat(exampleContentTypes);
if (examples && Object.keys(examples).length > 0) {
contentTypes = Object.keys(examples);
} else {
const produces = (methodValue.produces || this.swagger.produces || []);
const jsonContentTypes = produces.filter(isJsonContentType);

if (jsonContentTypes.length > 0) {
contentTypes = [jsonContentTypes[0]];
}
}

contentTypes = contentTypes.filter(isValidContentType);
Expand Down
78 changes: 78 additions & 0 deletions test/parser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { expect } from 'chai';
import fury from 'fury';
import Parser from '../src/parser';

describe('Parser', () => {
let parser;

before(() => {
parser = new Parser({ minim: fury.minim });
parser.swagger = {
consumes: [],
produces: [],
};
});

context('content types', () => {
it('gathers null response type when no examples or produces', () => {
const methodValue = {};
const examples = {};
const contentTypes = parser.gatherResponseContentTypes(methodValue, examples);

expect(contentTypes).to.deep.equal([null]);
});

it('gathers all example response content types', () => {
const methodValue = {};
const examples = {
'application/json': '',
'application/hal+json': '',
'application/xml': '',
};
const contentTypes = parser.gatherResponseContentTypes(methodValue, examples);

expect(contentTypes).to.deep.equal([
'application/json',
'application/hal+json',
'application/xml',
]);
});

it('gathers first JSON produces without examples', () => {
const methodValue = {
produces: [
'text/plain',
'application/json',
'application/hal+json',
],
};
const examples = {};
const contentTypes = parser.gatherResponseContentTypes(methodValue, examples);

expect(contentTypes).to.deep.equal(['application/json']);
});

it('gathers only example response content types with produces', () => {
const methodValue = {
produces: [
'application/json',
'application/problem+json',
],
};
const examples = {
'application/vnd.error+json': '',
};
const contentTypes = parser.gatherResponseContentTypes(methodValue, examples);

expect(contentTypes).to.deep.equal(['application/vnd.error+json']);
});

it('rejects invalid content types when gathering response content types', () => {
const methodValue = {};
const examples = { '!!!': '' };
const contentTypes = parser.gatherResponseContentTypes(methodValue, examples);

expect(contentTypes).to.deep.equal([null]);
});
});
});

0 comments on commit 06ea645

Please sign in to comment.