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

Fix float/double default value in C# generator #791

Merged
merged 7 commits into from Aug 13, 2018
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions appveyor.yml
Expand Up @@ -35,13 +35,19 @@ build_script:
# install openapi-generator locally
- mvn clean install --quiet
test_script:
# restore test-related files
- copy /b/v/y CI\samples.ci\client\petstore\csharp\OpenAPIClient\src\Org.OpenAPITools.Test\Org.OpenAPITools.Test.csproj samples\client\petstore\csharp\OpenAPIClient\src\Org.OpenAPITools.Test\Org.OpenAPITools.Test.csproj
# test c# API client
- nunit-console samples\client\petstore\csharp\OpenAPIClient\src\Org.OpenAPITools.Test\bin\Debug\Org.OpenAPITools.Test.dll --result=myresults.xml;format=AppVeyor
# test c# API client (with PropertyChanged)
- nunit-console samples\client\petstore\csharp\OpenAPIClientWithPropertyChanged\src\Org.OpenAPITools.Test\bin\Debug\Org.OpenAPITools.Test.dll --result=myresults.xml;format=AppVeyor

# generate all petstore clients
- .\bin\windows\run-all-petstore.cmd

# generate all petstore clients (openapi3)
- .\bin\openapi3\windows\run-all-petstore.cmd

cache:
- C:\maven\
- C:\Users\appveyor\.m2
5 changes: 3 additions & 2 deletions bin/csharp-petstore.sh
Expand Up @@ -29,8 +29,9 @@ fi
export JAVA_OPTS="${JAVA_OPTS} -XX:MaxPermSize=256M -Xmx1024M -DloggerPath=conf/log4j.properties"
ags="generate -i modules/openapi-generator/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml -g csharp -o samples/client/petstore/csharp/OpenAPIClient --additional-properties packageGuid={321C8C3F-0156-40C1-AE42-D59761FB9B6C} $@"

java $JAVA_OPTS -jar $executable $ags

# restore csproj file
echo "restore csproject file: CI/samples/client/petstore/csharp/OpenAPIClient/src/Org.OpenAPITools.Test/Org.OpenAPITools.Test.csproj"
cp ./CI/samples/client/petstore/csharp/OpenAPIClient/src/Org.OpenAPITools.Test/Org.OpenAPITools.Test.csproj ./samples/client/petstore/csharp/OpenAPIClient/src/Org.OpenAPITools.Test/
cp ./CI/samples.ci/client/petstore/csharp/OpenAPIClient/src/Org.OpenAPITools.Test/Org.OpenAPITools.Test.csproj ./samples/client/petstore/csharp/OpenAPIClient/src/Org.OpenAPITools.Test/

java $JAVA_OPTS -jar $executable $ags
2 changes: 1 addition & 1 deletion bin/openapi3/csharp-petstore.sh
Expand Up @@ -27,6 +27,6 @@ fi

# if you've executed sbt assembly previously it will use that instead.
export JAVA_OPTS="${JAVA_OPTS} -XX:MaxPermSize=256M -Xmx1024M -DloggerPath=conf/log4j.properties"
ags="generate $@ -i modules/openapi-generator/src/test/resources/3_0/petstore-with-fake-endpoints-models-for-testing.yaml -g csharp -o samples/client/petstore/csharp/OpenAPIClient --additional-properties packageGuid={321C8C3F-0156-40C1-AE42-D59761FB9B6C}"
ags="generate $@ -i modules/openapi-generator/src/test/resources/3_0/petstore-with-fake-endpoints-models-for-testing.yaml -g csharp -o samples/openapi3/client/petstore/csharp/OpenAPIClient --additional-properties packageGuid={321C8C3F-0156-40C1-AE42-D59761FB9B6C}"

java $JAVA_OPTS -jar $executable $ags
33 changes: 33 additions & 0 deletions bin/openapi3/run-all-petstore
@@ -0,0 +1,33 @@
#!/bin/bash
# this bash script will loop through all the .sh files under bin
# execute the script and check the result (exit code) to see if
# there's any error

echo "IMPORTANT: this script should be run by the CI (e.g. Shippable) only. There's no need to run this script to update Petstore samples for all generators."
echo "Please press CTRL+C to stop or the script will continue in 10 seconds."

sleep 10

