Skip to content

Commit fcdd8b9

Browse files
authored
test: optional referenced parameters (#352)
* test: optional referenced parameters reproduces bug reported in #351 * fix: optional referenced parameters * test: adapt GitHub OpenAPI spec fixtures
1 parent 57ee832 commit fcdd8b9

File tree

4 files changed

+434
-411
lines changed

4 files changed

+434
-411
lines changed

src/utils.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,9 @@ export function tsPartial(type: string): string {
124124
export function tsUnionOf(types: string[]): string {
125125
return `(${types.join(") | (")})`;
126126
}
127+
128+
/** Convert the components object and a 'components["parameters"]["param"]' string into the `param` object **/
129+
export function unrefComponent(components: any, ref: string) {
130+
const [type, object] = ref.match(/(?<=\[")([^"]+)/g) as string[];
131+
return components[type][object];
132+
}

src/v3.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
tsPartial,
1919
tsUnionOf,
2020
tsTupleOf,
21+
unrefComponent,
2122
} from "./utils";
2223

2324
export const PRIMITIVES: { [key: string]: "boolean" | "string" | "number" } = {
@@ -193,7 +194,9 @@ export default function generateTypesV3(
193194
output += `"${loc}": {\n`;
194195
Object.entries(locParams).forEach(([paramName, paramProps]) => {
195196
if (typeof paramProps === "string") {
196-
output += `"${paramName}": ${paramProps}\n`;
197+
const { required } = unrefComponent(components, paramProps);
198+
const key = required ? `"${paramName}"` : `"${paramName}"?`;
199+
output += `${key}: ${paramProps}\n`;
197200
return;
198201
}
199202
if (paramProps.description) output += comment(paramProps.description);

0 commit comments

Comments
 (0)