forked from loafle/openapi-generator-original
better enum support for csharp
This commit is contained in:
parent
217d93401b
commit
45f3cfd5cf
@ -262,6 +262,7 @@ public class DefaultCodegen {
|
||||
*/
|
||||
public String toEnumVarName(String value) {
|
||||
String var = value.replaceAll("\\W+", "_").toUpperCase();
|
||||
LOGGER.info("toEnumVarName: " + value + " => " + var);
|
||||
if (var.matches("\\d.*")) {
|
||||
return "_" + var;
|
||||
} else {
|
||||
|
@ -203,11 +203,6 @@ public abstract class AbstractCSharpCodegen extends DefaultCodegen implements Co
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toEnumName(CodegenProperty property) {
|
||||
return StringUtils.capitalize(property.name) + "Enum?";
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> postProcessModels(Map<String, Object> objs) {
|
||||
List<Object> models = (List<Object>) objs.get("models");
|
||||
@ -223,7 +218,8 @@ public abstract class AbstractCSharpCodegen extends DefaultCodegen implements Co
|
||||
}
|
||||
}
|
||||
}
|
||||
return objs;
|
||||
// process enum in models
|
||||
return postProcessModelsEnum(objs);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -544,4 +540,54 @@ public abstract class AbstractCSharpCodegen extends DefaultCodegen implements Co
|
||||
public void setSourceFolder(String sourceFolder) {
|
||||
this.sourceFolder = sourceFolder;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String toEnumVarName(String name) {
|
||||
String enumName = sanitizeName(name);
|
||||
|
||||
enumName = enumName.replaceFirst("^_", "");
|
||||
enumName = enumName.replaceFirst("_$", "");
|
||||
|
||||
enumName = camelize(enumName) + "Enum";
|
||||
|
||||
LOGGER.info("toEnumVarName = " + enumName);
|
||||
|
||||
if (enumName.matches("\\d.*")) { // starts with number
|
||||
return "_" + enumName;
|
||||
} else {
|
||||
return enumName;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toEnumName(CodegenProperty property) {
|
||||
return sanitizeName(camelize(property.name)) + "Enum";
|
||||
}
|
||||
|
||||
/*
|
||||
@Override
|
||||
public String toEnumName(CodegenProperty property) {
|
||||
String enumName = sanitizeName(property.name);
|
||||
if (!StringUtils.isEmpty(modelNamePrefix)) {
|
||||
enumName = modelNamePrefix + "_" + enumName;
|
||||
}
|
||||
|
||||
if (!StringUtils.isEmpty(modelNameSuffix)) {
|
||||
enumName = enumName + "_" + modelNameSuffix;
|
||||
}
|
||||
|
||||
// model name cannot use reserved keyword, e.g. return
|
||||
if (isReservedWord(enumName)) {
|
||||
LOGGER.warn(enumName + " (reserved word) cannot be used as model name. Renamed to " + camelize("model_" + enumName));
|
||||
enumName = "model_" + enumName; // e.g. return => ModelReturn (after camelize)
|
||||
}
|
||||
|
||||
if (enumName.matches("\\d.*")) { // starts with number
|
||||
return "_" + enumName;
|
||||
} else {
|
||||
return enumName;
|
||||
}
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
@ -306,11 +306,14 @@ public class CSharpClientCodegen extends AbstractCSharpCodegen {
|
||||
this.packageGuid = packageGuid;
|
||||
}
|
||||
|
||||
/*
|
||||
*
|
||||
*/
|
||||
@Override
|
||||
public Map<String, Object> postProcessModels(Map<String, Object> objMap) {
|
||||
Map<String, Object> objs = super.postProcessModels(objMap);
|
||||
|
||||
List<Object> models = (List<Object>) objs.get("models");
|
||||
objMap = super.postProcessModels(objMap);
|
||||
|
||||
List<Object> models = (List<Object>) objMap.get("models");
|
||||
for (Object _mo : models) {
|
||||
Map<String, Object> mo = (Map<String, Object>) _mo;
|
||||
CodegenModel cm = (CodegenModel) mo.get("model");
|
||||
@ -325,11 +328,13 @@ public class CSharpClientCodegen extends AbstractCSharpCodegen {
|
||||
if (allowableValues == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
List<String> values = (List<String>) allowableValues.get("values");
|
||||
|
||||
if (values == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
// put "enumVars" map into `allowableValues", including `name` and `value`
|
||||
List<Map<String, String>> enumVars = new ArrayList<Map<String, String>>();
|
||||
String commonPrefix = findCommonPrefixOfVars(values);
|
||||
@ -351,6 +356,7 @@ public class CSharpClientCodegen extends AbstractCSharpCodegen {
|
||||
enumVars.add(enumVar);
|
||||
}
|
||||
allowableValues.put("enumVars", enumVars);
|
||||
|
||||
// handle default value for enum, e.g. available => StatusEnum.AVAILABLE
|
||||
|
||||
// HACK: strip ? from enum
|
||||
@ -362,21 +368,21 @@ public class CSharpClientCodegen extends AbstractCSharpCodegen {
|
||||
String enumName = null;
|
||||
|
||||
for (Map<String, String> enumVar : enumVars) {
|
||||
|
||||
if (var.defaultValue.replace("\"", "").equals(enumVar.get("value"))) {
|
||||
enumName = enumVar.get("name");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (enumName != null && var.vendorExtensions.containsKey(DATA_TYPE_WITH_ENUM_EXTENSION)) {
|
||||
var.defaultValue = var.vendorExtensions.get(DATA_TYPE_WITH_ENUM_EXTENSION) + "." + enumName;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
return objs;
|
||||
return super.postProcessModels(objMap);
|
||||
//return objs;
|
||||
}
|
||||
|
||||
public void setTargetFramework(String dotnetFramework) {
|
||||
|
@ -1,6 +1,6 @@
|
||||
public enum {{vendorExtensions.plainDatatypeWithEnum}} {
|
||||
public enum {{#datatypeWithEnum}}{{.}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{classname}}{{/datatypeWithEnum}} {
|
||||
{{#allowableValues}}{{#enumVars}}
|
||||
[EnumMember(Value = "{{jsonname}}")]
|
||||
[EnumMember(Value = {{{value}}})]
|
||||
{{name}}{{^-last}},
|
||||
{{/-last}}{{#-last}}{{/-last}}{{/enumVars}}{{/allowableValues}}
|
||||
}
|
||||
|
@ -5,6 +5,7 @@ using System.Text;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.ComponentModel;
|
||||
using System.Runtime.Serialization;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Converters;
|
||||
@ -113,6 +114,6 @@ namespace IO.Swagger.Model
|
||||
return hash;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -5,6 +5,7 @@ using System.Text;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.ComponentModel;
|
||||
using System.Runtime.Serialization;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Converters;
|
||||
@ -129,6 +130,6 @@ namespace IO.Swagger.Model
|
||||
return hash;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -5,6 +5,7 @@ using System.Text;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.ComponentModel;
|
||||
using System.Runtime.Serialization;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Converters;
|
||||
@ -121,6 +122,6 @@ namespace IO.Swagger.Model
|
||||
return hash;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -5,6 +5,7 @@ using System.Text;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.ComponentModel;
|
||||
using System.Runtime.Serialization;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Converters;
|
||||
@ -129,6 +130,6 @@ namespace IO.Swagger.Model
|
||||
return hash;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,27 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.ComponentModel;
|
||||
using System.Runtime.Serialization;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Converters;
|
||||
|
||||
namespace IO.Swagger.Model
|
||||
{
|
||||
public enum EnumClass {
|
||||
|
||||
[EnumMember(Value = "_abc")]
|
||||
Abc,
|
||||
|
||||
[EnumMember(Value = "-efg")]
|
||||
efg,
|
||||
|
||||
[EnumMember(Value = "(xyz)")]
|
||||
xyz
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,162 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.ComponentModel;
|
||||
using System.Runtime.Serialization;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Converters;
|
||||
|
||||
namespace IO.Swagger.Model
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
[DataContract]
|
||||
public partial class EnumTest : IEquatable<EnumTest>
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// Gets or Sets EnumString
|
||||
/// </summary>
|
||||
[JsonConverter(typeof(StringEnumConverter))]
|
||||
public enum EnumStringEnum {
|
||||
|
||||
[EnumMember(Value = "UPPER")]
|
||||
Upper,
|
||||
|
||||
[EnumMember(Value = "lower")]
|
||||
Lower
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Gets or Sets EnumString
|
||||
/// </summary>
|
||||
[DataMember(Name="enum_string", EmitDefaultValue=false)]
|
||||
public EnumStringEnum? EnumString { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="EnumTest" /> class.
|
||||
/// Initializes a new instance of the <see cref="EnumTest" />class.
|
||||
/// </summary>
|
||||
/// <param name="EnumString">EnumString.</param>
|
||||
/// <param name="EnumInteger">EnumInteger.</param>
|
||||
/// <param name="EnumNumber">EnumNumber.</param>
|
||||
|
||||
public EnumTest(EnumStringEnum? EnumString = null, int? EnumInteger = null, double? EnumNumber = null)
|
||||
{
|
||||
this.EnumString = EnumString;
|
||||
this.EnumInteger = EnumInteger;
|
||||
this.EnumNumber = EnumNumber;
|
||||
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Gets or Sets EnumInteger
|
||||
/// </summary>
|
||||
[DataMember(Name="enum_integer", EmitDefaultValue=false)]
|
||||
public int? EnumInteger { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or Sets EnumNumber
|
||||
/// </summary>
|
||||
[DataMember(Name="enum_number", EmitDefaultValue=false)]
|
||||
public double? EnumNumber { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Returns the string presentation of the object
|
||||
/// </summary>
|
||||
/// <returns>String presentation of the object</returns>
|
||||
public override string ToString()
|
||||
{
|
||||
var sb = new StringBuilder();
|
||||
sb.Append("class EnumTest {\n");
|
||||
sb.Append(" EnumString: ").Append(EnumString).Append("\n");
|
||||
sb.Append(" EnumInteger: ").Append(EnumInteger).Append("\n");
|
||||
sb.Append(" EnumNumber: ").Append(EnumNumber).Append("\n");
|
||||
|
||||
sb.Append("}\n");
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the JSON string presentation of the object
|
||||
/// </summary>
|
||||
/// <returns>JSON string presentation of the object</returns>
|
||||
public string ToJson()
|
||||
{
|
||||
return JsonConvert.SerializeObject(this, Formatting.Indented);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns true if objects are equal
|
||||
/// </summary>
|
||||
/// <param name="obj">Object to be compared</param>
|
||||
/// <returns>Boolean</returns>
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
// credit: http://stackoverflow.com/a/10454552/677735
|
||||
return this.Equals(obj as EnumTest);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns true if EnumTest instances are equal
|
||||
/// </summary>
|
||||
/// <param name="other">Instance of EnumTest to be compared</param>
|
||||
/// <returns>Boolean</returns>
|
||||
public bool Equals(EnumTest other)
|
||||
{
|
||||
// credit: http://stackoverflow.com/a/10454552/677735
|
||||
if (other == null)
|
||||
return false;
|
||||
|
||||
return
|
||||
(
|
||||
this.EnumString == other.EnumString ||
|
||||
this.EnumString != null &&
|
||||
this.EnumString.Equals(other.EnumString)
|
||||
) &&
|
||||
(
|
||||
this.EnumInteger == other.EnumInteger ||
|
||||
this.EnumInteger != null &&
|
||||
this.EnumInteger.Equals(other.EnumInteger)
|
||||
) &&
|
||||
(
|
||||
this.EnumNumber == other.EnumNumber ||
|
||||
this.EnumNumber != null &&
|
||||
this.EnumNumber.Equals(other.EnumNumber)
|
||||
);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the hash code
|
||||
/// </summary>
|
||||
/// <returns>Hash code</returns>
|
||||
public override int GetHashCode()
|
||||
{
|
||||
// credit: http://stackoverflow.com/a/263416/677735
|
||||
unchecked // Overflow is fine, just wrap
|
||||
{
|
||||
int hash = 41;
|
||||
// Suitable nullity checks etc, of course :)
|
||||
|
||||
if (this.EnumString != null)
|
||||
hash = hash * 59 + this.EnumString.GetHashCode();
|
||||
|
||||
if (this.EnumInteger != null)
|
||||
hash = hash * 59 + this.EnumInteger.GetHashCode();
|
||||
|
||||
if (this.EnumNumber != null)
|
||||
hash = hash * 59 + this.EnumNumber.GetHashCode();
|
||||
|
||||
return hash;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -5,6 +5,7 @@ using System.Text;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.ComponentModel;
|
||||
using System.Runtime.Serialization;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Converters;
|
||||
@ -212,6 +213,6 @@ namespace IO.Swagger.Model
|
||||
return hash;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -5,6 +5,7 @@ using System.Text;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.ComponentModel;
|
||||
using System.Runtime.Serialization;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Converters;
|
||||
@ -105,6 +106,6 @@ namespace IO.Swagger.Model
|
||||
return hash;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -5,6 +5,7 @@ using System.Text;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.ComponentModel;
|
||||
using System.Runtime.Serialization;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Converters;
|
||||
@ -105,6 +106,6 @@ namespace IO.Swagger.Model
|
||||
return hash;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -5,6 +5,7 @@ using System.Text;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.ComponentModel;
|
||||
using System.Runtime.Serialization;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Converters;
|
||||
@ -143,6 +144,6 @@ namespace IO.Swagger.Model
|
||||
return hash;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -5,6 +5,7 @@ using System.Text;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.ComponentModel;
|
||||
using System.Runtime.Serialization;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Converters;
|
||||
@ -212,6 +213,6 @@ namespace IO.Swagger.Model
|
||||
return hash;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -5,6 +5,7 @@ using System.Text;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.ComponentModel;
|
||||
using System.Runtime.Serialization;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Converters;
|
||||
@ -220,6 +221,6 @@ namespace IO.Swagger.Model
|
||||
return hash;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -5,6 +5,7 @@ using System.Text;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.ComponentModel;
|
||||
using System.Runtime.Serialization;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Converters;
|
||||
@ -105,6 +106,6 @@ namespace IO.Swagger.Model
|
||||
return hash;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -5,6 +5,7 @@ using System.Text;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.ComponentModel;
|
||||
using System.Runtime.Serialization;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Converters;
|
||||
@ -121,6 +122,6 @@ namespace IO.Swagger.Model
|
||||
return hash;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -5,6 +5,7 @@ using System.Text;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.ComponentModel;
|
||||
using System.Runtime.Serialization;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Converters;
|
||||
@ -218,6 +219,6 @@ namespace IO.Swagger.Model
|
||||
return hash;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -89,4 +89,4 @@
|
||||
<ItemGroup>
|
||||
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
</Project>
|
||||
|
Loading…
x
Reference in New Issue
Block a user