Skip to content

Commit

Permalink
fix: do not discard foreign values
Browse files Browse the repository at this point in the history
fix: Do not discard foreign values
  • Loading branch information
marbemac authored Sep 3, 2019
2 parents 33f82eb + 75f5d7a commit f4a7389
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 20 deletions.
9 changes: 2 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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.1.1",
Expand Down Expand Up @@ -60,7 +55,7 @@
"typescript": "3.6.2"
},
"lint-staged": {
"*.{ts,tsx}$": [
"src/**/*.ts": [
"yarn lint.fix",
"git add"
]
Expand Down
3 changes: 0 additions & 3 deletions rollup.config.js

This file was deleted.

26 changes: 26 additions & 0 deletions src/oas2/transformers/__tests__/responses.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ describe('responses', () => {
beforeEach(() => {
(translateToHeaderParams as jest.Mock).mockReturnValue(fakeHeaderParams);
});

test('should translate to multiple responses', () => {
const responses = translateToResponses(
{
Expand Down Expand Up @@ -68,4 +69,29 @@ describe('responses', () => {
),
).toMatchSnapshot();
});

describe('should keep foreign examples', () => {
it('aggregating them 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]).toHaveProperty('mediaType', 'application/json');

expect(responses[0].contents![0].examples).toBeDefined();
expect(responses[0].contents![0].examples).toHaveLength(2);
});
});
});
38 changes: 28 additions & 10 deletions src/oas2/transformers/responses.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,43 @@
import { IHttpOperationResponse } from '@stoplight/types';
import { JSONSchema4 } from 'json-schema';
import { map, partial } from 'lodash';
import { chain, map, partial } from 'lodash';
import { Response } from 'swagger-schema-official';

import { translateToHeaderParams } from './params';

const toObject = <T>(value: T, key: string) => ({ key, value });

function translateToResponse(produces: string[], response: Response, statusCode: string): IHttpOperationResponse {
const headers = translateToHeaderParams(response.headers || {});
return {
const objectifiedExamples = chain(response.examples)
.mapValues((value, key) => ({ key, value }))
.values()
.value();

const contents = produces.map(produceElement => ({
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,
};

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;
}

export function translateToResponses(
Expand Down

0 comments on commit f4a7389

Please sign in to comment.