Skip to content

Commit

Permalink
Arrays and dictionaries in class DTOs now default to the correct null…
Browse files Browse the repository at this point in the history
… value in the init() method (#1360)

* Address issue #1359 - Typescript code generation incorrect for classes with nullable arrays and NullValue = TypeScriptNullValue.Null

* Address issue #1359 - Fix formatting

* #1359 - Add unit tests

* #1359 - Only add the else condition if nullValue is not undefined

* Update ConvertToClass.liquid

Co-authored-by: Daniel West <sqiddster@gmail.com>
Co-authored-by: Rico Suter <mail@rsuter.com>
  • Loading branch information
3 people authored May 7, 2021
1 parent 94997ea commit e11596a
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using NJsonSchema.CodeGeneration.TypeScript.Tests.Models;
using System.Collections.Generic;
using NJsonSchema.CodeGeneration.TypeScript.Tests.Models;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Xunit;

Expand Down Expand Up @@ -353,5 +355,79 @@ public async Task When_default_is_generated_then_no_liquid_error_is_in_output()
//// Assert
Assert.DoesNotContain("Liquid error: ", code);
}

[Fact]
public async Task When_a_nullable_array_property_exists_and_typestyle_is_null_then_init_should_assign_null()
{
//// Arrange
var schema = new JsonSchema
{
Properties =
{
{ "Prop", new JsonSchemaProperty
{
Type = JsonObjectType.Array,
Item = new JsonSchema
{
Type = JsonObjectType.String
},
IsRequired = true,
IsNullableRaw = true
}
},
}
};

//// Act
var generator = new TypeScriptGenerator(schema, new TypeScriptGeneratorSettings
{
NullValue = TypeScriptNullValue.Null,
TypeScriptVersion = 4,
MarkOptionalProperties = false,
});
var code = generator.GenerateFile("Foo").Replace("\r\n", "\n");

//// Assert
Assert.Matches(new Regex(
@"init\(.*\)\s{.*}\s*else\s{\s*this\.prop\s=\s<any>null;\s*}", RegexOptions.Singleline),
code);
}

[Fact]
public async Task When_a_nullable_dict_property_exists_and_typestyle_is_null_then_init_should_assign_null()
{
//// Arrange
var schema = new JsonSchema
{
Properties =
{
{ "Prop", new JsonSchemaProperty
{
Type = JsonObjectType.Object,
AdditionalPropertiesSchema = new JsonSchema
{
Properties = { }
},
IsRequired = true,
IsNullableRaw = true
}
},
}
};

//// Act
var generator = new TypeScriptGenerator(schema, new TypeScriptGeneratorSettings
{
NullValue = TypeScriptNullValue.Null,
TypeScriptVersion = 4,
MarkOptionalProperties = false,
});
var code = generator.GenerateFile("Foo").Replace("\r\n", "\n");

//// Assert
Assert.Matches(new Regex(
@"init\(.*\)\s{.*}\s*else\s{\s*this\.prop\s=\s<any>null;\s*}", RegexOptions.Singleline),
code);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ if (Array.isArray({{ Value }})) {
{% endif -%}
{% endif -%}
}
{% if NullValue != "undefined" %}else {
{{ Variable }} = <any>{{ NullValue }};
}
{% endif -%}
{% elseif IsDictionary -%}
if ({{ Value }}) {
{{ Variable }} = {} as any;
Expand All @@ -38,6 +42,10 @@ if ({{ Value }}) {
{% endif -%}
}
}
{% if NullValue != "undefined" %}else {
{{ Variable }} = <any>{{ NullValue }};
}
{% endif -%}
{% else -%}
{% if IsDate or IsDateTime -%}
{{ Variable }} = {{ Value }} ? {{ StringToDateCode }}({{ Value }}.toString()) : {% if HasDefaultValue %}{{ StringToDateCode }}({{ DefaultValue }}){% else %}<any>{{ NullValue }}{% endif %};
Expand All @@ -48,4 +56,4 @@ if ({{ Value }}) {
{{ Variable }} = {{ Value }};
{% endif -%}
{% endif -%}
{% endif -%}
{% endif -%}

0 comments on commit e11596a

Please sign in to comment.