Skip to content

Commit 2ab288f

Browse files
authored
Merge pull request #1917 from Freddis/fixing_nullable_dates_in_transformers
2 parents c8eb3d6 + d7af22c commit 2ab288f

File tree

8 files changed

+56
-14
lines changed

8 files changed

+56
-14
lines changed

.changeset/red-days-camp.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@hey-api/openapi-ts": patch
3+
---
4+
5+
fix: handle nullable dates in transformers

packages/openapi-ts-tests/test/__snapshots__/3.0.x/transformers-any-of-null/transformers.gen.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ const fooSchemaResponseTransformer = (data: any) => {
99
if (data.bar) {
1010
data.bar = new Date(data.bar);
1111
}
12+
if (data.requiredBaz) {
13+
data.requiredBaz = new Date(data.requiredBaz);
14+
}
1215
return data;
1316
};
1417

packages/openapi-ts-tests/test/__snapshots__/3.0.x/transformers-any-of-null/types.gen.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
export type Foo = {
44
foo?: Date;
55
bar?: Date | null;
6+
requiredBaz: Date | null;
67
};
78

89
export type GetFooData = {

packages/openapi-ts-tests/test/__snapshots__/3.1.x/transformers-any-of-null/transformers.gen.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ const fooSchemaResponseTransformer = (data: any) => {
1212
if (data.baz) {
1313
data.baz = new Date(data.baz);
1414
}
15+
if (data.requiredQux) {
16+
data.requiredQux = new Date(data.requiredQux);
17+
}
1518
return data;
1619
};
1720

packages/openapi-ts-tests/test/__snapshots__/3.1.x/transformers-any-of-null/types.gen.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export type Foo = {
1313
foo?: Date;
1414
bar?: Date | null;
1515
baz?: Date | null;
16+
requiredQux: Date | null;
1617
};
1718

1819
export type GetFooData = {

packages/openapi-ts-tests/test/spec/3.0.x/transformers-any-of-null.json

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,18 @@
4242
"format": "date-time"
4343
}
4444
]
45+
},
46+
"requiredBaz": {
47+
"anyOf": [
48+
{
49+
"type": "string",
50+
"nullable": true,
51+
"format": "date-time"
52+
}
53+
]
4554
}
46-
}
55+
},
56+
"required": ["requiredBaz"]
4757
}
4858
}
4959
}

packages/openapi-ts-tests/test/spec/3.1.x/transformers-any-of-null.json

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,17 @@
8484
"format": "date-time"
8585
}
8686
]
87+
},
88+
"requiredQux": {
89+
"anyOf": [
90+
{
91+
"type": ["string", "null"],
92+
"format": "date-time"
93+
}
94+
]
8795
}
88-
}
96+
},
97+
"required": ["requiredQux"]
8998
}
9099
}
91100
}

packages/openapi-ts/src/plugins/@hey-api/transformers/plugin.ts

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -345,19 +345,29 @@ const processSchemaType = ({
345345
plugin,
346346
schema: property,
347347
});
348-
if (propertyNodes.length) {
349-
if (required.includes(name)) {
350-
nodes = nodes.concat(propertyNodes);
351-
} else {
352-
nodes.push(
353-
compiler.ifStatement({
354-
expression: propertyAccessExpression,
355-
thenStatement: compiler.block({
356-
statements: ensureStatements(propertyNodes),
357-
}),
348+
if (!propertyNodes.length) {
349+
continue;
350+
}
351+
const noNullableTypesInSchema = !property.items?.find(
352+
(x) => x.type === 'null',
353+
);
354+
const requiredField = required.includes(name);
355+
// Cannot fully rely on required fields
356+
// Such value has to be present, but it doesn't guarantee that this value is not nullish
357+
if (requiredField && noNullableTypesInSchema) {
358+
nodes = nodes.concat(propertyNodes);
359+
} else {
360+
nodes.push(
361+
// todo: Probably, it would make more sense to go with if(x !== undefined && x !== null) instead of if(x)
362+
// this place influences all underlying transformers, while it's not exactly transformer itself
363+
// Keep in mind that !!0 === false, so it already makes output for Bigint undesirable
364+
compiler.ifStatement({
365+
expression: propertyAccessExpression,
366+
thenStatement: compiler.block({
367+
statements: ensureStatements(propertyNodes),
358368
}),
359-
);
360-
}
369+
}),
370+
);
361371
}
362372
}
363373

0 commit comments

Comments
 (0)