Skip to content

Improve generated types for unknown schema objects #769

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 1 commit into from
Sep 30, 2021
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
944 changes: 472 additions & 472 deletions examples/stripe-openapi2.ts

Large diffs are not rendered by default.

434 changes: 218 additions & 216 deletions examples/stripe-openapi3.ts

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions src/transform/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export function transformSchemaObj(node: any, options: TransformSchemaObjOptions
case "number":
case "boolean":
case "unknown": {
output += nodeType(node) || "any";
output += nodeType(node);
break;
}
case "enum": {
Expand Down Expand Up @@ -139,7 +139,7 @@ export function transformSchemaObj(node: any, options: TransformSchemaObjOptions
(node.additionalProperties === undefined && options.additionalProperties && options.version === 3)
) {
if ((node.additionalProperties ?? true) === true || Object.keys(node.additionalProperties).length === 0) {
additionalProperties = `{ ${readonly}[key: string]: any }`;
additionalProperties = `{ ${readonly}[key: string]: unknown }`;
} else if (typeof node.additionalProperties === "object") {
const oneOf: any[] | undefined = (node.additionalProperties as any).oneOf || undefined; // TypeScript does a really bad job at inference here, so we enforce a type
const anyOf: any[] | undefined = (node.additionalProperties as any).anyOf || undefined; // "
Expand All @@ -149,7 +149,7 @@ export function transformSchemaObj(node: any, options: TransformSchemaObjOptions
additionalProperties = `{ ${readonly}[key: string]: ${transformAnyOf(anyOf, options)}; }`;
} else {
additionalProperties = `{ ${readonly}[key: string]: ${
transformSchemaObj(node.additionalProperties, options) || "any"
transformSchemaObj(node.additionalProperties, options) || "unknown"
}; }`;
}
}
Expand All @@ -172,7 +172,7 @@ export function transformSchemaObj(node: any, options: TransformSchemaObjOptions
if (Array.isArray(node.items)) {
output += `${readonly}${tsTupleOf(node.items.map((node: any) => transformSchemaObj(node, options)))}`;
} else {
output += `${readonly}${tsArrayOf(node.items ? transformSchemaObj(node.items as any, options) : "any")}`;
output += `${readonly}${tsArrayOf(node.items ? transformSchemaObj(node.items as any, options) : "unknown")}`;
}
break;
}
Expand Down
21 changes: 14 additions & 7 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ type SchemaObjectType =
| "ref"
| "string"
| "unknown";
export function nodeType(obj: any): SchemaObjectType | undefined {
export function nodeType(obj: any): SchemaObjectType {
if (!obj || typeof obj !== "object") {
return undefined;
return "unknown";
}

if (obj.$ref) {
Expand Down Expand Up @@ -77,13 +77,20 @@ export function nodeType(obj: any): SchemaObjectType | undefined {
return "array";
}

// file
if (obj.type === "file") {
return "unknown";
// object
if (
obj.type === "object" ||
obj.hasOwnProperty("allOf") ||
obj.hasOwnProperty("anyOf") ||
obj.hasOwnProperty("oneOf") ||
obj.hasOwnProperty("properties") ||
obj.hasOwnProperty("additionalProperties")
) {
return "object";
}

// return object by default
return "object";
// return unknown by default
return "unknown";
}

/** Return OpenAPI version from definition */
Expand Down
2 changes: 1 addition & 1 deletion tests/request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ describe("requestBodies", () => {
content: {
"application/json": {
test?: string;
} & { [key: string]: any };
} & { [key: string]: unknown };
};
};`)
);
Expand Down
8 changes: 4 additions & 4 deletions tests/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ describe("SchemaObject", () => {
expect(transform(objUnknown, { ...defaults })).toBe(`{ [key: string]: unknown }`);

// empty
expect(transform({}, { ...defaults })).toBe(`{ [key: string]: unknown }`);
expect(transform({}, { ...defaults })).toBe(`unknown`);

// nullable
expect(transform(objNullable, { ...defaults })).toBe(`({\n"string"?: string;\n\n}) | null`);
Expand All @@ -74,7 +74,7 @@ describe("SchemaObject", () => {
`{\nreadonly "object"?: {\nreadonly "string"?: string;\nreadonly "number"?: components["schemas"]["object_ref"];\n\n};\n\n}`
);
expect(transform(objUnknown, opts)).toBe(`{ readonly [key: string]: unknown }`);
expect(transform({}, opts)).toBe(`{ readonly [key: string]: unknown }`);
expect(transform({}, opts)).toBe(`unknown`);
expect(transform(objNullable, opts)).toBe(`({\nreadonly "string"?: string;\n\n}) | null`);
expect(transform(objRequired, opts)).toBe(`{\nreadonly "required": string;\nreadonly "optional"?: boolean;\n\n}`);
});
Expand Down Expand Up @@ -218,10 +218,10 @@ describe("SchemaObject", () => {
describe("advanced", () => {
it("additionalProperties", () => {
// boolean
expect(transform({ additionalProperties: true }, { ...defaults })).toBe(`{ [key: string]: any }`);
expect(transform({ additionalProperties: true }, { ...defaults })).toBe(`{ [key: string]: unknown }`);

// empty object
expect(transform({ additionalProperties: {} }, { ...defaults })).toBe(`{ [key: string]: any }`);
expect(transform({ additionalProperties: {} }, { ...defaults })).toBe(`{ [key: string]: unknown }`);

// type
expect(transform({ additionalProperties: { type: "string" } }, { ...defaults })).toBe(
Expand Down
Loading