Skip to content

Fixed openapi 3 spec to default additionalProperties to true #585

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
May 7, 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
2 changes: 1 addition & 1 deletion src/transform/headers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { transformSchemaObj } from "./schema";

export function transformHeaderObjMap(
headerMap: Record<string, HeaderObject>,
options: { formatter?: SchemaFormatter; immutableTypes: boolean }
options: { formatter?: SchemaFormatter; immutableTypes: boolean; version: number }
): string {
let output = "";

Expand Down
10 changes: 10 additions & 0 deletions src/transform/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@ export function transformAll(schema: any, { formatter, immutableTypes, rawSchema
formatter,
immutableTypes,
required: Object.keys(schema),
version,
})}\n}`;
}
case 3: {
return `export interface schemas {\n ${transformSchemaObjMap(schema, {
formatter,
immutableTypes,
required: Object.keys(schema),
version,
})}\n }\n\n`;
}
}
Expand All @@ -60,6 +62,7 @@ export function transformAll(schema: any, { formatter, immutableTypes, rawSchema
formatter,
immutableTypes,
required: Object.keys(schema.definitions),
version,
})}\n}\n\n`;
}

Expand All @@ -70,6 +73,7 @@ export function transformAll(schema: any, { formatter, immutableTypes, rawSchema
formatter,
immutableTypes,
required,
version,
})}\n }\n\n`;
}

Expand All @@ -78,6 +82,7 @@ export function transformAll(schema: any, { formatter, immutableTypes, rawSchema
output += `export interface responses {\n ${transformResponsesObj(schema.responses, {
formatter,
immutableTypes,
version,
})}\n }\n\n`;
}
break;
Expand All @@ -94,6 +99,7 @@ export function transformAll(schema: any, { formatter, immutableTypes, rawSchema
formatter,
immutableTypes,
required,
version,
})}\n }\n`;
}

