Skip to content

Commit 5a1f86b

Browse files
authored
fix(schema-object): transform toplevel schema object (#1622)
* fix(schema-object): transform toplevel schemas fix * fix: changeset
1 parent 41cf263 commit 5a1f86b

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

.changeset/smooth-cameras-fail.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"openapi-typescript": patch
3+
---
4+
5+
Transform toplevel schemas

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,21 @@ export function transformSchemaObjectWithComposition(
236236
*/
237237
function transformSchemaObjectCore(schemaObject: SchemaObject, options: TransformNodeOptions): ts.TypeNode | undefined {
238238
if ("type" in schemaObject && schemaObject.type) {
239+
if (typeof options.ctx.transform === "function") {
240+
const result = options.ctx.transform(schemaObject, options);
241+
if (result) {
242+
if ("schema" in result) {
243+
if (result.questionToken) {
244+
return ts.factory.createUnionTypeNode([result.schema, UNDEFINED]);
245+
} else {
246+
return result.schema;
247+
}
248+
} else {
249+
return result;
250+
}
251+
}
252+
}
253+
239254
// primitives
240255
// type: null
241256
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)