From 0a3b4d298924203a30951e7e43ef9a743271b98f Mon Sep 17 00:00:00 2001 From: Jakob Niggel Date: Tue, 29 Mar 2022 17:53:46 +0200 Subject: [PATCH] feat: improve anyOf handling --- .../schema/__tests__/nullableSchema.test.ts | 7 +++ .../schema/helper/convertNullToNullable.ts | 12 ++-- ...v2.test.ts.snap => swagger-2.test.ts.snap} | 2 +- .../__snapshots__/swagger.test.ts.snap | 58 ------------------- .../tests-e2e/swagger-2.test.ts | 6 ++ .../tests-e2e/swagger.test.ts | 7 +-- 6 files changed, 23 insertions(+), 69 deletions(-) rename packages/integration-tests/tests-e2e/__snapshots__/{swagger-v2.test.ts.snap => swagger-2.test.ts.snap} (98%) create mode 100644 packages/integration-tests/tests-e2e/swagger-2.test.ts diff --git a/packages/cli/src/schema/__tests__/nullableSchema.test.ts b/packages/cli/src/schema/__tests__/nullableSchema.test.ts index 0cef037c..748fda02 100644 --- a/packages/cli/src/schema/__tests__/nullableSchema.test.ts +++ b/packages/cli/src/schema/__tests__/nullableSchema.test.ts @@ -103,6 +103,9 @@ const input2 = { multiNull2: { type: ['number', 'string'], }, + multiNull3: { + type: ['null', 'string'], + }, }, additionalProperties: false, $id: 'AnyResponse', @@ -137,6 +140,10 @@ test('convert anyOf to nullable', () => { }, ], }, + "multiNull3": Object { + "nullable": true, + "type": "string", + }, }, "type": "object", }, diff --git a/packages/cli/src/schema/helper/convertNullToNullable.ts b/packages/cli/src/schema/helper/convertNullToNullable.ts index dcfe9ea8..73d7a413 100644 --- a/packages/cli/src/schema/helper/convertNullToNullable.ts +++ b/packages/cli/src/schema/helper/convertNullToNullable.ts @@ -24,12 +24,16 @@ export function convertNullToNullable(object: any): any { if (value.type.includes('null')) { value.nullable = true; } - value.anyOf = value.type - .filter((x: string) => x !== 'null') - .map((x: string) => { + const notNullTypes = value.type.filter((x: string) => x !== 'null'); + if (notNullTypes.length === 1) { + value.type = notNullTypes[0]; + } else { + value.anyOf = notNullTypes.map((x: string) => { return { type: x }; }); - delete value.type; + delete value.type; + } + return convertNullToNullable(value); } }); diff --git a/packages/integration-tests/tests-e2e/__snapshots__/swagger-v2.test.ts.snap b/packages/integration-tests/tests-e2e/__snapshots__/swagger-2.test.ts.snap similarity index 98% rename from packages/integration-tests/tests-e2e/__snapshots__/swagger-v2.test.ts.snap rename to packages/integration-tests/tests-e2e/__snapshots__/swagger-2.test.ts.snap index 542048cb..a03afa8f 100644 --- a/packages/integration-tests/tests-e2e/__snapshots__/swagger-v2.test.ts.snap +++ b/packages/integration-tests/tests-e2e/__snapshots__/swagger-2.test.ts.snap @@ -1,6 +1,6 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`schema 1`] = ` +exports[`schema2 1`] = ` Object { "components": Object { "schemas": Object { diff --git a/packages/integration-tests/tests-e2e/__snapshots__/swagger.test.ts.snap b/packages/integration-tests/tests-e2e/__snapshots__/swagger.test.ts.snap index 0740962e..ebd283d0 100644 --- a/packages/integration-tests/tests-e2e/__snapshots__/swagger.test.ts.snap +++ b/packages/integration-tests/tests-e2e/__snapshots__/swagger.test.ts.snap @@ -1184,61 +1184,3 @@ Object { }, } `; - -exports[`schema 2`] = ` -Object { - "components": Object { - "schemas": Object { - "V2Response": Object { - "additionalProperties": false, - "properties": Object { - "foo": Object { - "type": "string", - }, - }, - "required": Array [ - "foo", - ], - "type": "object", - }, - }, - }, - "info": Object { - "description": "", - "title": "Api", - "version": "", - }, - "openapi": "3.0.3", - "paths": Object { - "/v2": Object { - "get": Object { - "operationId": "string", - "responses": Object { - "200": Object { - "content": Object { - "application/json": Object { - "schema": Object { - "additionalProperties": false, - "properties": Object { - "foo": Object { - "type": "string", - }, - }, - "required": Array [ - "foo", - ], - "type": "object", - }, - }, - }, - "description": "Default Response", - }, - }, - "tags": Array [ - "v2", - ], - }, - }, - }, -} -`; diff --git a/packages/integration-tests/tests-e2e/swagger-2.test.ts b/packages/integration-tests/tests-e2e/swagger-2.test.ts new file mode 100644 index 00000000..127ffbfd --- /dev/null +++ b/packages/integration-tests/tests-e2e/swagger-2.test.ts @@ -0,0 +1,6 @@ +import { client2 } from './api'; + +test('schema2', async () => { + const { data } = await client2.instance.get('/json'); + expect(data).toMatchSnapshot(); +}); diff --git a/packages/integration-tests/tests-e2e/swagger.test.ts b/packages/integration-tests/tests-e2e/swagger.test.ts index 7bc8a329..9bfe609e 100644 --- a/packages/integration-tests/tests-e2e/swagger.test.ts +++ b/packages/integration-tests/tests-e2e/swagger.test.ts @@ -1,11 +1,6 @@ -import { client2, client } from './api'; +import { client } from './api'; test('schema', async () => { const { data } = await client.instance.get('/json'); expect(data).toMatchSnapshot(); }); - -test('schema', async () => { - const { data } = await client2.instance.get('/json'); - expect(data).toMatchSnapshot(); -});