Skip to content

Commit

Permalink
fix(oas2): redundant ids & schemas (#179)
Browse files Browse the repository at this point in the history
* fix(oas2): redundant ids

* refactor(oas2): no empty schemas
  • Loading branch information
P0lip authored Apr 7, 2022
1 parent e13dc26 commit d5a6c04
Show file tree
Hide file tree
Showing 8 changed files with 213 additions and 96 deletions.
Empty file removed src/oas/transformers/security.ts
Empty file.
4 changes: 0 additions & 4 deletions src/oas2/__tests__/operation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,10 +244,6 @@ describe('transformOas2Operation', () => {
{
id: expect.any(String),
name: 'name',
schema: {
'x-stoplight-id': expect.any(String),
$schema: 'http://json-schema.org/draft-07/schema#',
},
style: 'simple',
},
],
Expand Down
84 changes: 67 additions & 17 deletions src/oas2/transformers/__tests__/__snapshots__/request.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,14 @@
exports[`request given single body param should translate to request with body 1`] = `
Object {
"body": Object {
"in": "body",
"name": "param",
"contents": Array [
Object {
"examples": Array [],
"id": Any<String>,
"mediaType": "*",
},
],
"id": Any<String>,
},
"cookie": Array [],
"headers": Array [],
Expand All @@ -16,9 +22,23 @@ Object {
exports[`request given single form param should translate to request with form 1`] = `
Object {
"body": Object {
"in": "formData",
"name": "param",
"type": "number",
"contents": Array [
Object {
"id": Any<String>,
"mediaType": "*",
"schema": Object {
"$schema": "http://json-schema.org/draft-07/schema#",
"properties": Object {
"param": Object {
"type": "number",
},
},
"type": "object",
"x-stoplight-id": Any<String>,
},
},
],
"id": Any<String>,
},
"cookie": Array [],
"headers": Array [],
Expand All @@ -32,8 +52,9 @@ Object {
"cookie": Array [],
"headers": Array [
Object {
"in": "header",
"id": Any<String>,
"name": "param",
"style": "simple",
},
],
"path": Array [],
Expand All @@ -47,9 +68,10 @@ Object {
"headers": Array [],
"path": Array [
Object {
"in": "path",
"id": Any<String>,
"name": "param",
"required": true,
"style": "simple",
},
],
"query": Array [],
Expand All @@ -63,8 +85,9 @@ Object {
"path": Array [],
"query": Array [
Object {
"in": "query",
"id": Any<String>,
"name": "param",
"style": "form",
},
],
}
Expand All @@ -75,8 +98,9 @@ Object {
"cookie": Array [],
"headers": Array [
Object {
"in": "header",
"id": Any<String>,
"name": "param",
"style": "simple",
},
],
"path": Array [],
Expand All @@ -90,9 +114,10 @@ Object {
"headers": Array [],
"path": Array [
Object {
"in": "path",
"id": Any<String>,
"name": "param",
"required": true,
"style": "simple",
},
],
"query": Array [],
Expand All @@ -106,8 +131,9 @@ Object {
"path": Array [],
"query": Array [
Object {
"in": "query",
"id": Any<String>,
"name": "param",
"style": "form",
},
],
}
Expand All @@ -116,13 +142,37 @@ Object {
exports[`request should translate mixed request 1`] = `
Object {
"body": Object {
"in": "formData",
"name": "param",
"type": "number",
"contents": Array [
Object {
"examples": Array [],
"id": Any<String>,
"mediaType": "*",
},
],
"id": Any<String>,
},
"cookie": Array [],
"headers": Array [],
"path": Array [],
"query": Array [],
"headers": Array [
Object {
"id": Any<String>,
"name": "param",
"style": "simple",
},
],
"path": Array [
Object {
"id": Any<String>,
"name": "param",
"required": true,
"style": "simple",
},
],
"query": Array [
Object {
"id": Any<String>,
"name": "param",
"style": "form",
},
],
}
`;
3 changes: 0 additions & 3 deletions src/oas2/transformers/__tests__/params.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,6 @@ describe('params.translator', () => {
$schema: 'http://json-schema.org/draft-07/schema#',
properties: {
arr: {
'x-stoplight-id': expect.any(String),
description: 'desc',
items: {
type: 'number',
Expand All @@ -361,14 +360,12 @@ describe('params.translator', () => {
type: 'array',
},
int: {
'x-stoplight-id': expect.any(String),
description: 'desc',
maximum: 3,
minimum: 0,
type: 'integer',
},
str: {
'x-stoplight-id': expect.any(String),
minLength: 1,
default: '25-07-2019',
description: 'desc',
Expand Down
140 changes: 101 additions & 39 deletions src/oas2/transformers/__tests__/request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,81 +7,143 @@ import {
} from 'swagger-schema-official';

import { createContext } from '../../../oas/context';
import {
translateFromFormDataParameters,
translateToBodyParameter,
translateToHeaderParam,
translateToPathParameter,
translateToQueryParameter,
} from '../params';
import { translateToRequest as _translateToRequest } from '../request';

jest.mock('../params');

const translateToRequest = (path: Record<string, unknown>, parameters: any[]) => {
const ctx = createContext({ consumes: ['*'], paths: { '/api': { parameters } } });
return _translateToRequest.call(ctx, path, { parameters });
const translateToRequest = (parameters: any[]) => {
const document = { consumes: ['*'], paths: { '/api': { get: { parameters } } } };
const ctx = createContext(document);
return _translateToRequest.call(ctx, document.paths['/api'], document.paths['/api'].get);
};

describe('request', () => {
const fakeParameter: FormDataParameter = {
name: 'name',
type: 'string',
in: 'formData',
};
const fakeBodyParameter: BodyParameter = { in: 'body', name: 'param' };
const fakeFormParameter: FormDataParameter = { in: 'formData', name: 'param', type: 'number' };
const fakeQueryParameter: QueryParameter = { in: 'query', name: 'param' };
const fakeHeaderParameter: HeaderParameter = { in: 'header', name: 'param' };
const fakePathParameter: PathParameter = { in: 'path', name: 'param', required: true };

beforeEach(() => {
(translateToBodyParameter as jest.Mock).mockReturnValue(fakeBodyParameter);
(translateFromFormDataParameters as jest.Mock).mockReturnValue(fakeFormParameter);
(translateToQueryParameter as jest.Mock).mockReturnValue(fakeQueryParameter);
(translateToPathParameter as jest.Mock).mockReturnValue(fakePathParameter);
(translateToHeaderParam as jest.Mock).mockReturnValue(fakeHeaderParameter);
});

afterEach(() => {
jest.resetAllMocks();
});

it('given single body param should translate to request with body', () => {
expect(translateToRequest({}, [fakeBodyParameter])).toMatchSnapshot();
expect(translateToRequest([fakeBodyParameter])).toMatchSnapshot({
body: {
id: expect.any(String),
contents: [
{
id: expect.any(String),
},
],
},
});
});

it('given single form param should translate to request with form', () => {
expect(translateToRequest({}, [fakeFormParameter])).toMatchSnapshot();
expect(translateToRequest([fakeFormParameter])).toMatchSnapshot({
body: {
id: expect.any(String),
contents: [
{
id: expect.any(String),
schema: {
'x-stoplight-id': expect.any(String),
},
},
],
},
});
});

it('given single path param should translate to request with path', () => {
expect(translateToRequest({}, [fakePathParameter])).toMatchSnapshot();
expect(translateToRequest([fakePathParameter])).toMatchSnapshot({
path: [
{
id: expect.any(String),
},
],
});
});

it('given single query param should translate to request with query', () => {
expect(translateToRequest({}, [fakeQueryParameter])).toMatchSnapshot();
expect(translateToRequest([fakeQueryParameter])).toMatchSnapshot({
query: [
{
id: expect.any(String),
},
],
});
});

it('given single header param should translate to request with header', () => {
expect(translateToRequest({}, [fakeHeaderParameter])).toMatchSnapshot();
expect(translateToRequest([fakeHeaderParameter])).toMatchSnapshot({
headers: [
{
id: expect.any(String),
},
],
});
});

it('given two query params should translate', () => {
expect(translateToRequest({}, [fakeQueryParameter, fakeQueryParameter])).toMatchSnapshot();
expect(translateToRequest([fakeQueryParameter, fakeQueryParameter])).toMatchSnapshot({
query: [
{
id: expect.any(String),
},
],
});
});

it('given two header params should translate', () => {
expect(translateToRequest({}, [fakeHeaderParameter, fakeHeaderParameter])).toMatchSnapshot();
expect(translateToRequest([fakeHeaderParameter, fakeHeaderParameter])).toMatchSnapshot({
headers: [
{
id: expect.any(String),
},
],
});
});

it('given two path params should translate', () => {
expect(translateToRequest({}, [fakePathParameter, fakePathParameter])).toMatchSnapshot();
expect(translateToRequest([fakePathParameter, fakePathParameter])).toMatchSnapshot({
path: [
{
id: expect.any(String),
},
],
});
});

it('should translate mixed request', () => {
expect(
translateToRequest({}, [fakeParameter, fakeParameter, fakeParameter, fakeParameter, fakeParameter]),
).toMatchSnapshot();
translateToRequest([
fakeBodyParameter,
fakeQueryParameter,
fakePathParameter,
fakeHeaderParameter,
fakeFormParameter,
]),
).toMatchSnapshot({
body: {
id: expect.any(String),
contents: [
{
id: expect.any(String),
},
],
},
headers: [
{
id: expect.any(String),
},
],
path: [
{
id: expect.any(String),
},
],
query: [
{
id: expect.any(String),
},
],
});
});
});
Empty file removed src/oas2/transformers/content.ts
Empty file.
Loading

0 comments on commit d5a6c04

Please sign in to comment.