[abstract csharp] Ensure enum dataType is not nullable string (#9891)

* ensure enum cannot be a nullable string

* build samples

* removed errounous changes meant for another pr

* build samples

* build samples

* reordered test parameters for readability

* build samples

* deleted eroneous sample files
This commit is contained in:
devhl-labs 2021-08-22 11:40:46 -04:00 committed by GitHub
parent 84a6e31fb4
commit bee8b615a5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 0 deletions

View File

@ -437,6 +437,22 @@ public abstract class AbstractCSharpCodegen extends DefaultCodegen implements Co
return processed;
}
@Override
protected List<Map<String, Object>> buildEnumVars(List<Object> values, String dataType) {
List<Map<String, Object>> enumVars = super.buildEnumVars(values, dataType);
// this is needed for enumRefs like OuterEnum marked as nullable and also have string values
// keep isString true so that the index will be used as the enum value instead of a string
// this is inline with C# enums with string values
if ("string?".equals(dataType)){
enumVars.forEach((enumVar) -> {
enumVar.put("isString", true);
});
}
return enumVars;
}
/**
* C# differs from other languages in that Enums are not _true_ objects; enums are compiled to integral types.
* So, in C#, an enum is considers more like a user-defined primitive.

View File

@ -26,7 +26,9 @@ 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.AbstractCSharpCodegen;
import org.openapitools.codegen.languages.AspNetCoreServerCodegen;
import org.openapitools.codegen.languages.CSharpNetCoreClientCodegen;
import org.openapitools.codegen.languages.CSharpClientCodegen;
import org.testng.Assert;
import org.testng.annotations.Test;
@ -34,6 +36,32 @@ import org.testng.annotations.Test;
@SuppressWarnings("static-method")
public class CSharpModelTest {
@Test
public void assertOuterEnumIsString() {
// this issue has not been found yet in version 2
// Assert.assertEquals(outerEnumVarsIsString(new AspNetCoreServerCodegen(), 2, false), true);
// Assert.assertEquals(outerEnumVarsIsString(new AspNetCoreServerCodegen(), 2, true), true);
Assert.assertEquals(outerEnumVarsIsString(new AspNetCoreServerCodegen(), 3, false), true);
Assert.assertEquals(outerEnumVarsIsString(new AspNetCoreServerCodegen(), 3, true), true);
// this issue has not been found yet in version 2
// Assert.assertEquals(outerEnumVarsIsString(new CSharpNetCoreClientCodegen(), 2, false), true);
// Assert.assertEquals(outerEnumVarsIsString(new CSharpNetCoreClientCodegen(), 2, true), true);
Assert.assertEquals(outerEnumVarsIsString(new CSharpNetCoreClientCodegen(), 3, false), true);
Assert.assertEquals(outerEnumVarsIsString(new CSharpNetCoreClientCodegen(), 3, true), true);
}
public boolean outerEnumVarsIsString(final AbstractCSharpCodegen codegen, final int openApiVersion, final Boolean nullableReferenceTypes){
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/" + Integer.toString(openApiVersion) + "_0/petstore-with-fake-endpoints-models-for-testing-with-http-signature.yaml");
codegen.setNullableReferenceTypes(nullableReferenceTypes);
codegen.setOpenAPI(openAPI);
Schema schema = openAPI.getComponents().getSchemas().get("Enum_Test");
final CodegenModel generated = codegen.fromModel("OuterEnum", schema);
CodegenProperty cp0 = generated.getVars().get(0);
return cp0.isString;
}
@Test(description = "convert a model with array property to default List<T>")
public void arrayPropertyTest() {
final Schema schema = getArrayTestSchema();