From a3068093df394531fe5921dfff10d471327a3b63 Mon Sep 17 00:00:00 2001 From: Vincenzo Chianese Date: Mon, 22 Jul 2019 12:15:19 +0200 Subject: [PATCH 01/12] chore: update lodash --- package.json | 2 +- yarn.lock | 43 ++++++++----------------------------------- 2 files changed, 9 insertions(+), 36 deletions(-) diff --git a/package.json b/package.json index 12e2d4cf..f08c28df 100644 --- a/package.json +++ b/package.json @@ -38,7 +38,7 @@ "@stoplight/types": "^9.1.2", "@types/swagger-schema-official": "~2.0.18", "@types/urijs": "~1.19.1", - "lodash": "^4.17.11", + "lodash": "^4.17.15", "openapi3-ts": "~1.3.0", "urijs": "~1.19.1" }, diff --git a/yarn.lock b/yarn.lock index 2d54eb4a..37d91385 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2240,7 +2240,7 @@ debug@^4.0.0, debug@^4.0.1, debug@^4.1.0, debug@^4.1.1: dependencies: ms "^2.1.1" -debuglog@*, debuglog@^1.0.1: +debuglog@^1.0.1: version "1.0.1" resolved "https://registry.yarnpkg.com/debuglog/-/debuglog-1.0.1.tgz#aa24ffb9ac3df9a2351837cfb2d279360cd78492" integrity sha1-qiT/uaw9+aI1GDfPstJ5NgzXhJI= @@ -3508,7 +3508,7 @@ import-local@^2.0.0: pkg-dir "^3.0.0" resolve-cwd "^2.0.0" -imurmurhash@*, imurmurhash@^0.1.4: +imurmurhash@^0.1.4: version "0.1.4" resolved "https://registry.yarnpkg.com/imurmurhash/-/imurmurhash-0.1.4.tgz#9218b9b2b928a238b13dc4fb6b6d576f231453ea" integrity sha1-khi5srkoojixPcT7a21XbyMUU+o= @@ -4878,11 +4878,6 @@ lockfile@^1.0.4: dependencies: signal-exit "^3.0.2" -lodash._baseindexof@*: - version "3.1.0" - resolved "https://registry.yarnpkg.com/lodash._baseindexof/-/lodash._baseindexof-3.1.0.tgz#fe52b53a1c6761e42618d654e4a25789ed61822c" - integrity sha1-/lK1OhxnYeQmGNZU5KJXie1hgiw= - lodash._baseuniq@~4.6.0: version "4.6.0" resolved "https://registry.yarnpkg.com/lodash._baseuniq/-/lodash._baseuniq-4.6.0.tgz#0ebb44e456814af7905c6212fa2c9b2d51b841e8" @@ -4891,33 +4886,11 @@ lodash._baseuniq@~4.6.0: lodash._createset "~4.0.0" lodash._root "~3.0.0" -lodash._bindcallback@*: - version "3.0.1" - resolved "https://registry.yarnpkg.com/lodash._bindcallback/-/lodash._bindcallback-3.0.1.tgz#e531c27644cf8b57a99e17ed95b35c748789392e" - integrity sha1-5THCdkTPi1epnhftlbNcdIeJOS4= - -lodash._cacheindexof@*: - version "3.0.2" - resolved "https://registry.yarnpkg.com/lodash._cacheindexof/-/lodash._cacheindexof-3.0.2.tgz#3dc69ac82498d2ee5e3ce56091bafd2adc7bde92" - integrity sha1-PcaayCSY0u5ePOVgkbr9Ktx73pI= - -lodash._createcache@*: - version "3.1.2" - resolved "https://registry.yarnpkg.com/lodash._createcache/-/lodash._createcache-3.1.2.tgz#56d6a064017625e79ebca6b8018e17440bdcf093" - integrity sha1-VtagZAF2JeeevKa4AY4XRAvc8JM= - dependencies: - lodash._getnative "^3.0.0" - lodash._createset@~4.0.0: version "4.0.3" resolved "https://registry.yarnpkg.com/lodash._createset/-/lodash._createset-4.0.3.tgz#0f4659fbb09d75194fa9e2b88a6644d363c9fe26" integrity sha1-D0ZZ+7CddRlPqeK4imZE02PJ/iY= -lodash._getnative@*, lodash._getnative@^3.0.0: - version "3.9.1" - resolved "https://registry.yarnpkg.com/lodash._getnative/-/lodash._getnative-3.9.1.tgz#570bc7dede46d61cdcde687d65d3eecbaa3aaff5" - integrity sha1-VwvH3t5G1hzc3mh9ZdPuy6o6r/U= - lodash._reinterpolate@^3.0.0: version "3.0.0" resolved "https://registry.yarnpkg.com/lodash._reinterpolate/-/lodash._reinterpolate-3.0.0.tgz#0ccf2d89166af03b3663c796538b75ac6e114d9d" @@ -4963,11 +4936,6 @@ lodash.map@^4.5.1: resolved "https://registry.yarnpkg.com/lodash.map/-/lodash.map-4.6.0.tgz#771ec7839e3473d9c4cde28b19394c3562f4f6d3" integrity sha1-dx7Hg540c9nEzeKLGTlMNWL09tM= -lodash.restparam@*: - version "3.6.1" - resolved "https://registry.yarnpkg.com/lodash.restparam/-/lodash.restparam-3.6.1.tgz#936a4e309ef330a7645ed4145986c85ae5b20805" - integrity sha1-k2pOMJ7zMKdkXtQUWYbIWuWyCAU= - lodash.set@^4.3.2: version "4.3.2" resolved "https://registry.yarnpkg.com/lodash.set/-/lodash.set-4.3.2.tgz#d8757b1da807dde24816b0d6a84bea1a76230b23" @@ -5028,6 +4996,11 @@ lodash@^4.17, lodash@^4.17.10, lodash@^4.17.11, lodash@^4.17.4, lodash@^4.2.1: resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.14.tgz#9ce487ae66c96254fe20b599f21b6816028078ba" integrity sha512-mmKYbW3GLuJeX+iGP+Y7Gp1AiGHGbXHCOh/jZmrawMmsE7MS4znI3RL2FsjbqOyMayHInjOeykW7PEajUk1/xw== +lodash@^4.17.15: + version "4.17.15" + resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.15.tgz#b447f6670a0455bbfeedd11392eff330ea097548" + integrity sha512-8xOcRHvCjnocdS5cpwXQXVzmmh5e5+saE2QGoeQmbKmRS6J3VQppPOIt0MnmE+4xlZoumy0GPG0D0MVIQbNA1A== + log-symbols@^1.0.2: version "1.0.2" resolved "https://registry.yarnpkg.com/log-symbols/-/log-symbols-1.0.2.tgz#376ff7b58ea3086a0f09facc74617eca501e1a18" @@ -6921,7 +6894,7 @@ readable-stream@~1.1.10: isarray "0.0.1" string_decoder "~0.10.x" -readdir-scoped-modules@*, readdir-scoped-modules@^1.0.0, readdir-scoped-modules@^1.1.0: +readdir-scoped-modules@^1.0.0, readdir-scoped-modules@^1.1.0: version "1.1.0" resolved "https://registry.yarnpkg.com/readdir-scoped-modules/-/readdir-scoped-modules-1.1.0.tgz#8d45407b4f870a0dcaebc0e28670d18e74514309" integrity sha512-asaikDeqAQg7JifRsZn1NJZXo9E+VwlyCfbkZhwyISinqk5zNS6266HS5kah6P0SaQKGF6SkNnZVHUzHFYxYDw== From e97992d1aebd028dd5ac5de1ea85060a94f0d7ca Mon Sep 17 00:00:00 2001 From: Vincenzo Chianese Date: Mon, 22 Jul 2019 14:48:55 +0200 Subject: [PATCH 02/12] feat: do not discard unknown examples --- .../__snapshots__/responses.test.ts.snap | 22 +-------- .../transformers/__tests__/responses.test.ts | 47 +++++++++++++++++++ src/oas2/transformers/responses.ts | 38 +++++++++++---- 3 files changed, 78 insertions(+), 29 deletions(-) diff --git a/src/oas2/transformers/__tests__/__snapshots__/responses.test.ts.snap b/src/oas2/transformers/__tests__/__snapshots__/responses.test.ts.snap index 5ea1bc53..50469339 100644 --- a/src/oas2/transformers/__tests__/__snapshots__/responses.test.ts.snap +++ b/src/oas2/transformers/__tests__/__snapshots__/responses.test.ts.snap @@ -15,11 +15,6 @@ Array [ "mediaType": "application/json", "schema": Object {}, }, - Object { - "examples": Array [], - "mediaType": "application/xml", - "schema": Object {}, - }, ], "description": "d1", "headers": Array [ @@ -32,11 +27,6 @@ Array [ Object { "code": "r2", "contents": Array [ - Object { - "examples": Array [], - "mediaType": "application/json", - "schema": Object {}, - }, Object { "examples": Array [ Object { @@ -66,12 +56,7 @@ Array [ "contents": Array [ Object { "examples": Array [], - "mediaType": "application/json", - "schema": Object {}, - }, - Object { - "examples": Array [], - "mediaType": "application/xml", + "mediaType": "", "schema": Object {}, }, ], @@ -91,11 +76,6 @@ Array [ Object { "code": "r1", "contents": Array [ - Object { - "examples": Array [], - "mediaType": "application/json", - "schema": Object {}, - }, Object { "examples": Array [ Object { diff --git a/src/oas2/transformers/__tests__/responses.test.ts b/src/oas2/transformers/__tests__/responses.test.ts index dbbcdea0..62e07f48 100644 --- a/src/oas2/transformers/__tests__/responses.test.ts +++ b/src/oas2/transformers/__tests__/responses.test.ts @@ -12,6 +12,7 @@ describe('responses', () => { beforeEach(() => { (translateToHeaderParams as jest.Mock).mockReturnValue(fakeHeaderParams); }); + test('should translate to multiple responses', () => { const responses = translateToResponses( { @@ -68,4 +69,50 @@ describe('responses', () => { ), ).toMatchSnapshot(); }); + + describe('should keep foreign examples', () => { + + it('by creating a new content if necessary', () => { + + const responses = translateToResponses( + { + r1: { + description: 'd1', + examples: { + 'application/i-have-no-clue': {}, + }, + headers: {}, + schema: {}, + }, + }, + produces, + ); + + expect(responses[0].contents).toBeDefined(); + expect(responses[0].contents![0].examples).toBeDefined(); + expect(responses[0].contents![0].examples![0]).toBeDefined(); + expect(responses[0].contents![0].examples![0]).toHaveProperty('key', 'application/i-have-no-clue') + }) + it('aggregating to the first example', () => { + const responses = translateToResponses( + { + r1: { + description: 'd1', + examples: { + 'application/i-have-no-clue': {}, + 'application/json': {} + }, + headers: {}, + schema: {}, + }, + }, + produces, + ); + + expect(responses[0].contents).toBeDefined(); + expect(responses[0].contents![0].examples).toBeDefined(); + expect(responses[0].contents![0].examples![0]).toBeDefined(); + expect(responses[0].contents![0].examples![0]).toHaveProperty('key', 'application/json') + }) + }); }); diff --git a/src/oas2/transformers/responses.ts b/src/oas2/transformers/responses.ts index 39690da6..f81a9b3d 100644 --- a/src/oas2/transformers/responses.ts +++ b/src/oas2/transformers/responses.ts @@ -1,4 +1,4 @@ -import { IHttpOperationResponse } from '@stoplight/types'; +import { IHttpOperationResponse, IMediaTypeContent } from '@stoplight/types'; import { JSONSchema4 } from 'json-schema'; import { map, partial } from 'lodash'; import { Response } from 'swagger-schema-official'; @@ -9,17 +9,39 @@ const toObject = (value: T, key: string) => ({ key, value }); function translateToResponse(produces: string[], response: Response, statusCode: string): IHttpOperationResponse { const headers = translateToHeaderParams(response.headers || {}); - return { + const objectifiedExamples = map(response.examples, toObject); + + const contents: IMediaTypeContent[] = [] + + produces.forEach(produceElement => { + if (response.examples && response.examples[produceElement]) { + contents.push({ + mediaType: produceElement, + schema: response.schema as JSONSchema4, + examples: objectifiedExamples.filter(example => example.key === produceElement), + }) + } + }) + + const translatedResponses = { code: statusCode, description: response.description, headers, - contents: produces.map(mediaType => ({ - mediaType, - schema: response.schema as JSONSchema4, - examples: map(response.examples, toObject).filter(example => example.key === mediaType), - })), - // `links` not supported by oas2 + contents }; + + if (translatedResponses.contents.length === 0) + translatedResponses.contents[0] = { + mediaType: '', + schema: {}, + examples: [] + }; + + translatedResponses.contents[0].examples!.push( + ...objectifiedExamples.filter(example => !produces.includes(example.key)), + ); + + return translatedResponses; } export function translateToResponses( From 213eb55d0dbb2ecb3c4a555c8db627a0a1bc91c2 Mon Sep 17 00:00:00 2001 From: Vincenzo Chianese Date: Mon, 22 Jul 2019 14:52:03 +0200 Subject: [PATCH 03/12] chore: delete stuff that is not that cool --- package.json | 7 +------ src/oas2/transformers/__tests__/responses.test.ts | 12 +++++------- src/oas2/transformers/responses.ts | 10 +++++----- 3 files changed, 11 insertions(+), 18 deletions(-) diff --git a/package.json b/package.json index f08c28df..80e5a6cb 100644 --- a/package.json +++ b/package.json @@ -21,17 +21,12 @@ }, "scripts": { "build": "sl-scripts build", - "commit": "git-cz", "lint": "tslint -c tslint.json 'src/**/*.ts'", "lint.fix": "yarn lint --fix", "release": "sl-scripts release", "release.dryRun": "sl-scripts release --dry-run --debug", "test": "jest", - "test.prod": "yarn lint && yarn test --coverage --maxWorkers=2", - "test.update": "yarn test --updateSnapshot", - "test.watch": "yarn test --watch", - "yalc": "yarn build && (cd dist && yalc push)", - "yalc.watch": "nodemon --watch 'src/**/*' --exec 'yarn yalc'" + "test.prod": "yarn lint && yarn test --coverage --maxWorkers=2" }, "dependencies": { "@stoplight/json": "^3.0.1", diff --git a/src/oas2/transformers/__tests__/responses.test.ts b/src/oas2/transformers/__tests__/responses.test.ts index 62e07f48..f81d29c5 100644 --- a/src/oas2/transformers/__tests__/responses.test.ts +++ b/src/oas2/transformers/__tests__/responses.test.ts @@ -71,9 +71,7 @@ describe('responses', () => { }); describe('should keep foreign examples', () => { - it('by creating a new content if necessary', () => { - const responses = translateToResponses( { r1: { @@ -91,8 +89,8 @@ describe('responses', () => { expect(responses[0].contents).toBeDefined(); expect(responses[0].contents![0].examples).toBeDefined(); expect(responses[0].contents![0].examples![0]).toBeDefined(); - expect(responses[0].contents![0].examples![0]).toHaveProperty('key', 'application/i-have-no-clue') - }) + expect(responses[0].contents![0].examples![0]).toHaveProperty('key', 'application/i-have-no-clue'); + }); it('aggregating to the first example', () => { const responses = translateToResponses( { @@ -100,7 +98,7 @@ describe('responses', () => { description: 'd1', examples: { 'application/i-have-no-clue': {}, - 'application/json': {} + 'application/json': {}, }, headers: {}, schema: {}, @@ -112,7 +110,7 @@ describe('responses', () => { expect(responses[0].contents).toBeDefined(); expect(responses[0].contents![0].examples).toBeDefined(); expect(responses[0].contents![0].examples![0]).toBeDefined(); - expect(responses[0].contents![0].examples![0]).toHaveProperty('key', 'application/json') - }) + expect(responses[0].contents![0].examples![0]).toHaveProperty('key', 'application/json'); + }); }); }); diff --git a/src/oas2/transformers/responses.ts b/src/oas2/transformers/responses.ts index f81a9b3d..3238d5af 100644 --- a/src/oas2/transformers/responses.ts +++ b/src/oas2/transformers/responses.ts @@ -11,7 +11,7 @@ function translateToResponse(produces: string[], response: Response, statusCode: const headers = translateToHeaderParams(response.headers || {}); const objectifiedExamples = map(response.examples, toObject); - const contents: IMediaTypeContent[] = [] + const contents: IMediaTypeContent[] = []; produces.forEach(produceElement => { if (response.examples && response.examples[produceElement]) { @@ -19,22 +19,22 @@ function translateToResponse(produces: string[], response: Response, statusCode: mediaType: produceElement, schema: response.schema as JSONSchema4, examples: objectifiedExamples.filter(example => example.key === produceElement), - }) + }); } - }) + }); const translatedResponses = { code: statusCode, description: response.description, headers, - contents + contents, }; if (translatedResponses.contents.length === 0) translatedResponses.contents[0] = { mediaType: '', schema: {}, - examples: [] + examples: [], }; translatedResponses.contents[0].examples!.push( From 54f9625b647f87a4146d35616e8541c79b256739 Mon Sep 17 00:00:00 2001 From: Vincenzo Chianese Date: Mon, 22 Jul 2019 14:53:15 +0200 Subject: [PATCH 04/12] chore: lint the things correctly --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 80e5a6cb..353bc846 100644 --- a/package.json +++ b/package.json @@ -55,7 +55,7 @@ }, "lint-staged": { "*.{ts,tsx}$": [ - "yarn lint.fix", + "tslint -p tsconfig.build.json", "git add" ] }, From bf6711949e856383289f864c28a9005e10b0615f Mon Sep 17 00:00:00 2001 From: Vincenzo Chianese Date: Mon, 22 Jul 2019 14:55:35 +0200 Subject: [PATCH 05/12] fix: rollup file is not that needed --- rollup.config.js | 3 --- 1 file changed, 3 deletions(-) delete mode 100644 rollup.config.js diff --git a/rollup.config.js b/rollup.config.js deleted file mode 100644 index 84ce158f..00000000 --- a/rollup.config.js +++ /dev/null @@ -1,3 +0,0 @@ -import config from '@stoplight/scripts/rollup.config'; - -export default config; // you can customize your Rollup configuration here From 0f0ba60d4af0eb139c678f3b65bd1542b653b9df Mon Sep 17 00:00:00 2001 From: Vincenzo Chianese Date: Mon, 22 Jul 2019 15:09:51 +0200 Subject: [PATCH 06/12] test: rewrite --- src/oas2/transformers/__tests__/responses.test.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/oas2/transformers/__tests__/responses.test.ts b/src/oas2/transformers/__tests__/responses.test.ts index f81d29c5..ecf66895 100644 --- a/src/oas2/transformers/__tests__/responses.test.ts +++ b/src/oas2/transformers/__tests__/responses.test.ts @@ -88,8 +88,9 @@ describe('responses', () => { expect(responses[0].contents).toBeDefined(); expect(responses[0].contents![0].examples).toBeDefined(); + expect(responses[0].contents![0]).toHaveProperty('mediaType', ''); + expect(responses[0].contents![0].examples![0]).toBeDefined(); - expect(responses[0].contents![0].examples![0]).toHaveProperty('key', 'application/i-have-no-clue'); }); it('aggregating to the first example', () => { const responses = translateToResponses( @@ -109,8 +110,9 @@ describe('responses', () => { expect(responses[0].contents).toBeDefined(); expect(responses[0].contents![0].examples).toBeDefined(); - expect(responses[0].contents![0].examples![0]).toBeDefined(); - expect(responses[0].contents![0].examples![0]).toHaveProperty('key', 'application/json'); + expect(responses[0].contents![0].examples).toHaveLength(2); + + expect(responses[0].contents![0]).toHaveProperty('mediaType', 'application/json'); }); }); }); From f6e3707cca4a5789ece31b77270ff4b1a8118989 Mon Sep 17 00:00:00 2001 From: Vincenzo Chianese Date: Mon, 22 Jul 2019 15:30:20 +0200 Subject: [PATCH 07/12] refactor: leverage compact to avoid a forEach --- src/oas2/transformers/responses.ts | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/oas2/transformers/responses.ts b/src/oas2/transformers/responses.ts index 3238d5af..f588579e 100644 --- a/src/oas2/transformers/responses.ts +++ b/src/oas2/transformers/responses.ts @@ -1,6 +1,6 @@ -import { IHttpOperationResponse, IMediaTypeContent } from '@stoplight/types'; +import { IHttpOperationResponse } from '@stoplight/types'; import { JSONSchema4 } from 'json-schema'; -import { map, partial } from 'lodash'; +import { map, partial, compact } from 'lodash'; import { Response } from 'swagger-schema-official'; import { translateToHeaderParams } from './params'; @@ -11,17 +11,16 @@ function translateToResponse(produces: string[], response: Response, statusCode: const headers = translateToHeaderParams(response.headers || {}); const objectifiedExamples = map(response.examples, toObject); - const contents: IMediaTypeContent[] = []; - - produces.forEach(produceElement => { + const contents = compact(produces.map(produceElement => { if (response.examples && response.examples[produceElement]) { - contents.push({ + return { mediaType: produceElement, schema: response.schema as JSONSchema4, examples: objectifiedExamples.filter(example => example.key === produceElement), - }); + }; } - }); + return undefined + })); const translatedResponses = { code: statusCode, From 0bf704ee06a3a71f8875af2221afe28dc0f8dcf4 Mon Sep 17 00:00:00 2001 From: Vincenzo Chianese Date: Mon, 22 Jul 2019 15:35:31 +0200 Subject: [PATCH 08/12] refactor: leverage compact to avoid a forEach --- src/oas2/transformers/responses.ts | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/src/oas2/transformers/responses.ts b/src/oas2/transformers/responses.ts index f588579e..f9a69369 100644 --- a/src/oas2/transformers/responses.ts +++ b/src/oas2/transformers/responses.ts @@ -1,6 +1,6 @@ import { IHttpOperationResponse } from '@stoplight/types'; import { JSONSchema4 } from 'json-schema'; -import { map, partial, compact } from 'lodash'; +import { compact, map, partial } from 'lodash'; import { Response } from 'swagger-schema-official'; import { translateToHeaderParams } from './params'; @@ -11,16 +11,18 @@ function translateToResponse(produces: string[], response: Response, statusCode: const headers = translateToHeaderParams(response.headers || {}); const objectifiedExamples = map(response.examples, toObject); - const contents = compact(produces.map(produceElement => { - if (response.examples && response.examples[produceElement]) { - return { - mediaType: produceElement, - schema: response.schema as JSONSchema4, - examples: objectifiedExamples.filter(example => example.key === produceElement), - }; - } - return undefined - })); + const contents = compact( + produces.map(produceElement => { + if (response.examples && response.examples[produceElement]) { + return { + mediaType: produceElement, + schema: response.schema as JSONSchema4, + examples: objectifiedExamples.filter(example => example.key === produceElement), + }; + } + return undefined; + }), + ); const translatedResponses = { code: statusCode, From 4fa95a9b3f4e033fa925b4cc5777c3214f2a3d08 Mon Sep 17 00:00:00 2001 From: Vincenzo Chianese Date: Mon, 22 Jul 2019 15:45:06 +0200 Subject: [PATCH 09/12] refactor: use more lodash --- src/oas2/transformers/responses.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/oas2/transformers/responses.ts b/src/oas2/transformers/responses.ts index f9a69369..4f1a1768 100644 --- a/src/oas2/transformers/responses.ts +++ b/src/oas2/transformers/responses.ts @@ -1,15 +1,16 @@ import { IHttpOperationResponse } from '@stoplight/types'; import { JSONSchema4 } from 'json-schema'; -import { compact, map, partial } from 'lodash'; +import { chain, compact, map, partial } from 'lodash'; import { Response } from 'swagger-schema-official'; import { translateToHeaderParams } from './params'; -const toObject = (value: T, key: string) => ({ key, value }); - function translateToResponse(produces: string[], response: Response, statusCode: string): IHttpOperationResponse { const headers = translateToHeaderParams(response.headers || {}); - const objectifiedExamples = map(response.examples, toObject); + const objectifiedExamples = chain(response.examples) + .mapValues((value, key) => ({ key, value })) + .values() + .value(); const contents = compact( produces.map(produceElement => { From 5c413216aed9a3b9e5ee824ad7647cbf478295a2 Mon Sep 17 00:00:00 2001 From: Vincenzo Chianese Date: Mon, 22 Jul 2019 16:15:41 +0200 Subject: [PATCH 10/12] refactor: do not creat empty examples --- package.json | 4 ++-- .../__snapshots__/responses.test.ts.snap | 8 +------ src/oas2/transformers/responses.ts | 21 ++++++++++--------- 3 files changed, 14 insertions(+), 19 deletions(-) diff --git a/package.json b/package.json index 353bc846..ebeb50bd 100644 --- a/package.json +++ b/package.json @@ -54,8 +54,8 @@ "typescript": "3.5.3" }, "lint-staged": { - "*.{ts,tsx}$": [ - "tslint -p tsconfig.build.json", + "src/**/*.ts": [ + "yarn lint.fix", "git add" ] }, diff --git a/src/oas2/transformers/__tests__/__snapshots__/responses.test.ts.snap b/src/oas2/transformers/__tests__/__snapshots__/responses.test.ts.snap index 50469339..2ceb96d4 100644 --- a/src/oas2/transformers/__tests__/__snapshots__/responses.test.ts.snap +++ b/src/oas2/transformers/__tests__/__snapshots__/responses.test.ts.snap @@ -53,13 +53,7 @@ exports[`responses should translate to response w/o examples 1`] = ` Array [ Object { "code": "r1", - "contents": Array [ - Object { - "examples": Array [], - "mediaType": "", - "schema": Object {}, - }, - ], + "contents": Array [], "description": "d1", "headers": Array [ Object { diff --git a/src/oas2/transformers/responses.ts b/src/oas2/transformers/responses.ts index 4f1a1768..735dcc35 100644 --- a/src/oas2/transformers/responses.ts +++ b/src/oas2/transformers/responses.ts @@ -32,16 +32,17 @@ function translateToResponse(produces: string[], response: Response, statusCode: contents, }; - if (translatedResponses.contents.length === 0) - translatedResponses.contents[0] = { - mediaType: '', - schema: {}, - examples: [], - }; - - translatedResponses.contents[0].examples!.push( - ...objectifiedExamples.filter(example => !produces.includes(example.key)), - ); + const foreignExamples = objectifiedExamples.filter(example => !produces.includes(example.key)); + if (foreignExamples.length > 0) { + if (translatedResponses.contents.length === 0) + translatedResponses.contents[0] = { + mediaType: '', + schema: {}, + examples: [], + }; + + translatedResponses.contents[0].examples!.push(...foreignExamples); + } return translatedResponses; } From 98f8f7975deb11f8764dfa12d9b71484ddb94d44 Mon Sep 17 00:00:00 2001 From: Vincenzo Chianese Date: Wed, 24 Jul 2019 12:59:25 +0200 Subject: [PATCH 11/12] fix: keep empty contents --- .../__snapshots__/responses.test.ts.snap | 28 ++++++++++++++++++- .../transformers/__tests__/responses.test.ts | 23 +-------------- src/oas2/transformers/responses.ts | 19 ++++--------- 3 files changed, 34 insertions(+), 36 deletions(-) diff --git a/src/oas2/transformers/__tests__/__snapshots__/responses.test.ts.snap b/src/oas2/transformers/__tests__/__snapshots__/responses.test.ts.snap index 2ceb96d4..5ea1bc53 100644 --- a/src/oas2/transformers/__tests__/__snapshots__/responses.test.ts.snap +++ b/src/oas2/transformers/__tests__/__snapshots__/responses.test.ts.snap @@ -15,6 +15,11 @@ Array [ "mediaType": "application/json", "schema": Object {}, }, + Object { + "examples": Array [], + "mediaType": "application/xml", + "schema": Object {}, + }, ], "description": "d1", "headers": Array [ @@ -27,6 +32,11 @@ Array [ Object { "code": "r2", "contents": Array [ + Object { + "examples": Array [], + "mediaType": "application/json", + "schema": Object {}, + }, Object { "examples": Array [ Object { @@ -53,7 +63,18 @@ exports[`responses should translate to response w/o examples 1`] = ` Array [ Object { "code": "r1", - "contents": Array [], + "contents": Array [ + Object { + "examples": Array [], + "mediaType": "application/json", + "schema": Object {}, + }, + Object { + "examples": Array [], + "mediaType": "application/xml", + "schema": Object {}, + }, + ], "description": "d1", "headers": Array [ Object { @@ -70,6 +91,11 @@ Array [ Object { "code": "r1", "contents": Array [ + Object { + "examples": Array [], + "mediaType": "application/json", + "schema": Object {}, + }, Object { "examples": Array [ Object { diff --git a/src/oas2/transformers/__tests__/responses.test.ts b/src/oas2/transformers/__tests__/responses.test.ts index ecf66895..d7faa521 100644 --- a/src/oas2/transformers/__tests__/responses.test.ts +++ b/src/oas2/transformers/__tests__/responses.test.ts @@ -71,28 +71,7 @@ describe('responses', () => { }); describe('should keep foreign examples', () => { - it('by creating a new content if necessary', () => { - const responses = translateToResponses( - { - r1: { - description: 'd1', - examples: { - 'application/i-have-no-clue': {}, - }, - headers: {}, - schema: {}, - }, - }, - produces, - ); - - expect(responses[0].contents).toBeDefined(); - expect(responses[0].contents![0].examples).toBeDefined(); - expect(responses[0].contents![0]).toHaveProperty('mediaType', ''); - - expect(responses[0].contents![0].examples![0]).toBeDefined(); - }); - it('aggregating to the first example', () => { + it('aggregating them to the first example', () => { const responses = translateToResponses( { r1: { diff --git a/src/oas2/transformers/responses.ts b/src/oas2/transformers/responses.ts index 735dcc35..14240ba8 100644 --- a/src/oas2/transformers/responses.ts +++ b/src/oas2/transformers/responses.ts @@ -1,6 +1,6 @@ import { IHttpOperationResponse } from '@stoplight/types'; import { JSONSchema4 } from 'json-schema'; -import { chain, compact, map, partial } from 'lodash'; +import { chain, map, partial } from 'lodash'; import { Response } from 'swagger-schema-official'; import { translateToHeaderParams } from './params'; @@ -12,18 +12,11 @@ function translateToResponse(produces: string[], response: Response, statusCode: .values() .value(); - const contents = compact( - produces.map(produceElement => { - if (response.examples && response.examples[produceElement]) { - return { - mediaType: produceElement, - schema: response.schema as JSONSchema4, - examples: objectifiedExamples.filter(example => example.key === produceElement), - }; - } - return undefined; - }), - ); + const contents = produces.map(produceElement => ({ + mediaType: produceElement, + schema: response.schema as JSONSchema4, + examples: objectifiedExamples.filter(example => example.key === produceElement), + })); const translatedResponses = { code: statusCode, From 012ccbf87dc40df9c96a824bb2794dafd66172d2 Mon Sep 17 00:00:00 2001 From: Vincenzo Chianese Date: Wed, 24 Jul 2019 13:04:34 +0200 Subject: [PATCH 12/12] test: reorder --- src/oas2/transformers/__tests__/responses.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/oas2/transformers/__tests__/responses.test.ts b/src/oas2/transformers/__tests__/responses.test.ts index d7faa521..4e729ce2 100644 --- a/src/oas2/transformers/__tests__/responses.test.ts +++ b/src/oas2/transformers/__tests__/responses.test.ts @@ -88,10 +88,10 @@ describe('responses', () => { ); expect(responses[0].contents).toBeDefined(); + expect(responses[0].contents![0]).toHaveProperty('mediaType', 'application/json'); + expect(responses[0].contents![0].examples).toBeDefined(); expect(responses[0].contents![0].examples).toHaveLength(2); - - expect(responses[0].contents![0]).toHaveProperty('mediaType', 'application/json'); }); }); });