Skip to content

Commit

Permalink
Improve LeafType generated code (#1382)
Browse files Browse the repository at this point in the history
  • Loading branch information
trejjam authored Jul 1, 2021
1 parent 6342154 commit 82a3e89
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down

0 comments on commit 82a3e89

Please sign in to comment.