diff --git a/src/oas/transformers/security.ts b/src/oas/transformers/security.ts deleted file mode 100644 index e69de29b..00000000 diff --git a/src/oas2/__tests__/operation.test.ts b/src/oas2/__tests__/operation.test.ts index a43e338b..4d722656 100644 --- a/src/oas2/__tests__/operation.test.ts +++ b/src/oas2/__tests__/operation.test.ts @@ -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', }, ], diff --git a/src/oas2/transformers/__tests__/__snapshots__/request.test.ts.snap b/src/oas2/transformers/__tests__/__snapshots__/request.test.ts.snap index c496d16a..3c87ea1f 100644 --- a/src/oas2/transformers/__tests__/__snapshots__/request.test.ts.snap +++ b/src/oas2/transformers/__tests__/__snapshots__/request.test.ts.snap @@ -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, + "mediaType": "*", + }, + ], + "id": Any, }, "cookie": Array [], "headers": Array [], @@ -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, + "mediaType": "*", + "schema": Object { + "$schema": "http://json-schema.org/draft-07/schema#", + "properties": Object { + "param": Object { + "type": "number", + }, + }, + "type": "object", + "x-stoplight-id": Any, + }, + }, + ], + "id": Any, }, "cookie": Array [], "headers": Array [], @@ -32,8 +52,9 @@ Object { "cookie": Array [], "headers": Array [ Object { - "in": "header", + "id": Any, "name": "param", + "style": "simple", }, ], "path": Array [], @@ -47,9 +68,10 @@ Object { "headers": Array [], "path": Array [ Object { - "in": "path", + "id": Any, "name": "param", "required": true, + "style": "simple", }, ], "query": Array [], @@ -63,8 +85,9 @@ Object { "path": Array [], "query": Array [ Object { - "in": "query", + "id": Any, "name": "param", + "style": "form", }, ], } @@ -75,8 +98,9 @@ Object { "cookie": Array [], "headers": Array [ Object { - "in": "header", + "id": Any, "name": "param", + "style": "simple", }, ], "path": Array [], @@ -90,9 +114,10 @@ Object { "headers": Array [], "path": Array [ Object { - "in": "path", + "id": Any, "name": "param", "required": true, + "style": "simple", }, ], "query": Array [], @@ -106,8 +131,9 @@ Object { "path": Array [], "query": Array [ Object { - "in": "query", + "id": Any, "name": "param", + "style": "form", }, ], } @@ -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, + "mediaType": "*", + }, + ], + "id": Any, }, "cookie": Array [], - "headers": Array [], - "path": Array [], - "query": Array [], + "headers": Array [ + Object { + "id": Any, + "name": "param", + "style": "simple", + }, + ], + "path": Array [ + Object { + "id": Any, + "name": "param", + "required": true, + "style": "simple", + }, + ], + "query": Array [ + Object { + "id": Any, + "name": "param", + "style": "form", + }, + ], } `; diff --git a/src/oas2/transformers/__tests__/params.test.ts b/src/oas2/transformers/__tests__/params.test.ts index 46e6c2af..42b709d2 100644 --- a/src/oas2/transformers/__tests__/params.test.ts +++ b/src/oas2/transformers/__tests__/params.test.ts @@ -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', @@ -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', diff --git a/src/oas2/transformers/__tests__/request.test.ts b/src/oas2/transformers/__tests__/request.test.ts index 4b990b1b..de584c4f 100644 --- a/src/oas2/transformers/__tests__/request.test.ts +++ b/src/oas2/transformers/__tests__/request.test.ts @@ -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, 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), + }, + ], + }); }); }); diff --git a/src/oas2/transformers/content.ts b/src/oas2/transformers/content.ts deleted file mode 100644 index e69de29b..00000000 diff --git a/src/oas2/transformers/params.ts b/src/oas2/transformers/params.ts index 894c3520..6d1d0f19 100644 --- a/src/oas2/transformers/params.ts +++ b/src/oas2/transformers/params.ts @@ -114,8 +114,14 @@ export const translateToBodyParameter = withContext< return { id: this.generateId(`http_media-${this.parentId}-${mediaType}`), mediaType, - schema: isPlainObject(body.schema) ? translateSchemaObject.call(this, body.schema) : void 0, examples, + + ...pickBy( + { + schema: isPlainObject(body.schema) ? translateSchemaObject.call(this, body.schema) : void 0, + }, + isNonNullable, + ), }; }), this, @@ -143,43 +149,48 @@ export const translateFromFormDataParameters = withContext< IHttpOperationRequestBody > >(function (parameters, consumes) { - const finalBody: IHttpOperationRequestBody = { - id: this.generateId(`http_request_body-${this.parentId}`), - contents: consumes.map( - withContext(mediaType => ({ - id: this.generateId(`http_media-${this.parentId}-${mediaType}`), - mediaType, - schema: translateSchemaObject.call(this, { type: 'object' }), - })), - this, - ), - }; + const finalBody: Omit & Required> = + { + id: this.generateId(`http_request_body-${this.parentId}`), + contents: consumes.map( + withContext(mediaType => ({ + id: this.generateId(`http_media-${this.parentId}-${mediaType}`), + mediaType, + + ...pickBy( + { + schema: + parameters.length > 0 ? translateSchemaObject.call(this, { type: 'object', properties: {} }) : void 0, + }, + isNonNullable, + ), + })), + this, + ), + }; return parameters.reduce((body, parameter) => { - const { schema, description } = buildSchemaForParameter.call(this, parameter); - (body.contents || []).forEach(content => { - delete schema.$schema; + const { schema = {}, description } = buildSchemaForParameter.call(this, parameter); + delete schema.$schema; + delete schema['x-stoplight-id']; - if (description) { + for (const content of body.contents) { + if (typeof description === 'string' && description.length > 0) { schema.description = description; } - content.schema ||= {}; - content.schema.properties ||= {}; - content.schema.properties[parameter.name] = schema; + content.schema!.properties![parameter.name] = schema; if (parameter.required) { - (content.schema.required ||= []).push(parameter.name); + (content.schema!.required ??= []).push(parameter.name); } - if (parameter.collectionFormat) { - content.encodings = content.encodings || []; - const encoding = buildEncoding(parameter); - if (encoding) { - content.encodings.push(encoding); - } + const encoding = buildEncoding(parameter); + if (encoding) { + (content.encodings ??= []).push(encoding); } - }); + } + return body; }, finalBody); }); @@ -259,7 +270,7 @@ export const translateToPathParameter = withContext< const buildSchemaForParameter: Oas2TranslateFunction< [param: DeepPartial], - { schema: JSONSchema7; description?: string; deprecated?: boolean } + { schema?: JSONSchema7; description?: string; deprecated?: boolean } > = function (param) { const schema = pick( param, @@ -287,7 +298,12 @@ const buildSchemaForParameter: Oas2TranslateFunction< } return { - schema: translateSchemaObject.call(this, schema), + ...pickBy( + { + schema: Object.keys(schema).length > 0 ? translateSchemaObject.call(this, schema) : void 0, + }, + isNonNullable, + ), ...pickBy( { diff --git a/src/oas3/transformers/__tests__/content.test.ts b/src/oas3/transformers/__tests__/content.test.ts index ac4ea12d..b226d569 100644 --- a/src/oas3/transformers/__tests__/content.test.ts +++ b/src/oas3/transformers/__tests__/content.test.ts @@ -13,10 +13,6 @@ const translateHeaderObject = (object: unknown, key: string) => _translateHeaderObject.call(createContext({}), [key, object], 0, []); describe('translateMediaTypeObject', () => { - afterEach(() => { - jest.restoreAllMocks(); - }); - it('should gracefully handle invalid data', () => { expect(translateMediaTypeObject({}, null, 'mediaType')).toBeUndefined(); });