Skip to content

Commit 10840e8

Browse files
committed
fix(schema-object): transform toplevel schemas
squash
1 parent cc8073b commit 10840e8

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

packages/openapi-typescript/src/transform/schema-object.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,21 @@ function transformSchemaObjectCore(
284284
options: TransformNodeOptions,
285285
): ts.TypeNode | undefined {
286286
if ("type" in schemaObject && schemaObject.type) {
287+
if (typeof options.ctx.transform === "function") {
288+
const result = options.ctx.transform(schemaObject, options);
289+
if (result) {
290+
if ("schema" in result) {
291+
if (result.optional) {
292+
return ts.factory.createUnionTypeNode([result.schema, UNDEFINED]);
293+
} else {
294+
return result.schema;
295+
}
296+
} else {
297+
return result;
298+
}
299+
}
300+
}
301+
287302
// primitives
288303
// type: null
289304
if (schemaObject.type === "null") {

packages/openapi-typescript/test/transform/request-body-object.test.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,37 @@ describe("transformRequestBodyObject", () => {
5353
// options: DEFAULT_OPTIONS,
5454
},
5555
],
56+
[
57+
"blob with transform",
58+
{
59+
given: {
60+
content: {
61+
"application/json": {
62+
schema: {
63+
type: "string",
64+
format: "binary",
65+
},
66+
},
67+
},
68+
},
69+
want: `{
70+
content: {
71+
"application/json": Blob;
72+
};
73+
}`,
74+
options: {
75+
...DEFAULT_OPTIONS,
76+
ctx: {
77+
...DEFAULT_CTX,
78+
transform(schemaObject) {
79+
if (schemaObject.format === "binary") {
80+
return BLOB;
81+
}
82+
},
83+
},
84+
},
85+
},
86+
],
5687
[
5788
"optional blob property with transform",
5889
{

0 commit comments

Comments
 (0)