Skip to content

fix(ISSUE-810): oneOf/allOf/anyOf fixing #830

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(") & (")})`;
}
Expand All @@ -195,6 +197,8 @@ export function tsReadonly(immutable: boolean): string {

/** Convert [X, Y, Z] into X | Y | Z */
export function tsUnionOf(types: Array<string | number | boolean>): 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(") | (")})`;
}
30 changes: 30 additions & 0 deletions test/v3/expected/one-of-empty.additional.ts
Original file line number Diff line number Diff line change
@@ -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 {}
30 changes: 30 additions & 0 deletions test/v3/expected/one-of-empty.immutable.ts
Original file line number Diff line number Diff line change
@@ -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 {}
30 changes: 30 additions & 0 deletions test/v3/expected/one-of-empty.ts
Original file line number Diff line number Diff line change
@@ -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 {}
22 changes: 22 additions & 0 deletions test/v3/specs/one-of-empty.yaml
Original file line number Diff line number Diff line change
@@ -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: []