diff --git a/docs/usage/tags-interface.md b/docs/usage/tags-interface.md index 3ff05e649..45e780f31 100644 --- a/docs/usage/tags-interface.md +++ b/docs/usage/tags-interface.md @@ -99,6 +99,7 @@ Option | Description `requestInterceptor` | `Function=identity`. Either synchronous or asynchronous function transformer that accepts `Request` and should return `Request`. `responseInterceptor` | `Function=identity`. Either synchronous or asynchronous function transformer that accepts `Response` and should return `Response`. `userFetch` | `Function=cross-fetch`. Custom **asynchronous** fetch function that accepts two arguments: the `url` and the `Request` object and must return a [Response](https://developer.mozilla.org/en-US/docs/Web/API/Response) object. +`skipNormalization` | `Boolean=false`. Normalization creates unique operationIds when explicit operationIds are duplicates, and preserve originals. > *__Note:__ for more information about [requestInterceptor](http-client.md#request-interceptor), [responseInterceptor](http-client.md#response-interceptor) and [userFetch](https://github.com/swagger-api/swagger-js/blob/master/docs/usage/http-client.md#custom-fetch), please refer to the [HTTP Client](http-client.md) documentation.* diff --git a/src/index.js b/src/index.js index 32412169a..a751b5a66 100644 --- a/src/index.js +++ b/src/index.js @@ -74,6 +74,7 @@ Swagger.prototype = { useCircularStructures: this.useCircularStructures, requestInterceptor: this.requestInterceptor || null, responseInterceptor: this.responseInterceptor || null, + skipNormalization: this.skipNormalization || false, ...options, }).then((obj) => { this.originalSpec = this.spec; diff --git a/test/index.js b/test/index.js index 0faddaaaa..4ee2f2ce5 100644 --- a/test/index.js +++ b/test/index.js @@ -1,4 +1,5 @@ import xmock from 'xmock'; +import cloneDeep from 'lodash/cloneDeep'; import Swagger from '../src/index'; @@ -710,4 +711,107 @@ describe('constructor', () => { .catch(cb); }); }); + + describe('skipNormalization', () => { + const spec = { + openapi: '3.0.0', + info: { + title: 'Cloudpotato - Medwork', + description: 'The Cloudpotato API', + version: '1.0.3', + contact: {}, + }, + tags: [], + servers: [], + paths: { + '/v1/clients/{id}/groups': { + get: { + operationId: 'getGroups', + summary: '', + parameters: [ + { + name: 'options', + required: false, + in: 'query', + schema: { + type: 'string', + }, + }, + { + name: 'id', + required: true, + in: 'path', + schema: { + type: 'number', + }, + }, + ], + responses: { + 200: { + description: '', + }, + }, + tags: ['clients'], + security: [ + { + bearer: [], + }, + ], + }, + }, + '/v1/groups': { + get: { + operationId: 'getGroups', + summary: '', + parameters: [], + responses: { + 200: { + description: '', + }, + }, + tags: ['groups'], + security: [ + { + bearer: [], + }, + ], + }, + }, + }, + }; + + /** + * We're deep-cloning the spec before using it as resolution mutates + * the original object. + */ + + describe('skipNormalization', () => { + describe('given skipNormalization option not provided', () => { + test('should resolve with normalized interfaces', async () => { + const client = await new Swagger({ spec: cloneDeep(spec) }); + + expect(client.apis.clients.getGroups1).toBeInstanceOf(Function); + expect(client.apis.groups.getGroups2).toBeInstanceOf(Function); + }); + }); + + describe('given skipNormalization option provided as `false`', () => { + test('should resolve with normalized interfaces', async () => { + const client = await new Swagger({ spec: cloneDeep(spec), skipNormalization: false }); + + expect(client.apis.clients.getGroups1).toBeInstanceOf(Function); + expect(client.apis.groups.getGroups2).toBeInstanceOf(Function); + }); + }); + + describe('given skipNormalization option provided as `true`', () => { + test('should resolve with normalized interfaces', async () => { + const client = await new Swagger({ spec: cloneDeep(spec), skipNormalization: true }); + + expect(client.apis.clients.getGroups).toBeInstanceOf(Function); + expect(client.apis.groups.getGroups).toBeInstanceOf(Function); + }); + }); + }); + }); });