From 888e8a550a5cc72b20b7049836af08b74496d90b Mon Sep 17 00:00:00 2001 From: Artem Shuvaev Date: Wed, 15 Dec 2021 17:17:19 +0500 Subject: [PATCH 1/2] fix(ISSUE-810): oneOf/allOf/anyOf with empty definitions at yaml generating error solving, also add tests --- src/utils.ts | 4 ++++ test/v3/expected/one-of-empty.ts | 30 ++++++++++++++++++++++++++++++ test/v3/specs/one-of-empty.yaml | 22 ++++++++++++++++++++++ 3 files changed, 56 insertions(+) create mode 100644 test/v3/expected/one-of-empty.ts create mode 100644 test/v3/specs/one-of-empty.yaml diff --git a/src/utils.ts b/src/utils.ts index bcb9d21a7..cc94cbb62 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -180,6 +180,8 @@ export function tsTupleOf(types: string[]): string { export function tsIntersectionOf(types: string[]): string { const typesWithValues = types.filter(Boolean); + if (!typesWithValues.length) return "undefined"; // usually allOf/anyOf with empty input - so it's undefined + if (typesWithValues.length === 1) return typesWithValues[0]; // don’t add parentheses around one thing return `(${typesWithValues.join(") & (")})`; } @@ -195,6 +197,8 @@ export function tsReadonly(immutable: boolean): string { /** Convert [X, Y, Z] into X | Y | Z */ export function tsUnionOf(types: Array): string { + if (!types.length) return "undefined"; // usually oneOf with empty input - so it's undefined + if (types.length === 1) return `${types[0]}`; // don’t add parentheses around one thing return `(${types.join(") | (")})`; } diff --git a/test/v3/expected/one-of-empty.ts b/test/v3/expected/one-of-empty.ts new file mode 100644 index 000000000..7d391f33d --- /dev/null +++ b/test/v3/expected/one-of-empty.ts @@ -0,0 +1,30 @@ +/** + * This file was auto-generated by openapi-typescript. + * Do not make direct changes to the file. + */ + +export interface paths { + "/test": { + get: { + responses: { + /** A list of types. */ + 200: unknown; + }; + }; + }; +} + +export interface components { + schemas: { + /** @description Enum with null and nullable */ + Example: { + emptyAllOf?: undefined; + emptyOneOf?: undefined; + emptyAnyOf?: undefined; + }; + }; +} + +export interface operations {} + +export interface external {} diff --git a/test/v3/specs/one-of-empty.yaml b/test/v3/specs/one-of-empty.yaml new file mode 100644 index 000000000..10bd4abb7 --- /dev/null +++ b/test/v3/specs/one-of-empty.yaml @@ -0,0 +1,22 @@ +openapi: 3.0 +paths: + /test: + get: + tags: + - test + summary: "Just a test path" + responses: + 200: + description: A list of types. +components: + schemas: + Example: + description: Enum with null and nullable + type: object + properties: + emptyAllOf: + allOf: [] + emptyOneOf: + oneOf: [] + emptyAnyOf: + anyOf: [] \ No newline at end of file From ef103f236c588e0d3de520e1c70e581cf8ddc112 Mon Sep 17 00:00:00 2001 From: Artem Shuvaev Date: Tue, 28 Dec 2021 01:32:00 +0500 Subject: [PATCH 2/2] fix(ISSUE-810): tests additional and immutable --- test/v3/expected/one-of-empty.additional.ts | 30 +++++++++++++++++++++ test/v3/expected/one-of-empty.immutable.ts | 30 +++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 test/v3/expected/one-of-empty.additional.ts create mode 100644 test/v3/expected/one-of-empty.immutable.ts diff --git a/test/v3/expected/one-of-empty.additional.ts b/test/v3/expected/one-of-empty.additional.ts new file mode 100644 index 000000000..079a85a4a --- /dev/null +++ b/test/v3/expected/one-of-empty.additional.ts @@ -0,0 +1,30 @@ +/** + * This file was auto-generated by openapi-typescript. + * Do not make direct changes to the file. + */ + +export interface paths { + "/test": { + get: { + responses: { + /** A list of types. */ + 200: unknown; + }; + }; + }; +} + +export interface components { + schemas: { + /** @description Enum with null and nullable */ + Example: { + emptyAllOf?: { [key: string]: unknown }; + emptyOneOf?: undefined & { [key: string]: unknown }; + emptyAnyOf?: { [key: string]: unknown }; + } & { [key: string]: unknown }; + }; +} + +export interface operations {} + +export interface external {} diff --git a/test/v3/expected/one-of-empty.immutable.ts b/test/v3/expected/one-of-empty.immutable.ts new file mode 100644 index 000000000..c88ded8ea --- /dev/null +++ b/test/v3/expected/one-of-empty.immutable.ts @@ -0,0 +1,30 @@ +/** + * This file was auto-generated by openapi-typescript. + * Do not make direct changes to the file. + */ + +export interface paths { + readonly "/test": { + readonly get: { + readonly responses: { + /** A list of types. */ + readonly 200: unknown; + }; + }; + }; +} + +export interface components { + readonly schemas: { + /** @description Enum with null and nullable */ + readonly Example: { + readonly emptyAllOf?: undefined; + readonly emptyOneOf?: undefined; + readonly emptyAnyOf?: undefined; + }; + }; +} + +export interface operations {} + +export interface external {}