Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[C#] allow customization of generated enum suffixes #4301

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/generators/aspnetcore.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ sidebar_label: aspnetcore
|useNewtonsoft|Uses the Newtonsoft JSON library.| |true|
|newtonsoftVersion|Version for Microsoft.AspNetCore.Mvc.NewtonsoftJson for ASP.NET Core 3.0+| |3.0.0-preview5-19227-01|
|useDefaultRouting|Use default routing for the ASP.NET Core version. For 3.0 turn off default because it is not yet supported.| |true|
|enumNameSuffix|Suffix that will be appended to all enum names.| |Enum|
|enumValueNameSuffix|Suffix that will be appended to all enum value names.| |Enum|
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the use case for this? Why wouldn't someone just put the expected value in the enum text field in the specification document?

Copy link
Contributor Author

@scottdallamura scottdallamura Nov 11, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The behavior before my change is to append Enum to both the name of the enum type and each value. So you end up with

enum MyEnum {
  Value1Enum,
  Value2Enum
}

I didn't want to break the existing default behavior, so I made that suffix customizable too.

If you have suggestions I'd be happy to change it :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the delay in response. The holidays were pretty hectic for me.

I think we'll want to call the value one enumValueSuffix. I'll do that in a separate commit.

|classModifier|Class Modifier can be empty, abstract| ||
|operationModifier|Operation Modifier can be virtual, abstract or partial| |virtual|
|buildTarget|Target to build an application or library| |program|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,12 @@ public static enum ENUM_PROPERTY_NAMING_TYPE {camelCase, PascalCase, snake_case,
public static final String MODEL_NAME_SUFFIX = "modelNameSuffix";
public static final String MODEL_NAME_SUFFIX_DESC = "Suffix that will be appended to all model names.";

public static final String ENUM_NAME_SUFFIX = "enumNameSuffix";
public static final String ENUM_NAME_SUFFIX_DESC = "Suffix that will be appended to all enum names.";

public static final String ENUM_VALUE_NAME_SUFFIX = "enumValueNameSuffix";
public static final String ENUM_VALUE_NAME_SUFFIX_DESC = "Suffix that will be appended to all enum value names.";

public static final String GIT_HOST = "gitHost";
public static final String GIT_HOST_DESC = "Git host, e.g. gitlab.com.";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ public abstract class AbstractCSharpCodegen extends DefaultCodegen implements Co
protected String packageAuthors = "OpenAPI";

protected String interfacePrefix = "I";
protected String enumNameSuffix = "Enum";
protected String enumValueNameSuffix = "Enum";

protected String sourceFolder = "src";

Expand Down Expand Up @@ -361,6 +363,14 @@ public void processOpts() {
}
}

if (additionalProperties().containsKey(CodegenConstants.ENUM_NAME_SUFFIX)) {
setEnumNameSuffix(additionalProperties.get(CodegenConstants.ENUM_NAME_SUFFIX).toString());
}

if (additionalProperties().containsKey(CodegenConstants.ENUM_VALUE_NAME_SUFFIX)) {
setEnumValueNameSuffix(additionalProperties.get(CodegenConstants.ENUM_VALUE_NAME_SUFFIX).toString());
}

// This either updates additionalProperties with the above fixes, or sets the default if the option was not specified.
additionalProperties.put(CodegenConstants.INTERFACE_PREFIX, interfacePrefix);
}
Expand Down Expand Up @@ -1003,6 +1013,14 @@ public void setInterfacePrefix(final String interfacePrefix) {
this.interfacePrefix = interfacePrefix;
}

public void setEnumNameSuffix(final String enumNameSuffix) {
this.enumNameSuffix = enumNameSuffix;
}

public void setEnumValueNameSuffix(final String enumValueNameSuffix) {
this.enumValueNameSuffix = enumValueNameSuffix;
}

