Skip to content

Commit 972df34

Browse files
committed
Add TypeConverter for enum
1 parent 5ab1c9c commit 972df34

File tree

4 files changed

+38
-1
lines changed

4 files changed

+38
-1
lines changed

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AspNetCoreServerCodegen.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,7 @@ public void processOpts() {
327327
supportingFiles.add(new SupportingFile("Solution.mustache", "", packageName + ".sln"));
328328
supportingFiles.add(new SupportingFile("gitignore", packageFolder, ".gitignore"));
329329
supportingFiles.add(new SupportingFile("validateModel.mustache", packageFolder + File.separator + "Attributes", "ValidateModelStateAttribute.cs"));
330+
supportingFiles.add(new SupportingFile("typeConverter.mustache", packageFolder + File.separator + "Converters", "CustomEnumConverter.cs"));
330331
supportingFiles.add(new SupportingFile("Project.csproj.mustache", packageFolder, packageName + ".csproj"));
331332
if (!isLibrary) {
332333
supportingFiles.add(new SupportingFile("Dockerfile.mustache", packageFolder, "Dockerfile"));

modules/openapi-generator/src/main/resources/aspnetcore/2.1/enumClass.mustache

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
{{#description}}
66
/// <value>{{{description}}}</value>
77
{{/description}}
8-
{{#allowableValues}}{{#enumVars}}{{#-first}}{{#isString}}[JsonConverter(typeof(Newtonsoft.Json.Converters.StringEnumConverter))]{{/isString}}{{/-first}}{{/enumVars}}{{/allowableValues}}
8+
{{#allowableValues}}{{#enumVars}}{{#-first}}{{#isString}}[TypeConverter(typeof(CustomEnumConverter<{{#datatypeWithEnum}}{{.}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{classname}}{{/datatypeWithEnum}}>))]
9+
[JsonConverter(typeof(Newtonsoft.Json.Converters.StringEnumConverter))]{{/isString}}{{/-first}}{{/enumVars}}{{/allowableValues}}
910
public enum {{#datatypeWithEnum}}{{.}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{classname}}{{/datatypeWithEnum}}
1011
{
1112
{{#allowableValues}}{{#enumVars}}

modules/openapi-generator/src/main/resources/aspnetcore/2.1/model.mustache

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ using System;
33
using System.Linq;
44
using System.Text;
55
using System.Collections.Generic;
6+
using System.ComponentModel;
67
using System.ComponentModel.DataAnnotations;
78
using System.Runtime.Serialization;
89
using Newtonsoft.Json;
10+
using {{packageName}}.Converters;
911

1012
{{#models}}
1113
{{#model}}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System;
2+
using System.ComponentModel;
3+
using System.Globalization;
4+
using Newtonsoft.Json;
5+
6+
namespace {{packageName}}.Converters
7+
{
8+
/// <summary>
9+
/// Custom string to enum converter
10+
/// </summary>
11+
public class CustomEnumConverter<T> : TypeConverter
12+
{
13+
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
14+
{
15+
if (sourceType == typeof(string))
16+
{
17+
return true;
18+
}
19+
return base.CanConvertFrom(context, sourceType);
20+
}
21+
22+
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
23+
{
24+
var s = value as string;
25+
if (string.IsNullOrEmpty(s))
26+
{
27+
return null;
28+
}
29+
30+
return JsonConvert.DeserializeObject<T>(@"""" + value.ToString() + @"""");
31+
}
32+
}
33+
}

0 commit comments

Comments
 (0)