Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
[csharp] Change enum value suffix name
'enumValueNameSuffix' and 'enumNameSuffix' were introduced in a recent
commit. This changes 'enumValueNameSuffix' to 'enumValueSuffix' to
better differentiate between the two options. This also adds a caveat to
the default description which explains that this flexibility may cause
issues when used by client generator.
  • Loading branch information
jimschubert committed Jan 5, 2020
commit 872edf60a9c5b4fdcb7a8dcc8e8c0216d1d76ada
2 changes: 1 addition & 1 deletion docs/generators/aspnetcore.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ sidebar_label: aspnetcore
|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|
|enumValueSuffix|Suffix that will be appended to all enum values.| |Enum|
|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 @@ -227,8 +227,8 @@ public static enum ENUM_PROPERTY_NAMING_TYPE {camelCase, PascalCase, snake_case,
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 ENUM_VALUE_SUFFIX = "enumValueSuffix";
public static final String ENUM_VALUE_SUFFIX_DESC = "Suffix that will be appended to all enum values. Note: For clients this may impact serialization and deserialization of enum values.";

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 @@ -63,7 +63,7 @@ public abstract class AbstractCSharpCodegen extends DefaultCodegen implements Co

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

protected String sourceFolder = "src";

Expand Down Expand Up @@ -367,8 +367,8 @@ public void processOpts() {
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());
if (additionalProperties().containsKey(CodegenConstants.ENUM_VALUE_SUFFIX)) {
setEnumValueSuffix(additionalProperties.get(CodegenConstants.ENUM_VALUE_SUFFIX).toString());
}

// This either updates additionalProperties with the above fixes, or sets the default if the option was not specified.
Expand Down Expand Up @@ -1017,8 +1017,8 @@ public void setEnumNameSuffix(final String enumNameSuffix) {
this.enumNameSuffix = enumNameSuffix;
}

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

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

enumName = camelize(enumName) + this.enumValueNameSuffix;
enumName = camelize(enumName) + this.enumValueSuffix;

if (enumName.matches("\\d.*")) { // starts with number
return "_" + enumName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,9 +214,9 @@ public AspNetCoreServerCodegen() {
CodegenConstants.ENUM_NAME_SUFFIX_DESC,
enumNameSuffix);

addOption(CodegenConstants.ENUM_VALUE_NAME_SUFFIX,
CodegenConstants.ENUM_VALUE_NAME_SUFFIX_DESC,
enumValueNameSuffix);
addOption(CodegenConstants.ENUM_VALUE_SUFFIX,
"Suffix that will be appended to all enum values.",
enumValueSuffix);

classModifier.addEnum("", "Keep class default with no modifier");
classModifier.addEnum("abstract", "Make class abstract");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/

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;
Expand All @@ -30,7 +31,6 @@

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

public class CsharpModelEnumTest {
Expand Down Expand Up @@ -93,6 +93,25 @@ public void overrideEnumTest() {
*/
}

@Test(description = "use custom suffixes for enums")
public void useCustomEnumSuffixes() {
final AspNetCoreServerCodegen codegen = new AspNetCoreServerCodegen();
codegen.setEnumNameSuffix("EnumName");
codegen.setEnumValueSuffix("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");
}

@Test(description = "use default suffixes for enums")
public void useDefaultEnumSuffixes() {
final AspNetCoreServerCodegen codegen = new AspNetCoreServerCodegen();
Expand All @@ -110,11 +129,11 @@ public void useDefaultEnumSuffixes() {
Assert.assertEquals(codegen.toEnumVarName("Aaaa", ""), "AaaaEnum");
}

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

OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/3_0/petstore.yaml");
codegen.setOpenAPI(openAPI);
Expand All @@ -124,8 +143,9 @@ public void useCustomEnumSuffixes() {
final CodegenProperty statusProperty = cm.vars.get(5);
Assert.assertEquals(statusProperty.name, "Status");
Assert.assertTrue(statusProperty.isEnum);
Assert.assertEquals(statusProperty.datatypeWithEnum, "StatusEnumName");
Assert.assertEquals(statusProperty.datatypeWithEnum, "Status");

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

}