Skip to content

Commit

Permalink
fix: handle null with new string renderers (#1841)
Browse files Browse the repository at this point in the history
  • Loading branch information
JFCote committed Feb 28, 2024
1 parent 0d2f4df commit 513c489
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, dynamic>[]? NullableFoos { get; set; }
public Dictionary<string, dynamic>[] MandatoryFoos { get; set; } = null!;
}",
Expand Down
18 changes: 17 additions & 1 deletion examples/csharp-generate-handle-nullable/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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: {
Expand Down
14 changes: 14 additions & 0 deletions src/generators/csharp/Constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@ export function isReservedCSharpKeyword(
);
}

const STRING_RENDERING_TYPES = [
'System.TimeSpan',
'System.DateTime',
'System.DateTimeOffset',
'System.Guid'
];

const PRIMITIVES = [
'bool',
'byte',
Expand All @@ -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);
}
Expand Down
5 changes: 3 additions & 2 deletions src/generators/csharp/renderers/ClassRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -106,7 +106,8 @@ export const CSHARP_DEFAULT_CLASS_PRESET: CsharpClassPreset<CSharpOptions> = {
options?.handleNullable &&
property.required &&
!isPrimitive(property) &&
!isEnum(property)
!isEnum(property) &&
!isStringRenderingType(property)
) {
nullablePropertyEnding = ' = null!';
}
Expand Down

0 comments on commit 513c489

Please sign in to comment.