Skip to content

Commit 7ab07fb

Browse files
committed
Use x- prefix for read/write schema name
1 parent a52a7e3 commit 7ab07fb

File tree

5 files changed

+32
-22
lines changed

5 files changed

+32
-22
lines changed

src/transform/index.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -74,23 +74,23 @@ export function transformAll(schema: any, ctx: GlobalContext): Record<string, st
7474
const required = new Set(Object.keys(schema.components.schemas));
7575

7676
if (splitSchema) {
77-
output.components += ` ${readonly}requestSchemas: {\n ${transformSchemaObjMap(
77+
output.components += ` ${readonly}"x-requestSchemas": {\n ${transformSchemaObjMap(
7878
schema.components.schemas,
7979
{
8080
...ctx,
8181
required,
8282
requestResponse: "request",
8383
}
8484
)}\n }\n`;
85-
output.components += ` ${readonly}responseSchemas: {\n ${transformSchemaObjMap(
85+
output.components += ` ${readonly}"x-responseSchemas": {\n ${transformSchemaObjMap(
8686
schema.components.schemas,
8787
{
8888
...ctx,
8989
required,
9090
requestResponse: "response",
9191
}
9292
)}\n }\n`;
93-
output.components += ` ${readonly}schemas: {\n ${transformSchemaRefMap(
93+
output.components += ` ${readonly}"schemas": {\n ${transformSchemaRefMap(
9494
schema.components.schemas,
9595
ctx
9696
)}\n }\n`;

src/transform/operation.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { GlobalContext, OperationObject, ParameterObject, PathItemObject } from "../types";
2-
import { comment, isRef, tsReadonly } from "../utils";
2+
import { comment, isRef, transformRequestResponseRef, tsReadonly } from "../utils";
33
import { transformParametersArray } from "./parameters";
44
import { transformRequestBodyObj } from "./request";
55
import { transformResponsesObj } from "./responses";
@@ -34,7 +34,10 @@ export function transformOperationObj(operation: OperationObject, options: Trans
3434
const requestResponse = options.splitSchema ? "request" : undefined;
3535

3636
if (isRef(operation.requestBody)) {
37-
output += ` ${readonly}requestBody: ${transformRef(operation.requestBody.$ref, undefined, requestResponse)};\n`;
37+
output += ` ${readonly}requestBody: ${transformRequestResponseRef(
38+
operation.requestBody.$ref,
39+
requestResponse
40+
)};\n`;
3841
} else {
3942
if (operation.requestBody.description) output += comment(operation.requestBody.description);
4043
output += ` ${readonly}requestBody: {\n ${transformRequestBodyObj(operation.requestBody, {

src/transform/schema.ts

+14-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
import { GlobalContext } from "../types";
2-
import { comment, nodeType, tsArrayOf, tsIntersectionOf, tsPartial, tsReadonly, tsTupleOf, tsUnionOf } from "../utils";
2+
import {
3+
comment,
4+
nodeType,
5+
transformRequestResponseRef,
6+
tsArrayOf,
7+
tsIntersectionOf,
8+
tsPartial,
9+
tsReadonly,
10+
tsTupleOf,
11+
tsUnionOf,
12+
} from "../utils";
313

414
interface TransformSchemaObjOptions extends GlobalContext {
515
required: Set<string>;
@@ -52,7 +62,7 @@ export function transformSchemaRefMap(obj: Record<string, any>, options: GlobalC
5262

5363
for (const k of Object.keys(obj)) {
5464
// name (with “?” if optional property)
55-
output += `${readonly}"${k}": components["requestSchemas"]["${k}"] | components["responseSchemas"]["${k}"];\n`;
65+
output += `${readonly}"${k}": components["x-requestSchemas"]["${k}"] | components["x-responseSchemas"]["${k}"];\n`;
5666
}
5767

5868
return output.replace(/\n+$/, "\n"); // replace repeat line endings with only one
@@ -87,7 +97,8 @@ export function transformSchemaObj(node: any, options: TransformSchemaObjOptions
8797
// transform core type
8898
switch (nodeType(node)) {
8999
case "ref": {
90-
output += transformRef(node.$ref, undefined, options.requestResponse);
100+
// these were transformed at load time when remote schemas were resolved; only transform if read/write only
101+
output += transformRequestResponseRef(node.$ref, options.requestResponse);
91102
break;
92103
}
93104
case "string":

src/utils.ts

+6-10
Original file line numberDiff line numberDiff line change
@@ -97,17 +97,13 @@ export function swaggerVersion(definition: OpenAPI2 | OpenAPI3): 2 | 3 {
9797
);
9898
}
9999

100-
/** Convert $ref to TS ref */
101-
export function transformRef(ref: string, root = "", requestResponse?: RequestResponse): string {
102-
// TODO: load external file
103-
const isExternalRef = !ref.startsWith("#"); // if # isn’t first character, we can assume this is a remote schema
104-
if (isExternalRef) return "any";
105-
106-
const parts = ref.replace(/^#\//, root).split("/");
107-
if (requestResponse && parts[0] === "components" && parts[1] === "schemas") {
108-
parts[1] = requestResponse + "Schemas";
100+
/** Convert transformed $ref to TS ref if it's an internal one */
101+
export function transformRequestResponseRef(ref: string, requestResponse?: RequestResponse): string {
102+
if (requestResponse && ref.startsWith('components["schemas"]')) {
103+
return ref.replace("schemas", `x-${requestResponse}Schemas`);
109104
}
110-
return `${parts[0]}["${parts.slice(1).join('"]["')}"]`;
105+
106+
return ref;
111107
}
112108

113109
/** Decode $ref (https://swagger.io/docs/specification/using-ref/#escape) */

tests/schema.test.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ describe("SchemaObject", () => {
5252
properties: {
5353
object: {
5454
properties: {
55-
string: { $ref: "#/components/schemas/object_ref", readOnly: true },
56-
number: { $ref: "#/components/schemas/object_ref", writeOnly: true },
55+
string: { $ref: 'components["schemas"]["object_ref"]', readOnly: true },
56+
number: { $ref: 'components["schemas"]["object_ref"]', writeOnly: true },
5757
},
5858
type: "object",
5959
},
@@ -81,10 +81,10 @@ describe("SchemaObject", () => {
8181

8282
it("object (splitSchema)", () => {
8383
expect(transform(objReadWrite, { ...defaults, splitSchema: true, requestResponse: "request" })).toBe(
84-
`{\n"object"?: {\n"string"?: components["requestSchemas"]["object_ref"]; // GET requests only\n\n};\n\n}`
84+
`{\n"object"?: {\n"string"?: components["x-requestSchemas"]["object_ref"]; // GET requests only\n\n};\n\n}`
8585
);
8686
expect(transform(objReadWrite, { ...defaults, splitSchema: true, requestResponse: "response" })).toBe(
87-
`{\n"object"?: {\n"number"?: components["responseSchemas"]["object_ref"]; // POST/PUT/PATCH responses only\n\n};\n\n}`
87+
`{\n"object"?: {\n"number"?: components["x-responseSchemas"]["object_ref"]; // POST/PUT/PATCH responses only\n\n};\n\n}`
8888
);
8989
});
9090

0 commit comments

Comments
 (0)