diff --git a/src/NJsonSchema.CodeGeneration.TypeScript.Tests/TypeScriptDiscriminatorTests.cs b/src/NJsonSchema.CodeGeneration.TypeScript.Tests/TypeScriptDiscriminatorTests.cs index 8bcf385b8..d3406b7d5 100644 --- a/src/NJsonSchema.CodeGeneration.TypeScript.Tests/TypeScriptDiscriminatorTests.cs +++ b/src/NJsonSchema.CodeGeneration.TypeScript.Tests/TypeScriptDiscriminatorTests.cs @@ -110,7 +110,7 @@ public async Task When_parameter_is_abstract_then_generate_union_interface() Assert.Contains("export interface OneChild extends Base", code); Assert.Contains("export interface SecondChild extends Base", code); Assert.Contains("Child: OneChild | SecondChild;", code); - Assert.Contains("Children: OneChild[] | SecondChild[];", code); + Assert.Contains("Children: (OneChild | SecondChild)[];", code); } [Fact] @@ -133,7 +133,7 @@ public async Task When_parameter_is_abstract_then_generate_union_class() Assert.Contains("export class OneChild extends Base", code); Assert.Contains("export class SecondChild extends Base", code); Assert.Contains("child: OneChild | SecondChild;", code); - Assert.Contains("children: OneChild[] | SecondChild[];", code); + Assert.Contains("children: (OneChild | SecondChild)[];", code); } } } diff --git a/src/NJsonSchema.CodeGeneration.TypeScript/TypeScriptTypeResolver.cs b/src/NJsonSchema.CodeGeneration.TypeScript/TypeScriptTypeResolver.cs index 51a7390f0..bf74a0ef7 100644 --- a/src/NJsonSchema.CodeGeneration.TypeScript/TypeScriptTypeResolver.cs +++ b/src/NJsonSchema.CodeGeneration.TypeScript/TypeScriptTypeResolver.cs @@ -304,10 +304,20 @@ private string ResolveArrayOrTuple(JsonSchema schema, string typeNameHint, bool if (Settings.UseLeafType) { - return string.Join(UnionPipe, - Resolve(schema.Item, true, typeNameHint) // TODO: Make typeNameHint singular if possible - .Split(new[] { UnionPipe }, StringSplitOptions.RemoveEmptyEntries) - .Select(x => string.Format("{0}[]", GetNullableItemType(schema, prefix + x)))); + var itemTypes = Resolve(schema.Item, true, typeNameHint) // TODO: Make typeNameHint singular if possible + .Split(new[] { UnionPipe }, StringSplitOptions.RemoveEmptyEntries) + .Select(x => GetNullableItemType(schema, prefix + x)) + .ToList(); + + var itemType = string.Join(UnionPipe, itemTypes); + + // is TypeUnion + if (itemTypes.Count > 1) + { + itemType = string.Format("({0})", itemType); + } + + return string.Format("{0}[]", itemType); } else {