Skip to content

Commit

Permalink
[csharp-netcore] Add unsigned integer/long support (#14885)
Browse files Browse the repository at this point in the history
* add unsigned integer/long support to c# netcore client

* undo change in test spec, samples

* new test spec

* update doc
  • Loading branch information
wing328 committed Mar 10, 2023
1 parent c4b404d commit 140d941
Show file tree
Hide file tree
Showing 11 changed files with 281 additions and 10 deletions.
4 changes: 4 additions & 0 deletions docs/generators/aspnetcore.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ These options may be applied as additional-properties (cli) or configOptions (pl
<li>long</li>
<li>long?</li>
<li>string</li>
<li>uint</li>
<li>uint?</li>
<li>ulong</li>
<li>ulong?</li>
</ul>

## RESERVED WORDS
Expand Down
4 changes: 4 additions & 0 deletions docs/generators/csharp-netcore-functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ These options may be applied as additional-properties (cli) or configOptions (pl
<li>long</li>
<li>long?</li>
<li>string</li>
<li>uint</li>
<li>uint?</li>
<li>ulong</li>
<li>ulong?</li>
</ul>

## RESERVED WORDS
Expand Down
4 changes: 4 additions & 0 deletions docs/generators/csharp-netcore.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ These options may be applied as additional-properties (cli) or configOptions (pl
<li>long</li>
<li>long?</li>
<li>string</li>
<li>uint</li>
<li>uint?</li>
<li>ulong</li>
<li>ulong?</li>
</ul>

## RESERVED WORDS
Expand Down
4 changes: 4 additions & 0 deletions docs/generators/csharp.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ These options may be applied as additional-properties (cli) or configOptions (pl
<li>long</li>
<li>long?</li>
<li>string</li>
<li>uint</li>
<li>uint?</li>
<li>ulong</li>
<li>ulong?</li>
</ul>

## RESERVED WORDS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1728,9 +1728,11 @@ public DefaultCodegen() {
typeMapping.put("DateTime", "Date");
typeMapping.put("long", "Long");
typeMapping.put("short", "Short");
typeMapping.put("integer", "Integer");
typeMapping.put("UnsignedInteger", "Integer");
typeMapping.put("UnsignedLong", "Long");
typeMapping.put("char", "String");
typeMapping.put("object", "Object");
typeMapping.put("integer", "Integer");
// FIXME: java specific type should be in Java Based Abstract Impl's
typeMapping.put("ByteArray", "byte[]");
typeMapping.put("binary", "File");
Expand Down Expand Up @@ -2394,8 +2396,14 @@ private String getPrimitiveType(Schema schema) {
return "number";
}
} else if (ModelUtils.isIntegerSchema(schema)) {
if (ModelUtils.isLongSchema(schema)) {
if (ModelUtils.isUnsignedLongSchema(schema)) {
return "UnsignedLong";
} else if (ModelUtils.isUnsignedIntegerSchema(schema)) {
return "UnsignedInteger";
} else if (ModelUtils.isLongSchema(schema)) {
return "long";
} else if (ModelUtils.isShortSchema(schema)) {// int32
return "integer";
} else {
return schema.getType(); // integer
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,12 @@ public AbstractCSharpCodegen() {
"decimal",
"int?",
"int",
"uint",
"uint?",
"long?",
"long",
"ulong",
"ulong?",
"float?",
"float",
"byte[]",
Expand Down Expand Up @@ -197,8 +201,10 @@ public AbstractCSharpCodegen() {
typeMapping.put("ByteArray", "byte[]");
typeMapping.put("boolean", "bool?");
typeMapping.put("integer", "int?");
typeMapping.put("float", "float?");
typeMapping.put("UnsignedInteger", "uint?");
typeMapping.put("UnsignedLong", "ulong?");
typeMapping.put("long", "long?");
typeMapping.put("float", "float?");
typeMapping.put("double", "double?");
typeMapping.put("number", "decimal?");
typeMapping.put("BigDecimal", "decimal?");
Expand All @@ -215,11 +221,12 @@ public AbstractCSharpCodegen() {

// nullable type
nullableType = new HashSet<>(
Arrays.asList("decimal", "bool", "int", "float", "long", "double", "DateTime", "DateTimeOffset", "Guid")
Arrays.asList("decimal", "bool", "int", "uint", "long", "ulong", "float", "double",
"DateTime", "DateTimeOffset", "Guid")
);
// value Types
valueTypes = new HashSet<>(
Arrays.asList("decimal", "bool", "int", "float", "long", "double")
Arrays.asList("decimal", "bool", "int", "uint", "long", "ulong", "float", "double")
);

this.setSortParamsByRequiredFlag(true);
Expand Down Expand Up @@ -1334,7 +1341,9 @@ public String toEnumValue(String value, String datatype) {
// Per: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/enum
// The approved types for an enum are byte, sbyte, short, ushort, int, uint, long, or ulong.
// but we're not supporting unsigned integral types or shorts.
if (datatype.startsWith("int") || datatype.startsWith("long") || datatype.startsWith("byte")) {
if (datatype.startsWith("int") || datatype.startsWith("uint") ||
datatype.startsWith("long") || datatype.startsWith("ulong") ||
datatype.startsWith("byte")) {
return value;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,10 @@ public CSharpClientCodegen() {

typeMapping.put("boolean", "bool");
typeMapping.put("integer", "int");
typeMapping.put("float", "float");
typeMapping.put("long", "long");
typeMapping.put("UnsignedInteger", "uint");
typeMapping.put("UnsignedLong", "ulong");
typeMapping.put("float", "float");
typeMapping.put("double", "double");
typeMapping.put("number", "decimal");
typeMapping.put("decimal", "decimal");
Expand Down Expand Up @@ -789,7 +791,8 @@ public String toEnumVarName(String value, String datatype) {
}

// number
if (datatype.startsWith("int") || datatype.startsWith("long") ||
if (datatype.startsWith("int") || datatype.startsWith("uint") ||
datatype.startsWith("ulong") || datatype.startsWith("long") ||
datatype.startsWith("double") || datatype.startsWith("float")) {
String varName = "NUMBER_" + value;
varName = varName.replaceAll("-", "MINUS_");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,10 @@ public CSharpNetCoreClientCodegen() {
typeMapping.put("ByteArray", "byte[]");
typeMapping.put("boolean", "bool");
typeMapping.put("integer", "int");
typeMapping.put("float", "float");
typeMapping.put("long", "long");
typeMapping.put("UnsignedInteger", "uint");
typeMapping.put("UnsignedLong", "ulong");
typeMapping.put("float", "float");
typeMapping.put("double", "double");
typeMapping.put("number", "decimal");
typeMapping.put("decimal", "decimal");
Expand Down Expand Up @@ -1176,7 +1178,8 @@ public String toEnumVarName(String value, String datatype) {
}

// number
if (datatype.startsWith("int") || datatype.startsWith("long") ||
if (datatype.startsWith("int") || datatype.startsWith("uint") ||
datatype.startsWith("long") || datatype.startsWith("ulong") ||
datatype.startsWith("double") || datatype.startsWith("float")) {
String varName = "NUMBER_" + value;
varName = varName.replaceAll("-", "MINUS_");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,15 @@ public static boolean isShortSchema(Schema schema) {
return false;
}

public static boolean isUnsignedIntegerSchema(Schema schema) {
if (SchemaTypeUtil.INTEGER_TYPE.equals(schema.getType()) && // type: integer
("int32".equals(schema.getFormat()) || schema.getFormat() == null) && // format: int32
(schema.getExtensions() != null && (Boolean) schema.getExtensions().getOrDefault("x-unsigned", Boolean.FALSE))) { // x-unsigned: true
return true;
}
return false;
}

public static boolean isLongSchema(Schema schema) {
if (SchemaTypeUtil.INTEGER_TYPE.equals(schema.getType()) // type: integer
&& SchemaTypeUtil.INTEGER64_FORMAT.equals(schema.getFormat())) { // format: long (int64)
Expand All @@ -614,6 +623,15 @@ public static boolean isLongSchema(Schema schema) {
return false;
}

public static boolean isUnsignedLongSchema(Schema schema) {
if (SchemaTypeUtil.INTEGER_TYPE.equals(schema.getType()) && // type: integer
"int64".equals(schema.getFormat()) && // format: int64
(schema.getExtensions() != null && (Boolean) schema.getExtensions().getOrDefault("x-unsigned", Boolean.FALSE))) { // x-unsigned: true
return true;
}
return false;
}

public static boolean isBooleanSchema(Schema schema) {
if (schema instanceof BooleanSchema) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@

package org.openapitools.codegen.csharpnetcore;

import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.media.Schema;
import org.openapitools.codegen.CodegenModel;
import org.openapitools.codegen.CodegenProperty;
import org.openapitools.codegen.TestUtils;
import org.openapitools.codegen.languages.CSharpNetCoreClientCodegen;
import org.openapitools.codegen.languages.JavaClientCodegen;
import org.testng.Assert;
import org.testng.annotations.Test;

Expand All @@ -37,4 +43,36 @@ public void testToEnumVarName() throws Exception {
// Assert.assertEquals(codegen.toEnumVarName("FOO-BAR", "string"), "FooBar");
// Assert.assertEquals(codegen.toEnumVarName("FOO_BAR", "string"), "FooBar");
}

@Test
public void testUnsigned() {
// test unsigned integer/long
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/unsigned-test.yaml");
CSharpNetCoreClientCodegen codegen = new CSharpNetCoreClientCodegen();

Schema test1 = openAPI.getComponents().getSchemas().get("format_test");
codegen.setOpenAPI(openAPI);
CodegenModel cm1 = codegen.fromModel("format_test", test1);
Assert.assertEquals(cm1.getClassname(), "FormatTest");

final CodegenProperty property1 = cm1.allVars.get(2);
Assert.assertEquals(property1.baseName, "unsigned_integer");
Assert.assertEquals(property1.dataType, "uint");
Assert.assertEquals(property1.vendorExtensions.get("x-unsigned"), Boolean.TRUE);
Assert.assertTrue(property1.isPrimitiveType);
Assert.assertTrue(property1.isInteger);
Assert.assertFalse(property1.isContainer);
Assert.assertFalse(property1.isFreeFormObject);
Assert.assertFalse(property1.isAnyType);

final CodegenProperty property2 = cm1.allVars.get(4);
Assert.assertEquals(property2.baseName, "unsigned_long");
Assert.assertEquals(property2.dataType, "ulong");
Assert.assertEquals(property2.vendorExtensions.get("x-unsigned"), Boolean.TRUE);
Assert.assertTrue(property2.isPrimitiveType);
Assert.assertTrue(property2.isLong);
Assert.assertFalse(property2.isContainer);
Assert.assertFalse(property2.isFreeFormObject);
Assert.assertFalse(property2.isAnyType);
}
}

0 comments on commit 140d941

Please sign in to comment.