Skip to content

Commit 181eccb

Browse files
Merge pull request #98 from atc-net/bug/49-nullable-properties-tostring
The ToString on models now print the count of collections
2 parents c7c65dd + bf7cb39 commit 181eccb

File tree

4 files changed

+58
-2
lines changed

4 files changed

+58
-2
lines changed

src/Atc.Rest.ApiGenerator/ProjectSyntaxFactories/SyntaxMethodDeclarationFactory.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,17 @@ public static MethodDeclarationSyntax CreateInterfaceMethod(string parameterType
6565
foreach (var schema in apiSchemaProperties)
6666
{
6767
var name = schema.Key.EnsureFirstCharacterToUpper();
68+
var isArray = schema.Value.Type == OpenApiDataTypeConstants.Array;
6869
var hasAnyProperties = schema.Value.HasAnyProperties();
6970

70-
if (!hasAnyProperties)
71+
if (isArray)
72+
{
73+
content.Add(SyntaxInterpolatedFactory.CreateNameOf(name));
74+
content.Add(SyntaxInterpolatedFactory.StringText(".Count: "));
75+
var countCoalesceExpression = SyntaxFactory.ParseExpression($"{name}?.Count ?? 0");
76+
content.Add(SyntaxFactory.Interpolation(countCoalesceExpression));
77+
}
78+
else if (!hasAnyProperties)
7179
{
7280
content.Add(SyntaxInterpolatedFactory.CreateNameOf(name));
7381
content.Add(SyntaxInterpolatedFactory.StringTextColon());
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System.CodeDom.Compiler;
2+
using System.Collections.Generic;
3+
4+
//------------------------------------------------------------------------------
5+
// This code was auto-generated by ApiGenerator x.x.x.x.
6+
//
7+
// Changes to this file may cause incorrect behavior and will be lost if
8+
// the code is regenerated.
9+
//------------------------------------------------------------------------------
10+
namespace TestProject.AtcTest.Contracts.Test
11+
{
12+
/// <summary>
13+
/// Test component description.
14+
/// </summary>
15+
[GeneratedCode("ApiGenerator", "x.x.x.x")]
16+
public class TestComponent
17+
{
18+
public List<string> Foos { get; set; } = new List<string>();
19+
20+
public List<string> NullableFoos { get; set; } = new List<string>();
21+
22+
/// <summary>
23+
/// Converts to string.
24+
/// </summary>
25+
public override string ToString()
26+
{
27+
return $"{nameof(Foos)}.Count: {Foos?.Count ?? 0}, {nameof(NullableFoos)}.Count: {NullableFoos?.Count ?? 0}";
28+
}
29+
}
30+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
openapi: 3.0.0
2+
components:
3+
schemas:
4+
TestComponent:
5+
title: TestComponent
6+
description: 'Test component description'
7+
type: object
8+
properties:
9+
foos:
10+
type: array
11+
required: true
12+
items:
13+
type: string
14+
nullableFoos:
15+
type: array
16+
nullable: true
17+
items:
18+
type: string

test/Atc.Rest.ApiGenerator.Tests/SyntaxGenerators/Api/XUnitTestData/ContractModel/requiredArrayComponent.verified.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public class RequiredArrayComponent
2424
/// </summary>
2525
public override string ToString()
2626
{
27-
return $"{nameof(MyItems)}: {MyItems}";
27+
return $"{nameof(MyItems)}.Count: {MyItems?.Count ?? 0}";
2828
}
2929
}
3030
}

0 commit comments

Comments
 (0)