fork of hey-api/openapi-ts because I need some additional things
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Merge pull request #1917 from Freddis/fixing_nullable_dates_in_transformers

authored by

Lubos and committed by
GitHub
2ab288f2 c8eb3d6e

+56 -14
+5
.changeset/red-days-camp.md
··· 1 + --- 2 + "@hey-api/openapi-ts": patch 3 + --- 4 + 5 + fix: handle nullable dates in transformers
+3
packages/openapi-ts-tests/test/__snapshots__/3.0.x/transformers-any-of-null/transformers.gen.ts
··· 9 9 if (data.bar) { 10 10 data.bar = new Date(data.bar); 11 11 } 12 + if (data.requiredBaz) { 13 + data.requiredBaz = new Date(data.requiredBaz); 14 + } 12 15 return data; 13 16 }; 14 17
+1
packages/openapi-ts-tests/test/__snapshots__/3.0.x/transformers-any-of-null/types.gen.ts
··· 3 3 export type Foo = { 4 4 foo?: Date; 5 5 bar?: Date | null; 6 + requiredBaz: Date | null; 6 7 }; 7 8 8 9 export type GetFooData = {
+3
packages/openapi-ts-tests/test/__snapshots__/3.1.x/transformers-any-of-null/transformers.gen.ts
··· 12 12 if (data.baz) { 13 13 data.baz = new Date(data.baz); 14 14 } 15 + if (data.requiredQux) { 16 + data.requiredQux = new Date(data.requiredQux); 17 + } 15 18 return data; 16 19 }; 17 20
+1
packages/openapi-ts-tests/test/__snapshots__/3.1.x/transformers-any-of-null/types.gen.ts
··· 13 13 foo?: Date; 14 14 bar?: Date | null; 15 15 baz?: Date | null; 16 + requiredQux: Date | null; 16 17 }; 17 18 18 19 export type GetFooData = {
+11 -1
packages/openapi-ts-tests/test/spec/3.0.x/transformers-any-of-null.json
··· 42 42 "format": "date-time" 43 43 } 44 44 ] 45 + }, 46 + "requiredBaz": { 47 + "anyOf": [ 48 + { 49 + "type": "string", 50 + "nullable": true, 51 + "format": "date-time" 52 + } 53 + ] 45 54 } 46 - } 55 + }, 56 + "required": ["requiredBaz"] 47 57 } 48 58 } 49 59 }
+10 -1
packages/openapi-ts-tests/test/spec/3.1.x/transformers-any-of-null.json
··· 84 84 "format": "date-time" 85 85 } 86 86 ] 87 + }, 88 + "requiredQux": { 89 + "anyOf": [ 90 + { 91 + "type": ["string", "null"], 92 + "format": "date-time" 93 + } 94 + ] 87 95 } 88 - } 96 + }, 97 + "required": ["requiredQux"] 89 98 } 90 99 } 91 100 }
+22 -12
packages/openapi-ts/src/plugins/@hey-api/transformers/plugin.ts
··· 345 345 plugin, 346 346 schema: property, 347 347 }); 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), 358 368 }), 359 - ); 360 - } 369 + }), 370 + ); 361 371 } 362 372 } 363 373