Skip to content

Commit 927e526

Browse files
authored
Fix V2 parameter types (#489)
* Fix V2 parameter types * Fix V2 parameter types * Make it readable fix #489 (comment) * Use `schema` object when `in` is `body` fix #489 (comment) * Rename variable fix #489 (comment) * Fix bug * Fix types * Update v2 snapshot tests
1 parent 3a20869 commit 927e526

File tree

12 files changed

+1094
-999
lines changed

12 files changed

+1094
-999
lines changed

src/transform/index.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,14 @@ export function transformAll(schema: any, { version, rawSchema }: TransformOptio
3434
// #/paths (V2 & V3)
3535
output += `export interface paths {\n`; // open paths
3636
if (schema.paths) {
37-
output += transformPathsObj(schema.paths, {
38-
operations,
39-
parameters: (schema.components && schema.components.parameters) || schema.parameters,
40-
});
37+
output += transformPathsObj(
38+
schema.paths,
39+
{
40+
operations,
41+
parameters: (schema.components && schema.components.parameters) || schema.parameters,
42+
},
43+
version
44+
);
4145
}
4246
output += `}\n\n`; // close paths
4347

@@ -110,6 +114,7 @@ export function transformAll(schema: any, { version, rawSchema }: TransformOptio
110114
if (operation.description) output += comment(operation.description); // handle comment
111115
output += ` "${operationId}": {\n ${transformOperationObj(
112116
operation,
117+
version,
113118
schema.components && schema.components.parameters
114119
)}\n }\n`;
115120
});

src/transform/operation.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ import { transformSchemaObj } from "./schema";
55

66
export function transformOperationObj(
77
operation: OperationObject,
8+
version: number,
89
globalParams?: Record<string, ParameterObject>
910
): string {
1011
let output = "";
1112

1213
if (operation.parameters) {
13-
output += ` parameters: {\n ${transformParametersArray(operation.parameters, globalParams)}\n }\n`;
14+
output += ` parameters: {\n ${transformParametersArray(operation.parameters, version, globalParams)}\n }\n`;
1415
}
1516

1617
if (operation.responses) {

src/transform/parameters.ts

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { comment } from "../utils";
33

44
export function transformParametersArray(
55
parameters: (ReferenceObject | ParameterObject)[],
6+
version: number,
67
globalParams?: Record<string, ParameterObject>
78
): string {
89
let output = "";
@@ -15,10 +16,17 @@ export function transformParametersArray(
1516
if (globalParams[paramName]) {
1617
const reference = globalParams[paramName] as any;
1718
if (!mappedParams[reference.in]) mappedParams[reference.in] = {};
18-
mappedParams[reference.in][reference.name || paramName] = {
19-
...reference,
20-
schema: { $ref: paramObj.$ref },
21-
} as any;
19+
if (version === 2) {
20+
mappedParams[reference.in][reference.name || paramName] = {
21+
...reference,
22+
$ref: paramObj.$ref,
23+
};
24+
} else if (version === 3) {
25+
mappedParams[reference.in][reference.name || paramName] = {
26+
...reference,
27+
schema: { $ref: paramObj.$ref },
28+
};
29+
}
2230
}
2331
return;
2432
}
@@ -38,9 +46,19 @@ export function transformParametersArray(
3846
if (paramComment) output += comment(paramComment);
3947

4048
const required = paramObj.required ? `` : `?`;
41-
output += ` "${paramName}"${required}: ${
42-
paramObj.schema ? transformSchemaObj(paramObj.schema) : "unknown"
43-
};\n`;
49+
let paramType = ``;
50+
if (version === 2) {
51+
if (paramObj.in === "body" && paramObj.schema) {
52+
paramType = transformSchemaObj(paramObj.schema);
53+
} else if (paramObj.type) {
54+
paramType = transformSchemaObj(paramObj);
55+
} else {
56+
paramType = "unknown";
57+
}
58+
} else if (version === 3) {
59+
paramType = paramObj.schema ? transformSchemaObj(paramObj.schema) : "unknown";
60+
}
61+
output += ` "${paramName}"${required}: ${paramType};\n`;
4462
});
4563
output += ` }\n`; // close in
4664
});

src/transform/paths.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ interface TransformPathsObjOption {
1010
/** Note: this needs to mutate objects passed in */
1111
export function transformPathsObj(
1212
paths: Record<string, PathItemObject>,
13-
{ operations, parameters }: TransformPathsObjOption
13+
{ operations, parameters }: TransformPathsObjOption,
14+
version: number
1415
): string {
1516
let output = "";
1617

@@ -40,12 +41,16 @@ export function transformPathsObj(
4041
}
4142

4243
// otherwise, inline operation
43-
output += ` "${method}": {\n ${transformOperationObj(operation, parameters)}\n }\n`;
44+
output += ` "${method}": {\n ${transformOperationObj(operation, version, parameters)}\n }\n`;
4445
});
4546

4647
// parameters
4748
if (pathItem.parameters) {
48-
output += ` parameters: {\n ${transformParametersArray(pathItem.parameters, parameters)}\n }\n`;
49+
output += ` parameters: {\n ${transformParametersArray(
50+
pathItem.parameters,
51+
version,
52+
parameters
53+
)}\n }\n`;
4954
}
5055

5156
output += ` }\n`; // close PathItem

tests/operation.test.ts

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,21 @@ import { transformRequestBodies } from "../src/transform/responses";
55
describe("requestBody", () => {
66
it("basic", () => {
77
expect(
8-
transformOperationObj({
9-
requestBody: {
10-
content: {
11-
"application/json": {
12-
schema: { $ref: "#/components/schemas/Pet" },
13-
},
14-
"application/xml": {
15-
schema: { $ref: "#/components/schemas/Pet" },
8+
transformOperationObj(
9+
{
10+
requestBody: {
11+
content: {
12+
"application/json": {
13+
schema: { $ref: "#/components/schemas/Pet" },
14+
},
15+
"application/xml": {
16+
schema: { $ref: "#/components/schemas/Pet" },
17+
},
1618
},
1719
},
1820
},
19-
}).trim()
21+
3
22+
).trim()
2023
).toBe(`requestBody: {
2124
content: {
2225
"application/json": components["schemas"]["Pet"];
@@ -27,9 +30,12 @@ describe("requestBody", () => {
2730

2831
it("ref", () => {
2932
expect(
30-
transformOperationObj({
31-
requestBody: { $ref: "#/components/requestBodies/Request" },
32-
}).trim()
33+
transformOperationObj(
34+
{
35+
requestBody: { $ref: "#/components/requestBodies/Request" },
36+
},
37+
3
38+
).trim()
3339
).toBe(`requestBody: components["requestBodies"]["Request"];`);
3440
});
3541
});

tests/parameters.test.ts

Lines changed: 99 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,116 @@
11
import { transformParametersArray } from "../src/transform/parameters";
22

3-
describe("transformParametersArray()", () => {
4-
it("basic", () => {
5-
expect(
6-
transformParametersArray([
7-
{
8-
description: "Specifies which fields in the response should be expanded.",
9-
in: "query",
10-
name: "expand",
11-
required: false,
12-
schema: {
13-
items: {
14-
type: "string",
3+
describe.only("transformParametersArray()", () => {
4+
describe("v2", () => {
5+
it("basic", () => {
6+
expect(
7+
transformParametersArray(
8+
[
9+
{
10+
description: "Specifies which fields in the response should be expanded.",
11+
in: "query",
12+
name: "expand",
13+
required: false,
14+
items: {
15+
type: "string",
16+
},
17+
type: "array",
1518
},
16-
type: "array",
17-
},
18-
},
19-
{
20-
in: "path",
21-
name: "three_d_secure",
22-
required: true,
23-
schema: {
24-
type: "string",
25-
},
26-
},
27-
]).trim()
28-
).toBe(
29-
`query: {
19+
{ in: "path", name: "three_d_secure", required: true, type: "string" },
20+
{ in: "body", name: "payload", schema: { type: "string" } },
21+
],
22+
2
23+
).trim()
24+
).toBe(
25+
`query: {
3026
/** Specifies which fields in the response should be expanded. */
3127
"expand"?: (string)[];
3228
}
3329
path: {
3430
"three_d_secure": string;
31+
}
32+
body: {
33+
"payload"?: string;
3534
}`
36-
);
35+
);
36+
});
37+
it("$ref", () => {
38+
expect(
39+
transformParametersArray(
40+
[{ $ref: "#/parameters/per_page" }, { $ref: "#/parameters/page" }, { $ref: "#/parameters/since" }],
41+
2,
42+
{
43+
per_page: { in: "query", name: "per_page", required: true, type: "number" },
44+
page: { in: "query", name: "page", type: "number" },
45+
since: { in: "query", name: "since", type: "string" },
46+
}
47+
).trim()
48+
).toBe(`query: {
49+
"per_page": parameters["per_page"];
50+
"page"?: parameters["page"];
51+
"since"?: parameters["since"];
52+
}`);
53+
});
3754
});
55+
describe("v3", () => {
56+
it("basic", () => {
57+
expect(
58+
transformParametersArray(
59+
[
60+
{
61+
description: "Specifies which fields in the response should be expanded.",
62+
in: "query",
63+
name: "expand",
64+
required: false,
65+
schema: {
66+
items: {
67+
type: "string",
68+
},
69+
type: "array",
70+
},
71+
},
72+
{
73+
in: "path",
74+
name: "three_d_secure",
75+
required: true,
76+
schema: {
77+
type: "string",
78+
},
79+
},
80+
],
81+
3
82+
).trim()
83+
).toBe(
84+
`query: {
85+
/** Specifies which fields in the response should be expanded. */
86+
"expand"?: (string)[];
87+
}
88+
path: {
89+
"three_d_secure": string;
90+
}`
91+
);
92+
});
3893

39-
it("$ref", () => {
40-
expect(
41-
transformParametersArray(
42-
[
43-
{ $ref: "#/components/parameters/per_page" },
44-
{ $ref: "#/components/parameters/page" },
45-
{ $ref: "#/components/parameters/since" },
46-
],
47-
{
48-
per_page: { in: "query", name: "per_page", required: true },
49-
page: { in: "query", name: "page" },
50-
since: { in: "query", name: "since" },
51-
}
52-
).trim()
53-
).toBe(`query: {
94+
it("$ref", () => {
95+
expect(
96+
transformParametersArray(
97+
[
98+
{ $ref: "#/components/parameters/per_page" },
99+
{ $ref: "#/components/parameters/page" },
100+
{ $ref: "#/components/parameters/since" },
101+
],
102+
3,
103+
{
104+
per_page: { in: "query", name: "per_page", required: true },
105+
page: { in: "query", name: "page" },
106+
since: { in: "query", name: "since" },
107+
}
108+
).trim()
109+
).toBe(`query: {
54110
"per_page": components["parameters"]["per_page"];
55111
"page"?: components["parameters"]["page"];
56112
"since"?: components["parameters"]["since"];
57113
}`);
114+
});
58115
});
59116
});

tests/paths.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import prettier from "prettier";
22
import { transformPathsObj } from "../src/transform/paths";
33

44
const transform = (schema: any, operations: any = { operations: {}, parameters: {} }, parameters?: any) =>
5-
prettier.format(`export interface paths {\n${transformPathsObj(schema, { operations, parameters })}\n}`.trim(), {
5+
prettier.format(`export interface paths {\n${transformPathsObj(schema, { operations, parameters }, 3)}\n}`.trim(), {
66
parser: "typescript",
77
});
88

0 commit comments

Comments
 (0)