Skip to content
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
13 changes: 12 additions & 1 deletion src/types/OpenAPI3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@ export interface OpenAPI3Paths {

export interface OpenAPI3Operation {
description?: string;
parameters?: OpenAPI3Parameter[];
parameters?: Parameter[];
requestBody?: OpenAPI3RequestBody;
responses: {
[statusCode: string]: OpenAPI3ResponseObject;
};
}

export type Parameter = { $ref: string } | OpenAPI3Parameter;

export interface OpenAPI3Parameter {
name: string;
description?: string;
Expand All @@ -37,9 +40,17 @@ export interface OpenAPI3ResponseObject {
};
}

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

export interface OpenAPI3Components {
schemas: OpenAPI3Schemas;
responses?: { [key: string]: OpenAPI3ResponseObject };
parameters?: { [key: string]: OpenAPI3Parameter };
}

export interface OpenAPI3 {
Expand Down
49 changes: 42 additions & 7 deletions src/v3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ export default function generateTypesV3(
}

// 4. transform
output += transform(value);
output += transform(value.schema ? value.schema : value);

// 5. close nullable
if (value.nullable) {
Expand All @@ -166,19 +166,38 @@ export default function generateTypesV3(
output += `parameters: {\n`;
const allParameters: Record<
string,
Record<string, OpenAPI3Parameter>
Record<string, OpenAPI3Parameter | string>
> = {};
operation.parameters.forEach((p) => {
if (!allParameters[p.in]) allParameters[p.in] = {};
// TODO: handle $ref parameters
if (p.name) {
allParameters[p.in][p.name] = p;
if ("$ref" in p) {
const referencedValue = (p.$ref
.substr(2)
.split("/")
.reduce(
(value, property) => value[property],
input
) as unknown) as OpenAPI3Parameter;

if (!allParameters[referencedValue.in])
allParameters[referencedValue.in] = {};

allParameters[referencedValue.in][
referencedValue.name
] = transformRef(p.$ref);
return;
}

if (!allParameters[p.in]) allParameters[p.in] = {};
allParameters[p.in][p.name] = p;
});

Object.entries(allParameters).forEach(([loc, locParams]) => {
output += `"${loc}": {\n`;
Object.entries(locParams).forEach(([paramName, paramProps]) => {
if (typeof paramProps === "string") {
output += `"${paramName}": ${paramProps}\n`;
return;
}
if (paramProps.description)
output += comment(paramProps.description);
output += `"${paramName}"${
Expand All @@ -190,6 +209,17 @@ export default function generateTypesV3(
output += `}\n`;
}

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

// handle responses
output += `responses: {\n`;
Object.entries(operation.responses).forEach(
Expand Down Expand Up @@ -242,7 +272,12 @@ export default function generateTypesV3(

finalOutput += "export interface components {\n";

// TODO: handle components.parameters
if (components.parameters && Object.keys(components.parameters).length) {
finalOutput += `
parameters: {
${createKeys(components.parameters, Object.keys(components.parameters))}
}\n`;
}

if (Object.keys(propertyMapped).length) {
finalOutput += `schemas: {
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']
}
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