successes=0
failures=0
for SCRIPT in $(ls -l ./bin/openapi3/*.sh | grep -v all)
do
if [ -f ${SCRIPT} -a -x ${SCRIPT} ]; then
echo "Running $SCRIPT (output to /dev/null)"
${SCRIPT} 2>&1 > /dev/null
rc=$?
if [[ ${rc} != 0 ]]; then
>&2 echo "ERROR!! FAILED TO RUN ${SCRIPT}"
((failures+=1))
else
((successes+=1))
fi
fi
done

if (( failures > 0 )); then
>&2 echo "[ERROR] ${failures} out of $((failures+successes)) scripts failed."
exit 1
else
echo "[SUCCESS] ${successes} generators finished."
fi
15 changes: 15 additions & 0 deletions bin/openapi3/windows/csharp-petstore.bat
@@ -0,0 +1,15 @@
set executable=.\modules\openapi-generator-cli\target\openapi-generator-cli.jar

If Not Exist %executable% (
mvn clean package
)

REM set JAVA_OPTS=%JAVA_OPTS% -Xmx1024M
set ags=generate -i modules\openapi-generator\src\test/resources\2_0\petstore-with-fake-endpoints-models-for-testing.yaml -g csharp -o samples\openapi3\client\petstore\csharp\SwaggerClient --additional-properties packageGuid={321C8C3F-0156-40C1-AE42-D59761FB9B6C}

java %JAVA_OPTS% -jar %executable% %ags%

# restore csproj file
echo "restore csproject file: CI\samples\client\petstore\csharp\OpenAPIClient\src\Org.OpenAPITools.Test\Org.OpenAPITools.Test.csproj"
copy /b/v/y .\CI\samples.ci\client\petstore\csharp\OpenAPIClient\src\Org.OpenAPITools.Test\Org.OpenAPITools.Test.csproj .\samples\openapi3\client\petstore\csharp\OpenAPIClient\src\Org.OpenAPITools.Test\

15 changes: 15 additions & 0 deletions bin/openapi3/windows/run-all-petstore.cmd
@@ -0,0 +1,15 @@
REM this batch file will loop through all the .bat files under bin\openapi3\windows\
REM execute the script and check the error level to see if there's any error

echo IMPORTANT: this script should be run by the CI (e.g. appveyor) only. There's no need to run this script to update Petstore samples for all generators.
echo Please press CTRL+C to stop or the script will continue in 10 seconds.

timeout 10

for /f "delims=" %%i in ('dir /b ".\bin\openapi3\windows\*.bat"') do (

CALL .\bin\openapi3\windows\%%i

IF ERRORLEVEL 1 IF NOT ERRORLEVEL 2 exit /b 1

)
6 changes: 3 additions & 3 deletions bin/run-all-petstore
Expand Up @@ -13,8 +13,8 @@ failures=0
for SCRIPT in $(ls -l ./bin/*.sh | grep -v all)
do
if [ -f ${SCRIPT} -a -x ${SCRIPT} ]; then
echo "Running $SCRIPT"
${SCRIPT}
echo "Running $SCRIPT (output to /dev/null)"
${SCRIPT} 2>&1 > /dev/null
rc=$?
if [[ ${rc} != 0 ]]; then
>&2 echo "ERROR!! FAILED TO RUN ${SCRIPT}"
Expand All @@ -30,4 +30,4 @@ if (( failures > 0 )); then
exit 1
else
echo "[SUCCESS] ${successes} generators finished."
fi
fi
1 change: 1 addition & 0 deletions bin/utils/ensure-up-to-date
Expand Up @@ -31,6 +31,7 @@ sleep 5
./bin/typescript-inversify-petstore.sh > /dev/null 2>&1
./bin/rust-server-petstore.sh > /dev/null 2>&1
./bin/openapi3/haskell-http-client-petstore.sh > /dev/null 2>&1
./bin/csharp-petstore.sh > /dev/null 2>&1

# Check:
if [ -n "$(git status --porcelain)" ]; then
Expand Down
2 changes: 1 addition & 1 deletion bin/windows/csharp-petstore.bat
Expand Up @@ -11,5 +11,5 @@ java %JAVA_OPTS% -jar %executable% %ags%

# restore csproj file
echo "restore csproject file: CI\samples\client\petstore\csharp\OpenAPIClient\src\Org.OpenAPITools.Test\Org.OpenAPITools.Test.csproj"
copy .\CI\samples\client\petstore\csharp\OpenAPIClient\src\Org.OpenAPITools.Test\Org.OpenAPITools.Test.csproj .\samples\client\petstore\csharp\OpenAPIClient\src\Org.OpenAPITools.Test\
copy /b/v/y .\CI\samples.ci\client\petstore\csharp\OpenAPIClient\src\Org.OpenAPITools.Test\Org.OpenAPITools.Test.csproj .\samples\client\petstore\csharp\OpenAPIClient\src\Org.OpenAPITools.Test\

Expand Up @@ -19,6 +19,7 @@

import com.google.common.collect.ImmutableMap;
import com.samskivert.mustache.Mustache;
import io.swagger.v3.core.util.Json;
import org.apache.commons.lang3.StringUtils;
import org.openapitools.codegen.*;
import org.openapitools.codegen.utils.*;
Expand Down Expand Up @@ -713,12 +714,22 @@ public String toDefaultValue(Schema p) {
return p.getDefault().toString();
}
} else if (ModelUtils.isDateSchema(p)) {
// TODO
if (p.getDefault() != null) {
return "\"" + p.getDefault().toString() + "\"";
}
} else if (ModelUtils.isDateTimeSchema(p)) {
// TODO
if (p.getDefault() != null) {
return "\"" + p.getDefault().toString() + "\"";
}
} else if (ModelUtils.isNumberSchema(p)) {
if (p.getDefault() != null) {
return p.getDefault().toString();
if (ModelUtils.isFloatSchema(p)) { // float
return p.getDefault().toString() + "F";
} else if (ModelUtils.isDoubleSchema(p)) { // double
return p.getDefault().toString() + "D";
} else {
return p.getDefault().toString();
}
}
} else if (ModelUtils.isIntegerSchema(p)) {
if (p.getDefault() != null) {
Expand Down Expand Up @@ -967,4 +978,42 @@ public boolean isDataTypeString(String dataType) {
// also treat double/decimal/float as "string" in enum so that the values (e.g. 2.8) get double-quoted
return "String".equalsIgnoreCase(dataType) || "double?".equals(dataType) || "decimal?".equals(dataType) || "float?".equals(dataType);
}

@Override
public void setParameterExampleValue(CodegenParameter codegenParameter) {

// set the example value
// if not specified in x-example, generate a default value
// TODO need to revise how to obtain the example value
if (codegenParameter.vendorExtensions != null && codegenParameter.vendorExtensions.containsKey("x-example")) {
codegenParameter.example = Json.pretty(codegenParameter.vendorExtensions.get("x-example"));
} else if (Boolean.TRUE.equals(codegenParameter.isBoolean)) {
codegenParameter.example = "true";
} else if (Boolean.TRUE.equals(codegenParameter.isLong)) {
codegenParameter.example = "789";
} else if (Boolean.TRUE.equals(codegenParameter.isInteger)) {
codegenParameter.example = "56";
} else if (Boolean.TRUE.equals(codegenParameter.isFloat)) {
codegenParameter.example = "3.4F";
} else if (Boolean.TRUE.equals(codegenParameter.isDouble)) {
codegenParameter.example = "1.2D";
} else if (Boolean.TRUE.equals(codegenParameter.isNumber)) {
codegenParameter.example = "8.14";
} else if (Boolean.TRUE.equals(codegenParameter.isBinary)) {
codegenParameter.example = "BINARY_DATA_HERE";
} else if (Boolean.TRUE.equals(codegenParameter.isByteArray)) {
codegenParameter.example = "BYTE_ARRAY_DATA_HERE";
} else if (Boolean.TRUE.equals(codegenParameter.isFile)) {
codegenParameter.example = "/path/to/file.txt";
} else if (Boolean.TRUE.equals(codegenParameter.isDate)) {
codegenParameter.example = "2013-10-20";
} else if (Boolean.TRUE.equals(codegenParameter.isDateTime)) {
codegenParameter.example = "2013-10-20T19:20:30+01:00";
} else if (Boolean.TRUE.equals(codegenParameter.isUuid)) {
codegenParameter.example = "38400000-8cf0-11bd-b23e-10b96e4ef00d";
} else if (Boolean.TRUE.equals(codegenParameter.isString)) {
codegenParameter.example = codegenParameter.paramName + "_example";
}

}
}
Expand Up @@ -603,11 +603,11 @@ public void postProcessModelProperty(CodegenModel model, CodegenProperty propert
}

/*
* The pattern spec follows the Perl convention and style of modifiers. .NET
* does not support this syntax directly so we need to convert the pattern to a .NET compatible
* format and apply modifiers in a compatible way.
* See https://msdn.microsoft.com/en-us/library/yd1hzczs(v=vs.110).aspx for .NET options.
*/
* The pattern spec follows the Perl convention and style of modifiers. .NET
* does not support this syntax directly so we need to convert the pattern to a .NET compatible
* format and apply modifiers in a compatible way.
* See https://msdn.microsoft.com/en-us/library/yd1hzczs(v=vs.110).aspx for .NET options.
*/
public void postProcessPattern(String pattern, Map<String, Object> vendorExtensions) {
if (pattern != null) {
int i = pattern.lastIndexOf('/');
Expand Down Expand Up @@ -707,7 +707,7 @@ public String toEnumVarName(String value, String datatype) {
}

// number
if(datatype.startsWith("int") || datatype.startsWith("long") ||
if (datatype.startsWith("int") || datatype.startsWith("long") ||
datatype.startsWith("double") || datatype.startsWith("float")) {
String varName = "NUMBER_" + value;
varName = varName.replaceAll("-", "MINUS_");
Expand Down Expand Up @@ -751,13 +751,18 @@ public String toVarName(String name) {

public String getNameUsingModelPropertyNaming(String name) {
switch (CodegenConstants.MODEL_PROPERTY_NAMING_TYPE.valueOf(getModelPropertyNaming())) {
case original: return name;
case camelCase: return camelize(name, true);
case PascalCase: return camelize(name);
case snake_case: return underscore(name);
default: throw new IllegalArgumentException("Invalid model property naming '" +
name + "'. Must be 'original', 'camelCase', " +
"'PascalCase' or 'snake_case'");
case original:
return name;
case camelCase:
return camelize(name, true);
case PascalCase:
return camelize(name);
case snake_case:
return underscore(name);
default:
throw new IllegalArgumentException("Invalid model property naming '" +
name + "'. Must be 'original', 'camelCase', " +
"'PascalCase' or 'snake_case'");
}
}

Expand Down
8 changes: 4 additions & 4 deletions samples/client/petstore/csharp/OpenAPIClient/docs/FakeApi.md
Expand Up @@ -160,7 +160,7 @@ namespace Example
public void main()
{
var apiInstance = new FakeApi();
var body = 1.2; // decimal? | Input number as post body (optional)
var body = 1.2D; // decimal? | Input number as post body (optional)

try
{
Expand Down Expand Up @@ -464,13 +464,13 @@ namespace Example

var apiInstance = new FakeApi();
var number = 8.14; // decimal? | None
var _double = 1.2; // double? | None
var _double = 1.2D; // double? | None
var patternWithoutDelimiter = patternWithoutDelimiter_example; // string | None
var _byte = BYTE_ARRAY_DATA_HERE; // byte[] | None
var integer = 56; // int? | None (optional)
var int32 = 56; // int? | None (optional)
var int64 = 789; // long? | None (optional)
var _float = 3.4; // float? | None (optional)
var _float = 3.4F; // float? | None (optional)
var _string = _string_example; // string | None (optional)
var binary = BINARY_DATA_HERE; // System.IO.Stream | None (optional)
var date = 2013-10-20; // DateTime? | None (optional)
Expand Down Expand Up @@ -554,7 +554,7 @@ namespace Example
var enumQueryStringArray = enumQueryStringArray_example; // List<string> | Query parameter enum test (string array) (optional)
var enumQueryString = enumQueryString_example; // string | Query parameter enum test (string) (optional) (default to -efg)
var enumQueryInteger = 56; // int? | Query parameter enum test (double) (optional)
var enumQueryDouble = 1.2; // double? | Query parameter enum test (double) (optional)
var enumQueryDouble = 1.2D; // double? | Query parameter enum test (double) (optional)
var enumFormStringArray = new List<string>(); // List<string> | Form parameter enum test (string array) (optional) (default to $)
var enumFormString = enumFormString_example; // string | Form parameter enum test (string) (optional) (default to -efg)

Expand Down
14 changes: 0 additions & 14 deletions samples/server/petstore/nancyfx/src/IO.Swagger/IO.Swagger.nuspec

This file was deleted.

Expand Up @@ -5,7 +5,7 @@
using Sharpility.Extensions;
using NodaTime;

namespace IO.Swagger.v2.Models
namespace Org.OpenAPITools.v2.Models
{
/// <summary>
/// Describes the result of uploading an image resource
Expand Down
Expand Up @@ -5,7 +5,7 @@
using Sharpility.Extensions;
using NodaTime;

namespace IO.Swagger.v2.Models
namespace Org.OpenAPITools.v2.Models
{
/// <summary>
/// A category for a pet
Expand Down
Expand Up @@ -5,7 +5,7 @@
using Sharpility.Extensions;
using NodaTime;

namespace IO.Swagger.v2.Models
namespace Org.OpenAPITools.v2.Models
{
/// <summary>
/// An order for a pets from the pet store
Expand Down
Expand Up @@ -5,7 +5,7 @@
using Sharpility.Extensions;
using NodaTime;

namespace IO.Swagger.v2.Models
namespace Org.OpenAPITools.v2.Models
{
/// <summary>
/// A pet for sale in the pet store
Expand Down
Expand Up @@ -5,7 +5,7 @@
using Sharpility.Extensions;
using NodaTime;

namespace IO.Swagger.v2.Models
namespace Org.OpenAPITools.v2.Models
{
/// <summary>
/// A tag for a pet
Expand Down
Expand Up @@ -5,7 +5,7 @@
using Sharpility.Extensions;
using NodaTime;

namespace IO.Swagger.v2.Models
namespace Org.OpenAPITools.v2.Models
{
/// <summary>
/// A User who is purchasing from the pet store
Expand Down