Skip to content

Feat: add support for "type": "file" in parameters #766

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
3 changes: 2 additions & 1 deletion src/transform/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ export function transformSchemaObj(node: any, options: TransformSchemaObjOptions
}
case "string":
case "number":
case "boolean": {
case "boolean":
case "unknown": {
output += nodeType(node) || "any";
break;
}
Expand Down
17 changes: 16 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,17 @@ export function isRef(obj: any): obj is ReferenceObject {
}

/** Return type of node (works for v2 or v3, as there are no conflicting types) */
type SchemaObjectType = "anyOf" | "array" | "boolean" | "enum" | "number" | "object" | "oneOf" | "ref" | "string";
type SchemaObjectType =
| "anyOf"
| "array"
| "boolean"
| "enum"
| "number"
| "object"
| "oneOf"
| "ref"
| "string"
| "unknown";
export function nodeType(obj: any): SchemaObjectType | undefined {
if (!obj || typeof obj !== "object") {
return undefined;
Expand Down Expand Up @@ -67,6 +77,11 @@ export function nodeType(obj: any): SchemaObjectType | undefined {
return "array";
}

// file
if (obj.type === "file") {
return "unknown";
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At least for now, we’ll return unknown for a file type. Because we’re not sure where this is running—server or browser—it would seem risky to simply assume a Buffer or string type for a file. Instead, we’ll rely on the implementer to specify how to deal with the file.

}

// return object by default
return "object";
}
Expand Down
4 changes: 4 additions & 0 deletions tests/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,10 @@ describe("SchemaObject", () => {
`components["parameters"]["ReferenceObject"]`
);
});

it("file", () => {
expect(transform({ type: "file" }, { ...defaults })).toBe("unknown");
});
});

describe("advanced", () => {
Expand Down
2 changes: 1 addition & 1 deletion tests/v2/expected/petstore.immutable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ export interface operations {
/** Additional data to pass to server */
readonly additionalMetadata?: string;
/** file to upload */
readonly file?: { readonly [key: string]: unknown };
readonly file?: unknown;
};
};
readonly responses: {
Expand Down
2 changes: 1 addition & 1 deletion tests/v2/expected/petstore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ export interface operations {
/** Additional data to pass to server */
additionalMetadata?: string;
/** file to upload */
file?: { [key: string]: unknown };
file?: unknown;
};
};
responses: {
Expand Down