Expand All @@ -102,6 +108,7 @@ export function transformAll(schema: any, { formatter, immutableTypes, rawSchema
output += ` ${readonly}responses: {\n ${transformResponsesObj(schema.components.responses, {
formatter,
immutableTypes,
version,
})}\n }\n`;
}

Expand All @@ -112,6 +119,7 @@ export function transformAll(schema: any, { formatter, immutableTypes, rawSchema
formatter,
immutableTypes,
required,
version,
})}\n }\n`;
}

Expand All @@ -120,6 +128,7 @@ export function transformAll(schema: any, { formatter, immutableTypes, rawSchema
output += ` ${readonly}requestBodies: {\n ${transformRequestBodies(schema.components.requestBodies, {
formatter,
immutableTypes,
version,
})}\n }\n`;
}

Expand All @@ -128,6 +137,7 @@ export function transformAll(schema: any, { formatter, immutableTypes, rawSchema
output += ` ${readonly}headers: {\n ${transformHeaderObjMap(schema.components.headers, {
formatter,
immutableTypes,
version,
})} }\n`;
}
}
Expand Down
7 changes: 4 additions & 3 deletions src/transform/operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export function transformOperationObj(
if (operation.responses) {
output += ` ${readonly}responses: {\n ${transformResponsesObj(operation.responses, {
immutableTypes,
version,
})}\n }\n`;
}

Expand All @@ -44,7 +45,7 @@ export function transformOperationObj(
if (operation.requestBody.description) output += comment(operation.requestBody.description);

output += ` ${readonly}requestBody: {\n`; // open requestBody
output += ` ${transformRequestBodyObj(operation.requestBody, { immutableTypes })}`;
output += ` ${transformRequestBodyObj(operation.requestBody, { immutableTypes, version })}`;
output += ` }\n`; // close requestBody
}
}
Expand All @@ -54,7 +55,7 @@ export function transformOperationObj(

export function transformRequestBodyObj(
requestBody: RequestBody,
{ immutableTypes }: { immutableTypes: boolean }
{ immutableTypes, version }: { immutableTypes: boolean; version: number }
): string {
const readonly = tsReadonly(immutableTypes);

Expand All @@ -66,7 +67,7 @@ export function transformRequestBodyObj(
output += ` ${readonly}content: {\n`; // open content

Object.entries(content).forEach(([k, v]) => {
output += ` ${readonly}"${k}": ${transformSchemaObj(v.schema, { immutableTypes })};\n`;
output += ` ${readonly}"${k}": ${transformSchemaObj(v.schema, { immutableTypes, version })};\n`;
});
output += ` }\n`; // close content
} else {
Expand Down
6 changes: 3 additions & 3 deletions src/transform/parameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,14 @@ export function transformParametersArray(
let paramType = ``;
if (version === 2) {
if (paramObj.in === "body" && paramObj.schema) {
paramType = transformSchemaObj(paramObj.schema, { immutableTypes });
paramType = transformSchemaObj(paramObj.schema, { immutableTypes, version });
} else if (paramObj.type) {
paramType = transformSchemaObj(paramObj, { immutableTypes });
paramType = transformSchemaObj(paramObj, { immutableTypes, version });
} else {
paramType = "unknown";
}
} else if (version === 3) {
paramType = paramObj.schema ? transformSchemaObj(paramObj.schema, { immutableTypes }) : "unknown";
paramType = paramObj.schema ? transformSchemaObj(paramObj.schema, { immutableTypes, version }) : "unknown";
}
output += ` ${readonly}"${paramName}"${required}: ${paramType};\n`;
});
Expand Down
1 change: 1 addition & 0 deletions src/transform/responses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const resType = (res: string | number) => (res === 204 || (res >= 300 && res < 4
interface Options {
formatter?: SchemaFormatter;
immutableTypes: boolean;
version: number;
}

export function transformResponsesObj(responsesObj: Record<string, any>, options: Options): string {
Expand Down
10 changes: 7 additions & 3 deletions src/transform/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ interface TransformSchemaObjMapOptions {
formatter?: SchemaFormatter;
immutableTypes: boolean;
required?: string[];
version: number;
}

/** Take object keys and convert to TypeScript interface */
Expand All @@ -34,6 +35,7 @@ export function transformSchemaObjMap(obj: Record<string, any>, options: Transfo
output += transformSchemaObj(value.schema || value, {
formatter: options.formatter,
immutableTypes: options.immutableTypes,
version: options.version,
});

// 4. close
Expand All @@ -46,6 +48,7 @@ export function transformSchemaObjMap(obj: Record<string, any>, options: Transfo
interface Options {
formatter?: SchemaFormatter;
immutableTypes: boolean;
version: number;
}

/** transform anyOf */
Expand Down Expand Up @@ -112,13 +115,14 @@ export function transformSchemaObj(node: any, options: Options): string {
let properties = transformSchemaObjMap(node.properties || {}, {
immutableTypes: options.immutableTypes,
required: node.required,
version: options.version,
});

// if additional properties, add an intersection with a generic map type
let additionalProperties: string | undefined;
if (node.additionalProperties) {
if (node.additionalProperties === true || Object.keys(node.additionalProperties).length === 0) {
additionalProperties = `{ [key: string]: any }`;
if (node.additionalProperties || (node.additionalProperties === undefined && options.version === 3)) {
if ((node.additionalProperties ?? true) === true || Object.keys(node.additionalProperties).length === 0) {
additionalProperties = `{ ${readonly}[key: string]: any }`;
} 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 Down
16 changes: 8 additions & 8 deletions tests/bin/expected/prettier-js.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ export interface components {
/** Order Status */
status?: 'placed' | 'approved' | 'delivered'
complete?: boolean
}
} & { [key: string]: any }
Category: {
id?: number
name?: string
}
} & { [key: string]: any }
User: {
id?: number
username?: string
Expand All @@ -88,11 +88,11 @@ export interface components {
phone?: string
/** User Status */
userStatus?: number
}
} & { [key: string]: any }
Tag: {
id?: number
name?: string
}
} & { [key: string]: any }
Pet: {
id?: number
category?: components['schemas']['Category']
Expand All @@ -101,12 +101,12 @@ export interface components {
tags?: components['schemas']['Tag'][]
/** pet status in the store */
status?: 'available' | 'pending' | 'sold'
}
} & { [key: string]: any }
ApiResponse: {
code?: number
type?: string
message?: string
}
} & { [key: string]: any }
}
}

Expand Down Expand Up @@ -221,7 +221,7 @@ export interface operations {
name?: string
/** Updated status of the pet */
status?: string
}
} & { [key: string]: any }
}
}
}
Expand Down Expand Up @@ -264,7 +264,7 @@ export interface operations {
additionalMetadata?: string
/** file to upload */
file?: string
}
} & { [key: string]: any }
}
}
}
Expand Down
16 changes: 8 additions & 8 deletions tests/bin/expected/prettier-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ export interface components {
/** Order Status */
status?: 'placed' | 'approved' | 'delivered'
complete?: boolean
}
} & { [key: string]: any }
Category: {
id?: number
name?: string
}
} & { [key: string]: any }
User: {
id?: number
username?: string
Expand All @@ -88,11 +88,11 @@ export interface components {
phone?: string
/** User Status */
userStatus?: number
}
} & { [key: string]: any }
Tag: {
id?: number
name?: string
}
} & { [key: string]: any }
Pet: {
id?: number
category?: components['schemas']['Category']
Expand All @@ -101,12 +101,12 @@ export interface components {
tags?: components['schemas']['Tag'][]
/** pet status in the store */
status?: 'available' | 'pending' | 'sold'
}
} & { [key: string]: any }
ApiResponse: {
code?: number
type?: string
message?: string
}
} & { [key: string]: any }
}
}

Expand Down Expand Up @@ -221,7 +221,7 @@ export interface operations {
name?: string
/** Updated status of the pet */
status?: string
}
} & { [key: string]: any }
}
}
}
Expand Down Expand Up @@ -264,7 +264,7 @@ export interface operations {
additionalMetadata?: string
/** file to upload */
file?: string
}
} & { [key: string]: any }
}
}
}
Expand Down
16 changes: 8 additions & 8 deletions tests/bin/expected/stdout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ export interface components {
/** Order Status */
status?: "placed" | "approved" | "delivered";
complete?: boolean;
};
} & { [key: string]: any };
Category: {
id?: number;
name?: string;
};
} & { [key: string]: any };
User: {
id?: number;
username?: string;
Expand All @@ -88,11 +88,11 @@ export interface components {
phone?: string;
/** User Status */
userStatus?: number;
};
} & { [key: string]: any };
Tag: {
id?: number;
name?: string;
};
} & { [key: string]: any };
Pet: {
id?: number;
category?: components["schemas"]["Category"];
Expand All @@ -101,12 +101,12 @@ export interface components {
tags?: components["schemas"]["Tag"][];
/** pet status in the store */
status?: "available" | "pending" | "sold";
};
} & { [key: string]: any };
ApiResponse: {
code?: number;
type?: string;
message?: string;
};
} & { [key: string]: any };
};
}

Expand Down Expand Up @@ -221,7 +221,7 @@ export interface operations {
name?: string;
/** Updated status of the pet */
status?: string;
};
} & { [key: string]: any };
};
};
};
Expand Down Expand Up @@ -264,7 +264,7 @@ export interface operations {
additionalMetadata?: string;
/** file to upload */
file?: string;
};
} & { [key: string]: any };
};
};
};
Expand Down
Loading