question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Generating C# DTOs with custom generic class

See original GitHub issue

I 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:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:12 (3 by maintainers)

github_iconTop GitHub Comments

5reactions
Rynaretcommented, Jun 14, 2020

I wrote an article to show one of the possible solutions https://medium.com/@rynaret/nswag-csharp-client-with-generics-support-6ad6a09f81d6

3reactions
fgildecommented, Oct 10, 2021

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Creating Generic templates from DTOs c# - Stack Overflow
I have a class that has multiple classes being set. I want to run [something] against the class file and generate my own...
Read more >
Using C# Source Generators to Generate Data ... - Artineering
emit code to create the DTO class with property getters and setters ... generic collection type with custom type args.
Read more >
Using C# Source Generators to Generate Data ... - Artineering
For e.g. generate DTOs for all entity classes decorated with a certain ... need converting to DTO, is decorate my domain types with...
Read more >
Implementing a Generic Data Transfer Object in C# ...
Implementing a generic Data Transfer Object. We basically have an interface called IDTO, a DTO class and a DTOStore class in this design....
Read more >
Have generic methods that transfer DTO's from different ...
Just write mapper for DTO to new model . It will better solution to ignore duplicated methods. If you also don't want to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found