Skip to content

Commit

Permalink
If no type is specified, type as unknown (#1049)
Browse files Browse the repository at this point in the history
* Add logic for unknown type

* Remove unknown from ts intersections

* Make tsIntersectionOf a bit more robust for edge cases

* Add basic tests for tsIntersectionOf

* update:examples

* not intended to be committed
  • Loading branch information
mitchell-merry committed Apr 7, 2023
1 parent 04c8379 commit 6408fa6
Show file tree
Hide file tree
Showing 9 changed files with 137 additions and 103 deletions.
120 changes: 60 additions & 60 deletions examples/digital-ocean-api.ts

Large diffs are not rendered by default.

40 changes: 20 additions & 20 deletions examples/github-api-next.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11899,8 +11899,8 @@ export interface components {
comments_url: string;
owner?: components["schemas"]["simple-user"];
truncated?: boolean;
forks?: (Record<string, never>)[];
history?: (Record<string, never>)[];
forks?: (unknown)[];
history?: (unknown)[];
};
/**
* Public User
Expand Down Expand Up @@ -12039,8 +12039,8 @@ export interface components {
comments_url: string;
owner?: null | components["schemas"]["simple-user"];
truncated?: boolean;
forks?: (Record<string, never>)[];
history?: (Record<string, never>)[];
forks?: (unknown)[];
history?: (unknown)[];
}, null]>;
url?: string;
forks_url?: string;
Expand Down Expand Up @@ -19026,8 +19026,8 @@ export interface components {
primary_key_id?: number;
key_id?: string;
public_key?: string;
emails?: (Record<string, never>)[];
subkeys?: (Record<string, never>)[];
emails?: (unknown)[];
subkeys?: (unknown)[];
can_sign?: boolean;
can_encrypt_comms?: boolean;
can_encrypt_storage?: boolean;
Expand Down Expand Up @@ -36760,7 +36760,7 @@ export interface components {
}, null]>;
created_at?: string;
description: string;
docker_metadata?: (Record<string, never>)[];
docker_metadata?: (unknown)[];
draft?: boolean;
/** Format: uri */
html_url: string;
Expand Down Expand Up @@ -36887,7 +36887,7 @@ export interface components {
/** Format: uri */
url: string;
};
rubygems_metadata?: (Record<string, never>)[];
rubygems_metadata?: (unknown)[];
source_url?: string;
summary: string;
tag_name?: string;
Expand Down Expand Up @@ -37007,14 +37007,14 @@ export interface components {
body_html: string;
created_at: string;
description: string;
docker_metadata?: (Record<string, never>)[];
docker_metadata?: (unknown)[];
draft?: boolean;
/** Format: uri */
html_url: string;
id: number;
installation_command: string;
manifest?: string;
metadata: (Record<string, never>)[];
metadata: (unknown)[];
name: string;
package_files: ({
content_type: string;
Expand Down Expand Up @@ -37083,7 +37083,7 @@ export interface components {
/** Format: uri */
url: string;
};
rubygems_metadata?: (Record<string, never>)[];
rubygems_metadata?: (unknown)[];
/** Format: uri */
source_url?: string;
summary: string;
Expand Down Expand Up @@ -69358,7 +69358,7 @@ export interface components {
};
created_at?: string;
description: string;
docker_metadata?: (Record<string, never>)[];
docker_metadata?: (unknown)[];
draft?: boolean;
html_url: string;
id: number;
Expand Down Expand Up @@ -69464,7 +69464,7 @@ export interface components {
target_commitish?: string;
url?: string;
};
rubygems_metadata?: (Record<string, never>)[];
rubygems_metadata?: (unknown)[];
summary: string;
tag_name?: string;
target_commitish?: string;
Expand Down Expand Up @@ -69549,7 +69549,7 @@ export interface components {
id: number;
installation_command: string;
manifest?: string;
metadata: (Record<string, never>)[];
metadata: (unknown)[];
name: string;
package_files: ({
content_type?: string;
Expand Down Expand Up @@ -69598,7 +69598,7 @@ export interface components {
target_commitish: string;
url: string;
};
rubygems_metadata?: (Record<string, never>)[];
rubygems_metadata?: (unknown)[];
summary: string;
tag_name?: string;
target_commitish: string;
Expand Down Expand Up @@ -77716,9 +77716,9 @@ export interface operations {
content?: string;
/** @description The new filename for the file. */
filename?: OneOf<[string, null]>;
} & (Record<string, never> | Record<string, never> | Record<string, never>), null]>) | undefined;
} & (unknown | unknown | Record<string, never>), null]>) | undefined;
};
}) & (Record<string, never> | Record<string, never>), null]>;
}) & (unknown | unknown), null]>;
};
};
responses: {
Expand Down Expand Up @@ -95046,7 +95046,7 @@ export interface operations {
*/
path: "/" | "/docs";
});
}) & (Record<string, never> | Record<string, never> | Record<string, never> | Record<string, never> | Record<string, never>);
}) & (unknown | unknown | unknown | unknown | unknown);
};
};
responses: {
Expand Down Expand Up @@ -95089,7 +95089,7 @@ export interface operations {
*/
path?: "/" | "/docs";
};
}) & (Record<string, never> | Record<string, never>), null]>;
}) & (unknown | unknown), null]>;
};
};
responses: {
Expand Down Expand Up @@ -96101,7 +96101,7 @@ export interface operations {
reviewers?: (string)[];
/** @description An array of team `slug`s that will be requested. */
team_reviewers?: (string)[];
} & (Record<string, never> | Record<string, never>);
} & (unknown | unknown);
};
};
responses: {
Expand Down
40 changes: 20 additions & 20 deletions examples/github-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10226,8 +10226,8 @@ export interface components {
comments_url: string;
owner?: components["schemas"]["simple-user"];
truncated?: boolean;
forks?: (Record<string, never>)[];
history?: (Record<string, never>)[];
forks?: (unknown)[];
history?: (unknown)[];
};
/**
* Public User
Expand Down Expand Up @@ -10371,8 +10371,8 @@ export interface components {
comments_url: string;
owner?: components["schemas"]["nullable-simple-user"];
truncated?: boolean;
forks?: (Record<string, never>)[];
history?: (Record<string, never>)[];
forks?: (unknown)[];
history?: (unknown)[];
}) | null;
url?: string;
forks_url?: string;
Expand Down Expand Up @@ -21050,8 +21050,8 @@ export interface components {
primary_key_id?: number;
key_id?: string;
public_key?: string;
emails?: (Record<string, never>)[];
subkeys?: (Record<string, never>)[];
emails?: (unknown)[];
subkeys?: (unknown)[];
can_sign?: boolean;
can_encrypt_comms?: boolean;
can_encrypt_storage?: boolean;
Expand Down Expand Up @@ -38870,7 +38870,7 @@ export interface components {
}) | null;
created_at?: string;
description: string;
docker_metadata?: (Record<string, never>)[];
docker_metadata?: (unknown)[];
draft?: boolean;
/** Format: uri */
html_url: string;
Expand Down Expand Up @@ -38997,7 +38997,7 @@ export interface components {
/** Format: uri */
url: string;
};
rubygems_metadata?: (Record<string, never>)[];
rubygems_metadata?: (unknown)[];
source_url?: string;
summary: string;
tag_name?: string;
Expand Down Expand Up @@ -39117,14 +39117,14 @@ export interface components {
body_html: string;
created_at: string;
description: string;
docker_metadata?: (Record<string, never>)[];
docker_metadata?: (unknown)[];
draft?: boolean;
/** Format: uri */
html_url: string;
id: number;
installation_command: string;
manifest?: string;
metadata: (Record<string, never>)[];
metadata: (unknown)[];
name: string;
package_files: ({
content_type: string;
Expand Down Expand Up @@ -39193,7 +39193,7 @@ export interface components {
/** Format: uri */
url: string;
};
rubygems_metadata?: (Record<string, never>)[];
rubygems_metadata?: (unknown)[];
/** Format: uri */
source_url?: string;
summary: string;
Expand Down Expand Up @@ -71468,7 +71468,7 @@ export interface components {
};
created_at?: string;
description: string;
docker_metadata?: (Record<string, never>)[];
docker_metadata?: (unknown)[];
draft?: boolean;
html_url: string;
id: number;
Expand Down Expand Up @@ -71574,7 +71574,7 @@ export interface components {
target_commitish?: string;
url?: string;
};
rubygems_metadata?: (Record<string, never>)[];
rubygems_metadata?: (unknown)[];
summary: string;
tag_name?: string;
target_commitish?: string;
Expand Down Expand Up @@ -71659,7 +71659,7 @@ export interface components {
id: number;
installation_command: string;
manifest?: string;
metadata: (Record<string, never>)[];
metadata: (unknown)[];
name: string;
package_files: ({
content_type?: string;
Expand Down Expand Up @@ -71708,7 +71708,7 @@ export interface components {
target_commitish: string;
url: string;
};
rubygems_metadata?: (Record<string, never>)[];
rubygems_metadata?: (unknown)[];
summary: string;
tag_name?: string;
target_commitish: string;
Expand Down Expand Up @@ -79864,9 +79864,9 @@ export interface operations {
content?: string;
/** @description The new filename for the file. */
filename?: string | null;
}) & (Record<string, never> | Record<string, never> | Record<string, never>)) | null) | undefined;
}) & (unknown | unknown | Record<string, never>)) | null) | undefined;
};
}) & (Record<string, never> | Record<string, never>)) | null;
}) & (unknown | unknown)) | null;
};
};
responses: {
Expand Down Expand Up @@ -97284,7 +97284,7 @@ export interface operations {
*/
path: "/" | "/docs";
});
}) & (Record<string, never> | Record<string, never> | Record<string, never> | Record<string, never> | Record<string, never>);
}) & (unknown | unknown | unknown | unknown | unknown);
};
};
responses: {
Expand Down Expand Up @@ -97327,7 +97327,7 @@ export interface operations {
*/
path?: "/" | "/docs";
};
}) & (Record<string, never> | Record<string, never>)) | null;
}) & (unknown | unknown)) | null;
};
};
responses: {
Expand Down Expand Up @@ -98344,7 +98344,7 @@ export interface operations {
reviewers?: (string)[];
/** @description An array of team `slug`s that will be requested. */
team_reviewers?: (string)[];
} & (Record<string, never> | Record<string, never>);
} & (unknown | unknown);
};
};
responses: {
Expand Down
6 changes: 3 additions & 3 deletions examples/octokit-ghes-3.6-diff-to-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3539,13 +3539,13 @@ export interface components {
ldap?: {
host?: string | null;
port?: number;
base?: (Record<string, never>)[];
base?: (unknown)[];
uid?: string | null;
bind_dn?: string | null;
password?: string | null;
method?: string;
search_strategy?: string;
user_groups?: (Record<string, never>)[];
user_groups?: (unknown)[];
admin_group?: string | null;
virtual_attribute_enabled?: boolean;
recursive_group_search?: boolean;
Expand Down Expand Up @@ -9371,7 +9371,7 @@ export interface operations {
op: "add" | "Add" | "remove" | "Remove" | "replace" | "Replace";
path?: string;
/** @description Can be any value - string, number, array or object. */
value?: Record<string, never>;
value?: unknown;
})[];
};
};
Expand Down
3 changes: 3 additions & 0 deletions src/transform/schema-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,9 @@ export function defaultSchemaObjectTransform(

if (finalType) return finalType;

// any type
if (!("type" in schemaObject)) return "unknown";

// if no type could be generated, fall back to “empty object” type
return ctx.emptyObjectsUnknown ? "Record<string, unknown>" : "Record<string, never>";
}
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,7 @@ export type SchemaObject = {
}
| { allOf: (SchemaObject | ReferenceObject)[]; anyOf?: (SchemaObject | ReferenceObject)[]; required?: string[] }
| { allOf?: (SchemaObject | ReferenceObject)[]; anyOf: (SchemaObject | ReferenceObject)[]; required?: string[] }
| {}
);

/**
Expand Down
2 changes: 2 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ export function tsArrayOf(type: string): string {

/** X & Y & Z; */
export function tsIntersectionOf(...types: string[]): string {
types = types.filter((t) => t !== "unknown");
if (types.length === 0) return "unknown";
if (types.length === 1) return String(types[0]); // don’t add parentheses around one thing
return types.map((t) => (TS_UNION_INTERSECTION_RE.test(t) ? `(${t})` : t)).join(" & ");
}
Expand Down
5 changes: 5 additions & 0 deletions test/schema-object.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,11 @@ describe("Schema Object", () => {
});
});
});

test("unknown", () => {
const generated = transformSchemaObject({}, options);
expect(generated).toBe(`unknown`);
});
});

describe("ReferenceObject", () => {
Expand Down
Loading

0 comments on commit 6408fa6

Please sign in to comment.