Generating C# DTOs with custom generic class
See original GitHub issueI have a C# WebApi that exposes a class and enum like this:
public class Error<T>
{
[Required]
public T Code { get; set; }
public string Message { get; set; }
}
public enum OneTimeAmountErrorType
{
Unspecified = 0,
AccountNotFound,
InsufficientFunds
}
The error code can be a string or one of several enum types.
The generated Swagger (not done with NSwag) looks like below. Only relevant parts and one of the enums included.
"Error[String]": {
"required": ["code"],
"type": "object",
"properties": {
"code": {
"type": "string"
},
"message": {
"type": "string"
}
}
}
"Error[OneTimeAmountErrorType]": {
"required": ["code"],
"type": "object",
"properties": {
"code": {
"enum": ["Unspecified", "AccountNotFound", "InsufficientFunds"],
"type": "string"
},
"message": {
"type": "string"
}
}
}
Is there anything I can do, either with the Swagger generation, or with the NSwag client generation, to get a generic DTO like the original one? Using NSwagStudio 11.12.16.0, I get the following output (some stuff removed for brevity):
[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "9.10.19.0 (Newtonsoft.Json v9.0.0.0)")]
public partial class ErrorOfString
{
[Newtonsoft.Json.JsonProperty("code", Required = Newtonsoft.Json.Required.Always)]
[System.ComponentModel.DataAnnotations.Required]
public string Code { get; set; }
[Newtonsoft.Json.JsonProperty("message", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
public string Message { get; set; }
}
[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "9.10.19.0 (Newtonsoft.Json v9.0.0.0)")]
public partial class ErrorOfOneTimeAmountErrorType
{
[Newtonsoft.Json.JsonProperty("code", Required = Newtonsoft.Json.Required.Always)]
[System.ComponentModel.DataAnnotations.Required]
[Newtonsoft.Json.JsonConverter(typeof(Newtonsoft.Json.Converters.StringEnumConverter))]
public ErrorOfOneTimeAmountErrorTypeCode Code { get; set; }
[Newtonsoft.Json.JsonProperty("message", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
public string Message { get; set; }
}
[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "9.10.19.0 (Newtonsoft.Json v9.0.0.0)")]
public enum ErrorOfOneTimeAmountErrorTypeCode
{
[System.Runtime.Serialization.EnumMember(Value = "Unspecified")]
Unspecified = 0,
[System.Runtime.Serialization.EnumMember(Value = "AccountNotFound")]
AccountNotFound = 1,
[System.Runtime.Serialization.EnumMember(Value = "InsufficientFunds")]
InsufficientFunds = 2,
}
Having an enum named “ErrorOfOneTimeAmountErrorTypeCode” is not what I call intuitive. 😃
If it is impossible to generate a Error<T>
, an acceptable solution for me would be if I could get more control of the generated names. I.e. the enum above should be named as the original enum (I am aware that that isn’t present in the Swagger though…).
Grateful for any help!
Issue Analytics
- State:
- Created 6 years ago
- Reactions:1
- Comments:12 (3 by maintainers)
I wrote an article to show one of the possible solutions https://medium.com/@rynaret/nswag-csharp-client-with-generics-support-6ad6a09f81d6
I#ve created a tool for this problem. Its called makeGenericAgain and can be found here https://www.nuget.org/packages/MakeGenericAgain/ also source is available here https://github.com/fgilde/MakeGenericAgain