Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Arrays and dictionaries in class DTOs now default to the correct null value in the init() method #1360

Merged
merged 5 commits into from
May 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 -%}