Skip to content
Closed
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
8 changes: 8 additions & 0 deletions src/types/OpenAPI3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export interface OpenAPI3Paths {
export interface OpenAPI3Response {
description?: string;
parameters?: OpenAPI3Parameter[];
requestBody?: OpenAPI3RequestBody;
responses: {
[statusCode: string]: OpenAPI3ResponseObject;
};
Expand All @@ -37,6 +38,13 @@ export interface OpenAPI3ResponseObject {
};
}

export interface OpenAPI3RequestBody {
description?: string;
content: {
[contentType: string]: { schema: OpenAPI3SchemaObject | { $ref: string } };
};
}

export interface OpenAPI3Components {
schemas: OpenAPI3Schemas;
responses?: { [key: string]: OpenAPI3ResponseObject };
Expand Down
2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export function comment(text: string): string {
}

/** shim for Object.fromEntries() for Node < 13 */
export function fromEntries(entries: [string, any][]): object {
export function fromEntries(entries: [string, any][]): Record<string, unknown> {
return entries.reduce((obj, [key, val]) => ({ ...obj, [key]: val }), {});
}

Expand Down
11 changes: 11 additions & 0 deletions src/v3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,17 @@ export default function generateTypesV3(
output += `}\n`;
}

// handle requestBody
if (responses.requestBody) {
output += `requestBody: {\n`;
Object.entries(responses.requestBody.content).forEach(
([contentType, { schema }]) => {
output += `"${contentType}": ${transform(schema)};\n`;
}
);
output += `}\n`;
}

// handle responses
output += `responses: {\n`;
Object.entries(responses.responses).forEach(
Expand Down
47 changes: 47 additions & 0 deletions tests/bin/expected/petstore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
export interface paths {
'/pet': {
put: {
requestBody: {
'application/json': components['schemas']['Pet']
'application/xml': components['schemas']['Pet']
}
responses: {
/**
* Invalid ID supplied
Expand All @@ -22,6 +26,10 @@ export interface paths {
}
}
post: {
requestBody: {
'application/json': components['schemas']['Pet']
'application/xml': components['schemas']['Pet']
}
responses: {
/**
* Invalid input
Expand Down Expand Up @@ -126,6 +134,18 @@ export interface paths {
petId: number
}
}
requestBody: {
'application/x-www-form-urlencoded': {
/**
* Updated name of the pet
*/
name?: string
/**
* Updated status of the pet
*/
status?: string
}
}
responses: {
/**
* Invalid input
Expand Down Expand Up @@ -167,6 +187,18 @@ export interface paths {
petId: number
}
}
requestBody: {
'multipart/form-data': {
/**
* Additional data to pass to server
*/
additionalMetadata?: string
/**
* file to upload
*/
file?: string
}
}
responses: {
/**
* successful operation
Expand Down Expand Up @@ -194,6 +226,9 @@ export interface paths {
}
'/store/order': {
post: {
requestBody: {
'*/*': components['schemas']['Order']
Copy link
Contributor

Choose a reason for hiding this comment

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

Ah interesting. Didn’t even know you could do this

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not exactly sure if this is part of the specification, but GitHub is using it for its "Upload a release asset" endpoint, which allows you to upload any kind of file, of any type

}
responses: {
/**
* successful operation
Expand Down Expand Up @@ -269,6 +304,9 @@ export interface paths {
* This can only be done by the logged in user.
*/
post: {
requestBody: {
'*/*': components['schemas']['User']
}
responses: {
/**
* successful operation
Expand All @@ -279,6 +317,9 @@ export interface paths {
}
'/user/createWithArray': {
post: {
requestBody: {
'*/*': components['schemas']['User'][]
}
responses: {
/**
* successful operation
Expand All @@ -289,6 +330,9 @@ export interface paths {
}
'/user/createWithList': {
post: {
requestBody: {
'*/*': components['schemas']['User'][]
}
responses: {
/**
* successful operation
Expand Down Expand Up @@ -376,6 +420,9 @@ export interface paths {
username: string
}
}
requestBody: {
'*/*': components['schemas']['User']
}
responses: {
/**
* Invalid user supplied
Expand Down
Loading