ToString bug -> should be a count, if the property is a List<x>
Closed this issue · 1 comments
davidkallesen commented
Ensure generated ToString method that contains ref to a property of List<#> should be generated with ".Count" and have null check with "?" infront.
Example with 3 properties and the ToString-method:
public class MyClass
{
public string Foo { get; set; }
public List<string> Bar { get; set; }
public List<string>? Baz { get; set; }
public override string ToString()
{
return $"{nameof(Foo)}: {Foo}, {nameof(Bar)}.Count: {Bar.Count}, {nameof(Baz)}.Count: {Baz?.Count}";
}
}
Note: Also look at the project UseNull
settings to generate the correct ToString.
egil commented
This yaml:
openapi: 3.0.0
components:
schemas:
TestComponent:
title: TestComponent
description: 'Test component description'
type: object
properties:
foo:
type: string
required: true
nullableFoo:
type: string
nullable: true
foos:
type: array
required: true
items:
type: string
nullableFoos:
type: array
nullable: true
items:
type: string
generates this c#:
using System.CodeDom.Compiler;
using System.Collections.Generic;
//------------------------------------------------------------------------------
// This code was auto-generated by ApiGenerator x.x.x.x.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
//------------------------------------------------------------------------------
namespace TestProject.AtcTest.Contracts.Test
{
/// <summary>
/// Test component description.
/// </summary>
[GeneratedCode("ApiGenerator", "x.x.x.x")]
public class TestComponent
{
public string Foo { get; set; }
public string? NullableFoo { get; set; }
public List<string> Foos { get; set; } = new List<string>();
public List<string> NullableFoos { get; set; } = new List<string>();
/// <summary>
/// Converts to string.
/// </summary>
public override string ToString()
{
return $"{nameof(Foo)}: {Foo}, {nameof(NullableFoo)}: {NullableFoo}, {nameof(Foos)}: {Foos}, {nameof(NullableFoos)}: {NullableFoos}";
}
}
}
The expected output should instead be:
using System.CodeDom.Compiler;
using System.Collections.Generic;
//------------------------------------------------------------------------------
// This code was auto-generated by ApiGenerator x.x.x.x.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
//------------------------------------------------------------------------------
namespace TestProject.AtcTest.Contracts.Test
{
/// <summary>
/// Test component description.
/// </summary>
[GeneratedCode("ApiGenerator", "x.x.x.x")]
public class TestComponent
{
public string Foo { get; set; }
public string? NullableFoo { get; set; }
public List<string> Foos { get; set; } = new List<string>();
public List<string>? NullableFoos { get; set; } = new List<string>();
/// <summary>
/// Converts to string.
/// </summary>
public override string ToString()
{
return $"{nameof(Foo)}: {Foo}, {nameof(NullableFoo)}: {NullableFoo}, {nameof(Foos)}: {Foos}, {nameof(NullableFoos)}: {NullableFoos?.Count ?? 0}";
}
}
}
Is this the correct @davidkallesen?