From 513c489a7fa01ae5dcba152dbd1cdde4e79cccb0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Fran=C3=A7ois=20C=C3=B4t=C3=A9?= Date: Wed, 28 Feb 2024 08:40:29 -0500 Subject: [PATCH] fix: handle null with new string renderers (#1841) --- .../__snapshots__/index.spec.ts.snap | 4 ++++ .../csharp-generate-handle-nullable/index.ts | 18 +++++++++++++++++- src/generators/csharp/Constants.ts | 14 ++++++++++++++ .../csharp/renderers/ClassRenderer.ts | 5 +++-- 4 files changed, 38 insertions(+), 3 deletions(-) diff --git a/examples/csharp-generate-handle-nullable/__snapshots__/index.spec.ts.snap b/examples/csharp-generate-handle-nullable/__snapshots__/index.spec.ts.snap index 8c8bc869bc..cd9838c69b 100644 --- a/examples/csharp-generate-handle-nullable/__snapshots__/index.spec.ts.snap +++ b/examples/csharp-generate-handle-nullable/__snapshots__/index.spec.ts.snap @@ -7,6 +7,10 @@ Array [ public string? Email { get; set; } public string Name { get; set; } = null!; public int Age { get; set; } + public System.DateTime ApplicationDate { get; set; } + public System.DateTimeOffset Timestamp { get; set; } + public System.DateTime? ApplicationDate2 { get; set; } + public System.DateTimeOffset? Timestamp2 { get; set; } public Dictionary[]? NullableFoos { get; set; } public Dictionary[] MandatoryFoos { get; set; } = null!; }", diff --git a/examples/csharp-generate-handle-nullable/index.ts b/examples/csharp-generate-handle-nullable/index.ts index ecaa345125..d98f2e656d 100644 --- a/examples/csharp-generate-handle-nullable/index.ts +++ b/examples/csharp-generate-handle-nullable/index.ts @@ -8,7 +8,7 @@ const jsonSchemaDraft7 = { $schema: 'http://json-schema.org/draft-07/schema#', type: 'object', additionalProperties: false, - required: ['name', 'age', 'mandatoryFoos'], + required: ['name', 'age', 'mandatoryFoos', 'applicationDate', 'timestamp'], properties: { email: { type: 'string', @@ -20,6 +20,22 @@ const jsonSchemaDraft7 = { age: { type: 'integer' }, + applicationDate: { + type: 'string', + format: 'date' + }, + timestamp: { + type: 'string', + format: 'date-time' + }, + applicationDate2: { + type: 'string', + format: 'date' + }, + timestamp2: { + type: 'string', + format: 'date-time' + }, nullableFoos: { type: 'array', items: { diff --git a/src/generators/csharp/Constants.ts b/src/generators/csharp/Constants.ts index 87298be0a9..32de082f35 100644 --- a/src/generators/csharp/Constants.ts +++ b/src/generators/csharp/Constants.ts @@ -97,6 +97,13 @@ export function isReservedCSharpKeyword( ); } +const STRING_RENDERING_TYPES = [ + 'System.TimeSpan', + 'System.DateTime', + 'System.DateTimeOffset', + 'System.Guid' +]; + const PRIMITIVES = [ 'bool', 'byte', @@ -112,6 +119,13 @@ const PRIMITIVES = [ 'short', 'ushort' ]; + +export function isStringRenderingType( + property: ConstrainedObjectPropertyModel +): boolean { + return STRING_RENDERING_TYPES.includes(property.property.type); +} + export function isPrimitive(property: ConstrainedObjectPropertyModel): boolean { return PRIMITIVES.includes(property.property.type); } diff --git a/src/generators/csharp/renderers/ClassRenderer.ts b/src/generators/csharp/renderers/ClassRenderer.ts index a5fc421572..c267aa86e2 100644 --- a/src/generators/csharp/renderers/ClassRenderer.ts +++ b/src/generators/csharp/renderers/ClassRenderer.ts @@ -7,7 +7,7 @@ import { import { pascalCase } from 'change-case'; import { CsharpClassPreset } from '../CSharpPreset'; import { CSharpOptions } from '../CSharpGenerator'; -import { isPrimitive, isEnum } from '../Constants'; +import { isPrimitive, isEnum, isStringRenderingType } from '../Constants'; /** * Renderer for CSharp's `struct` type @@ -106,7 +106,8 @@ export const CSHARP_DEFAULT_CLASS_PRESET: CsharpClassPreset = { options?.handleNullable && property.required && !isPrimitive(property) && - !isEnum(property) + !isEnum(property) && + !isStringRenderingType(property) ) { nullablePropertyEnding = ' = null!'; }