Skip to content
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
Expand Up @@ -186,5 +186,38 @@ public void When_array_item_is_nullable_then_generated_TypeScript_is_correct()
Assert.True(schema.Properties["Items"].Item.IsNullable(SchemaType.JsonSchema));
Assert.Contains(": (string | null)[]", output);
}

public class Complex
{
public int A { get; set; }
}

public class ClassWithComplexNullableArrayItems
{
[NotNull]
[ItemsCanBeNull]
public List<Complex> Items { get; set; }
}

[Fact]
public void When_complex_array_item_is_nullable_then_generated_TypeScript_is_nullsafe()
{
// Arrange
var schema = NewtonsoftJsonSchemaGenerator.FromType<ClassWithComplexNullableArrayItems>();
var json = schema.ToJson();
var generator = new TypeScriptGenerator(schema, new TypeScriptGeneratorSettings
{
TypeScriptVersion = 2.7m,
NullValue = TypeScriptNullValue.Null
});

// Act
var output = generator.GenerateFile("MyClass");

// Assert
Assert.True(schema.Properties["Items"].Item.IsNullable(SchemaType.JsonSchema));
Assert.Contains(": (Complex | null)[]", output);
Assert.Contains(".push(item ? item.toJSON() : <any>null)", output);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ export class MyClass implements IMyClass {
if (Array.isArray(this.array)) {
data["Array"] = [];
for (let item of this.array)
data["Array"].push(item.toJSON());
data["Array"].push(item ? item.toJSON() : <any>undefined);
}
if (this.dictionary) {
data["Dictionary"] = {};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ export class MyClass implements IMyClass {
if (Array.isArray(this.array)) {
data["Array"] = [];
for (let item of this.array)
data["Array"].push(item.toJSON());
data["Array"].push(item ? item.toJSON() : <any>undefined);
}
if (this.dictionary) {
data["Dictionary"] = {};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ export class MyClass implements IMyClass {
if (Array.isArray(this.array)) {
data["Array"] = [];
for (let item of this.array)
data["Array"].push(item.toJSON());
data["Array"].push(item ? item.toJSON() : <any>undefined);
}
if (this.dictionary) {
data["Dictionary"] = {};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ export class MyClass implements IMyClass {
if (Array.isArray(this.array)) {
data["Array"] = [];
for (let item of this.array)
data["Array"].push(item.toJSON());
data["Array"].push(item ? item.toJSON() : <any>undefined);
}
if (this.dictionary) {
data["Dictionary"] = {};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ export class MyClass {
if (Array.isArray(array_)) {
data["Array"] = [];
for (let item of array_)
data["Array"].push(item.toJSON());
data["Array"].push(item ? item.toJSON() : <any>undefined);
}

let dictionary_: any = this.dictionary();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ export class MyClass {
if (Array.isArray(array_)) {
data["Array"] = [];
for (let item of array_)
data["Array"].push(item.toJSON());
data["Array"].push(item ? item.toJSON() : <any>undefined);
}

let dictionary_: any = this.dictionary();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ export class MyClass {
if (Array.isArray(array_)) {
data["Array"] = [];
for (let item of array_)
data["Array"].push(item.toJSON());
data["Array"].push(item ? item.toJSON() : <any>undefined);
}

let dictionary_: any = this.dictionary();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ export class MyClass {
if (Array.isArray(array_)) {
data["Array"] = [];
for (let item of array_)
data["Array"].push(item.toJSON());
data["Array"].push(item ? item.toJSON() : <any>undefined);
}

let dictionary_: any = this.dictionary();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ export class MyClass implements IMyClass {
if (Array.isArray(this.children)) {
data["Children"] = [];
for (let item of this.children)
data["Children"].push(item.toJSON());
data["Children"].push(item ? item.toJSON() : <any>undefined);
}
return data;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ if (Array.isArray({{ Value }})) {
{{ Variable }} = [];
for (let item of {{ Value }})
{%- if IsArrayItemNewableObject -%}
{{ Variable }}.push(item.toJSON());
{{ Variable }}.push(item ? item.toJSON() : <any>{{ NullValue }});
{%- elsif IsArrayItemDate -%}
{{ Variable }}.push({% if UseJsDate %}formatDate(item){% else %}item.{{ DateToStringCode }}{% endif %});
{%- elsif IsArrayItemDateTime -%}
Expand Down