mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-12-21 00:57:15 +00:00
[aspnetcore] Add TypeConverter for enum string conversion (#3557)
* Add TypeConverter for enum
This commit is contained in:
committed by
Jim Schubert
parent
42d6420400
commit
22d022b2d5
@@ -327,6 +327,7 @@ public class AspNetCoreServerCodegen extends AbstractCSharpCodegen {
|
||||
supportingFiles.add(new SupportingFile("Solution.mustache", "", packageName + ".sln"));
|
||||
supportingFiles.add(new SupportingFile("gitignore", packageFolder, ".gitignore"));
|
||||
supportingFiles.add(new SupportingFile("validateModel.mustache", packageFolder + File.separator + "Attributes", "ValidateModelStateAttribute.cs"));
|
||||
supportingFiles.add(new SupportingFile("typeConverter.mustache", packageFolder + File.separator + "Converters", "CustomEnumConverter.cs"));
|
||||
supportingFiles.add(new SupportingFile("Project.csproj.mustache", packageFolder, packageName + ".csproj"));
|
||||
if (!isLibrary) {
|
||||
supportingFiles.add(new SupportingFile("Dockerfile.mustache", packageFolder, "Dockerfile"));
|
||||
|
||||
@@ -5,7 +5,8 @@
|
||||
{{#description}}
|
||||
/// <value>{{{description}}}</value>
|
||||
{{/description}}
|
||||
{{#allowableValues}}{{#enumVars}}{{#-first}}{{#isString}}[JsonConverter(typeof(Newtonsoft.Json.Converters.StringEnumConverter))]{{/isString}}{{/-first}}{{/enumVars}}{{/allowableValues}}
|
||||
{{#allowableValues}}{{#enumVars}}{{#-first}}{{#isString}}[TypeConverter(typeof(CustomEnumConverter<{{#datatypeWithEnum}}{{.}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{classname}}{{/datatypeWithEnum}}>))]
|
||||
[JsonConverter(typeof(Newtonsoft.Json.Converters.StringEnumConverter))]{{/isString}}{{/-first}}{{/enumVars}}{{/allowableValues}}
|
||||
public enum {{#datatypeWithEnum}}{{.}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{classname}}{{/datatypeWithEnum}}
|
||||
{
|
||||
{{#allowableValues}}{{#enumVars}}
|
||||
|
||||
@@ -3,9 +3,11 @@ using System;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Runtime.Serialization;
|
||||
using Newtonsoft.Json;
|
||||
using {{packageName}}.Converters;
|
||||
|
||||
{{#models}}
|
||||
{{#model}}
|
||||
|
||||
33
modules/openapi-generator/src/main/resources/aspnetcore/2.1/typeConverter.mustache
vendored
Normal file
33
modules/openapi-generator/src/main/resources/aspnetcore/2.1/typeConverter.mustache
vendored
Normal file
@@ -0,0 +1,33 @@
|
||||
using System;
|
||||
using System.ComponentModel;
|
||||
using System.Globalization;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace {{packageName}}.Converters
|
||||
{
|
||||
/// <summary>
|
||||
/// Custom string to enum converter
|
||||
/// </summary>
|
||||
public class CustomEnumConverter<T> : TypeConverter
|
||||
{
|
||||
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
|
||||
{
|
||||
if (sourceType == typeof(string))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
return base.CanConvertFrom(context, sourceType);
|
||||
}
|
||||
|
||||
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
|
||||
{
|
||||
var s = value as string;
|
||||
if (string.IsNullOrEmpty(s))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return JsonConvert.DeserializeObject<T>(@"""" + value.ToString() + @"""");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user