public boolean isSupportNullable() {
return supportNullable;
}
Expand Down Expand Up @@ -1040,7 +1058,7 @@ public String toEnumVarName(String name, String datatype) {
enumName = enumName.replaceFirst("^_", "");
enumName = enumName.replaceFirst("_$", "");

enumName = camelize(enumName) + "Enum";
enumName = camelize(enumName) + this.enumValueNameSuffix;

if (enumName.matches("\\d.*")) { // starts with number
return "_" + enumName;
Expand All @@ -1051,7 +1069,7 @@ public String toEnumVarName(String name, String datatype) {

@Override
public String toEnumName(CodegenProperty property) {
return sanitizeName(camelize(property.name)) + "Enum";
return sanitizeName(camelize(property.name)) + this.enumNameSuffix;
}

public String testPackageName() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,11 +206,18 @@ public AspNetCoreServerCodegen() {
"Version for Microsoft.AspNetCore.Mvc.NewtonsoftJson for ASP.NET Core 3.0+",
newtonsoftVersion);


addSwitch(USE_DEFAULT_ROUTING,
"Use default routing for the ASP.NET Core version. For 3.0 turn off default because it is not yet supported.",
useDefaultRouting);

addOption(CodegenConstants.ENUM_NAME_SUFFIX,
CodegenConstants.ENUM_NAME_SUFFIX_DESC,
enumNameSuffix);

addOption(CodegenConstants.ENUM_VALUE_NAME_SUFFIX,
CodegenConstants.ENUM_VALUE_NAME_SUFFIX_DESC,
enumValueNameSuffix);

classModifier.addEnum("", "Keep class default with no modifier");
classModifier.addEnum("abstract", "Make class abstract");
classModifier.setDefault("");
Expand Down Expand Up @@ -348,7 +355,6 @@ public void processOpts() {
supportingFiles.add(new SupportingFile("Project.nuspec.mustache", packageFolder, packageName + ".nuspec"));
}


if (useSwashbuckle) {
supportingFiles.add(new SupportingFile("Filters" + File.separator + "BasePathFilter.mustache",
packageFolder + File.separator + "Filters", "BasePathFilter.cs"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,21 @@
*/

package org.openapitools.codegen.csharp;

import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.media.StringSchema;
import org.openapitools.codegen.CodegenModel;
import org.openapitools.codegen.CodegenProperty;
import org.openapitools.codegen.DefaultCodegen;
import org.openapitools.codegen.TestUtils;
import org.openapitools.codegen.languages.AspNetCoreServerCodegen;
import org.openapitools.codegen.languages.CSharpClientCodegen;
import org.testng.Assert;
import org.testng.annotations.Test;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CsharpModelEnumTest {
Expand Down Expand Up @@ -86,4 +92,40 @@ public void overrideEnumTest() {
Assert.assertTrue(enumVar.isEnum);
*/
}

@Test(description = "use default suffixes for enums")
public void useDefaultEnumSuffixes() {
final AspNetCoreServerCodegen codegen = new AspNetCoreServerCodegen();

OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/3_0/petstore.yaml");
codegen.setOpenAPI(openAPI);

final Schema petSchema = openAPI.getComponents().getSchemas().get("Pet");
final CodegenModel cm = codegen.fromModel("Pet", petSchema);
final CodegenProperty statusProperty = cm.vars.get(5);
Assert.assertEquals(statusProperty.name, "Status");
Assert.assertTrue(statusProperty.isEnum);
Assert.assertEquals(statusProperty.datatypeWithEnum, "StatusEnum");

Assert.assertEquals(codegen.toEnumVarName("Aaaa", ""), "AaaaEnum");
}

@Test(description = "use custom suffixes for enums")
public void useCustomEnumSuffixes() {
final AspNetCoreServerCodegen codegen = new AspNetCoreServerCodegen();
codegen.setEnumNameSuffix("EnumName");
codegen.setEnumValueNameSuffix("EnumValue");

OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/3_0/petstore.yaml");
codegen.setOpenAPI(openAPI);

final Schema petSchema = openAPI.getComponents().getSchemas().get("Pet");
final CodegenModel cm = codegen.fromModel("Pet", petSchema);
final CodegenProperty statusProperty = cm.vars.get(5);
Assert.assertEquals(statusProperty.name, "Status");
Assert.assertTrue(statusProperty.isEnum);
Assert.assertEquals(statusProperty.datatypeWithEnum, "StatusEnumName");

Assert.assertEquals(codegen.toEnumVarName("Aaaa", ""), "AaaaEnumValue");
}
}