Skip to content

Commit 5d85a9b

Browse files
authored
fix(constructor): accept skipNormalization option (#2142)
Refs #2140
1 parent 594e413 commit 5d85a9b

File tree

3 files changed

+106
-0
lines changed

3 files changed

+106
-0
lines changed

docs/usage/tags-interface.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ Option | Description
9999
`requestInterceptor` | `Function=identity`. Either synchronous or asynchronous function transformer that accepts `Request` and should return `Request`.
100100
`responseInterceptor` | `Function=identity`. Either synchronous or asynchronous function transformer that accepts `Response` and should return `Response`.
101101
`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.
102+
`skipNormalization` | `Boolean=false`. Normalization creates unique operationIds when explicit operationIds are duplicates, and preserve originals.
102103

103104
> *__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.*
104105

src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ Swagger.prototype = {
7474
useCircularStructures: this.useCircularStructures,
7575
requestInterceptor: this.requestInterceptor || null,
7676
responseInterceptor: this.responseInterceptor || null,
77+
skipNormalization: this.skipNormalization || false,
7778
...options,
7879
}).then((obj) => {
7980
this.originalSpec = this.spec;

test/index.js

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import xmock from 'xmock';
2+
import cloneDeep from 'lodash/cloneDeep';
23

34
import Swagger from '../src/index';
45

@@ -710,4 +711,107 @@ describe('constructor', () => {
710711
.catch(cb);
711712
});
712713
});
714+
715+
describe('skipNormalization', () => {
716+
const spec = {
717+
openapi: '3.0.0',
718+
info: {
719+
title: 'Cloudpotato - Medwork',
720+
description: 'The Cloudpotato API',
721+
version: '1.0.3',
722+
contact: {},
723+
},
724+
tags: [],
725+
servers: [],
726+
paths: {
727+
'/v1/clients/{id}/groups': {
728+
get: {
729+
operationId: 'getGroups',
730+
summary: '',
731+
parameters: [
732+
{
733+
name: 'options',
734+
required: false,
735+
in: 'query',
736+
schema: {
737+
type: 'string',
738+
},
739+
},
740+
{
741+
name: 'id',
742+
required: true,
743+
in: 'path',
744+
schema: {
745+
type: 'number',
746+
},
747+
},
748+
],
749+
responses: {
750+
200: {
751+
description: '',
752+
},
753+
},
754+
tags: ['clients'],
755+
security: [
756+
{
757+
bearer: [],
758+
},
759+
],
760+
},
761+
},
762+
'/v1/groups': {
763+
get: {
764+
operationId: 'getGroups',
765+
summary: '',
766+
parameters: [],
767+
responses: {
768+
200: {
769+
description: '',
770+
},
771+
},
772+
tags: ['groups'],
773+
security: [
774+
{
775+
bearer: [],
776+
},
777+
],
778+
},
779+
},
780+
},
781+
};
782+
783+
/**
784+
* We're deep-cloning the spec before using it as resolution mutates
785+
* the original object.
786+
*/
787+
788+
describe('skipNormalization', () => {
789+
describe('given skipNormalization option not provided', () => {
790+
test('should resolve with normalized interfaces', async () => {
791+
const client = await new Swagger({ spec: cloneDeep(spec) });
792+
793+
expect(client.apis.clients.getGroups1).toBeInstanceOf(Function);
794+
expect(client.apis.groups.getGroups2).toBeInstanceOf(Function);
795+
});
796+
});
797+
798+
describe('given skipNormalization option provided as `false`', () => {
799+
test('should resolve with normalized interfaces', async () => {
800+
const client = await new Swagger({ spec: cloneDeep(spec), skipNormalization: false });
801+
802+
expect(client.apis.clients.getGroups1).toBeInstanceOf(Function);
803+
expect(client.apis.groups.getGroups2).toBeInstanceOf(Function);
804+
});
805+
});
806+
807+
describe('given skipNormalization option provided as `true`', () => {
808+
test('should resolve with normalized interfaces', async () => {
809+
const client = await new Swagger({ spec: cloneDeep(spec), skipNormalization: true });
810+
811+
expect(client.apis.clients.getGroups).toBeInstanceOf(Function);
812+
expect(client.apis.groups.getGroups).toBeInstanceOf(Function);
813+
});
814+
});
815+
});
816+
});
713817
});

0 commit comments

Comments
 (0)