Skip to content

Commit ea8f718

Browse files
committed
Fixing widespread case of nullable dates in tranformers
1 parent 3f39137 commit ea8f718

File tree

1 file changed

+27
-3
lines changed
  • packages/openapi-ts/src/plugins/@hey-api/transformers

1 file changed

+27
-3
lines changed

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

+27-3
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,11 @@ const bigIntExpressions = ({
5757

5858
const dateExpressions = ({
5959
dataExpression,
60+
completeFieldSchema,
6061
}: {
6162
dataExpression?: ts.Expression | string;
62-
}): Array<ts.Expression> => {
63+
completeFieldSchema: IR.SchemaObject[],
64+
}): Array<ts.Expression | ts.Statement> => {
6365
const identifierDate = compiler.identifier({ text: 'Date' });
6466

6567
if (typeof dataExpression === 'string') {
@@ -72,6 +74,25 @@ const dateExpressions = ({
7274
}
7375

7476
if (dataExpression) {
77+
78+
if (completeFieldSchema[1]?.type === 'null') {
79+
const ifStatement = ts.factory.createIfStatement(
80+
ts.factory.createBinaryExpression(
81+
dataExpression,
82+
ts.SyntaxKind.EqualsEqualsEqualsToken,
83+
ts.factory.createNull()
84+
),
85+
ts.factory.createExpressionStatement(
86+
ts.factory.createNewExpression(
87+
identifierDate,
88+
undefined,
89+
[dataExpression]
90+
)
91+
),
92+
);
93+
return [ifStatement];
94+
}
95+
7596
return [
7697
compiler.assignment({
7798
left: dataExpression,
@@ -186,11 +207,13 @@ const processSchemaType = ({
186207
dataExpression,
187208
plugin,
188209
schema,
210+
completeFieldSchema,
189211
}: {
190212
context: IR.Context;
191213
dataExpression?: ts.Expression | string;
192214
plugin: Plugin.Instance<Config>;
193215
schema: IR.SchemaObject;
216+
completeFieldSchema?: IR.SchemaObject[];
194217
}): Array<ts.Expression | ts.Statement> => {
195218
const file = context.file({ id: transformersId })!;
196219

@@ -369,7 +392,7 @@ const processSchemaType = ({
369392
schema.type === 'string' &&
370393
(schema.format === 'date' || schema.format === 'date-time')
371394
) {
372-
return dateExpressions({ dataExpression });
395+
return dateExpressions({ dataExpression, completeFieldSchema: completeFieldSchema ?? [] });
373396
}
374397

375398
if (plugin.bigInt && schema.type === 'integer' && schema.format === 'int64') {
@@ -382,7 +405,7 @@ const processSchemaType = ({
382405
context,
383406
dataExpression: 'item',
384407
plugin,
385-
schema: schema.items[0]!,
408+
schema: schema.items[0]!
386409
});
387410
}
388411

@@ -401,6 +424,7 @@ const processSchemaType = ({
401424
dataExpression: dataExpression || 'item',
402425
plugin,
403426
schema: item,
427+
completeFieldSchema: [...schema.items],
404428
});
405429
if (nodes.length) {
406430
if (dataExpression) {

0 commit comments

Comments
 (0)