Skip to content

Commit

Permalink
Improve example value handling in C# generators (#11355)
Browse files Browse the repository at this point in the history
* improve example value handling in C# generators

* fix typo

* update samples
  • Loading branch information
wing328 authored Jan 19, 2022
1 parent 0cb88ce commit 2d927a7
Show file tree
Hide file tree
Showing 33 changed files with 426 additions and 344 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1261,42 +1261,124 @@ protected boolean isValueType(CodegenProperty var) {
}

@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.isUri)) {
codegenParameter.example = "https://openapi-generator.tech";
} else if (Boolean.TRUE.equals(codegenParameter.isString)) {
codegenParameter.example = codegenParameter.paramName + "_example";
public void setParameterExampleValue(CodegenParameter p) {
String example;

boolean hasAllowableValues = p.allowableValues != null && !p.allowableValues.isEmpty();
if (hasAllowableValues) {
//support examples for inline enums
final List<Object> values = (List<Object>) p.allowableValues.get("values");
example = String.valueOf(values.get(0));
} else if (p.defaultValue == null) {
example = p.example;
} else {
example = p.defaultValue;
}

String type = p.baseType;
if (type == null) {
type = p.dataType;
}

if (p.isString) {
if (example == null) {
example = p.paramName + "_example";
}
example = "\"" + escapeText(example) + "\"";
} else if (p.isInteger || p.isShort) {
if (example == null) {
example = "56";
}
} else if (p.isLong) {
if (example == null) {
example = "789";
}
example = StringUtils.appendIfMissingIgnoreCase(example, "L");
} else if (p.isFloat) {
if (example == null) {
example = "3.4F";
}
example = StringUtils.appendIfMissingIgnoreCase(example, "F");
} else if (p.isDouble) {
if (example == null) {
example = "1.2D";
}
example = StringUtils.appendIfMissingIgnoreCase(example, "D");
} else if (p.isNumber) {
if (example == null) {
example = "8.14";
}
example = StringUtils.appendIfMissingIgnoreCase(example, "D");
} else if (p.isBoolean) {
if (example == null) {
example = "true";
}
} else if (p.isBinary || p.isFile) {
if (example == null) {
example = "/path/to/file.txt";
}
example = "new System.IO.MemoryStream(System.IO.File.ReadAllBytes(\"" + escapeText(example) + "\"))";
} else if (p.isByteArray) {
if (example == null) {
example = "BYTE_ARRAY_DATA_HERE";
}
example = "System.Text.Encoding.ASCII.GetBytes(\"" + escapeText(example) + "\")";
} else if (p.isDate) {
if (example == null) {
example = "DateTime.Parse(\"2013-10-20\")";
} else {
example = "DateTime.Parse(\"" + example + "\")";
}
} else if (p.isDateTime) {
if (example == null) {
example = "DateTime.Parse(\"2013-10-20T19:20:30+01:00\")";
} else {
example = "DateTime.Parse(\"" + example + "\")";
}
} else if (p.isDecimal) {
if (example == null) {
example = "8.9M";
}
example = StringUtils.appendIfMissingIgnoreCase(example, "M");
} else if (p.isUuid) {
if (example == null) {
example = "\"38400000-8cf0-11bd-b23e-10b96e4ef00d\"";
} else {
example = "\"" + example + "\"";
}
} else if (p.isUri) {
if (example == null) {
example = "new Uri(\"https://openapi-generator.tech\")";
} else {
example = "new Uri(\"" + example + "\")";
}
} else if (hasAllowableValues) {
//parameter is enum defined as a schema component
example = "(" + type + ") \"" + example + "\"";
} else if (!languageSpecificPrimitives.contains(type)) {
// type is a model class, e.g. User
example = "new " + type + "()";
}

if (example == null) {
example = "null";
} else if (Boolean.TRUE.equals(p.isArray)) {
if (p.items.defaultValue != null) {
String innerExample;
if ("String".equals(p.items.dataType)) {
innerExample = "\"" + p.items.defaultValue + "\"";
} else {
innerExample = p.items.defaultValue;
}
example = "new List<" + p.items.dataType + ">({" + innerExample + "})";
} else {
example = "new List<" + p.items.dataType + ">()";
}
} else if (Boolean.TRUE.equals(p.isMap)) {
example = "new Dictionary<String, " + p.items.dataType + ">";
}

p.example = example;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ namespace Example
Configuration config = new Configuration();
config.BasePath = "http://localhost";
var apiInstance = new MultipartApi(config);
var file = BINARY_DATA_HERE; // System.IO.Stream | a file
var file = new System.IO.MemoryStream(System.IO.File.ReadAllBytes("/path/to/file.txt")); // System.IO.Stream | a file
var marker = new MultipartMixedMarker(); // MultipartMixedMarker | (optional)
try
Expand Down Expand Up @@ -174,7 +174,7 @@ namespace Example
Configuration config = new Configuration();
config.BasePath = "http://localhost";
var apiInstance = new MultipartApi(config);
var file = BINARY_DATA_HERE; // System.IO.Stream | One file (optional)
var file = new System.IO.MemoryStream(System.IO.File.ReadAllBytes("/path/to/file.txt")); // System.IO.Stream | One file (optional)
try
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ namespace Example
Configuration config = new Configuration();
config.BasePath = "http://petstore.swagger.io:80/v2";
var apiInstance = new FakeApi(config);
var body = 8.14; // decimal? | Input number as post body (optional)
var body = 8.14D; // decimal? | Input number as post body (optional)
try
{
Expand Down Expand Up @@ -321,7 +321,7 @@ namespace Example
Configuration config = new Configuration();
config.BasePath = "http://petstore.swagger.io:80/v2";
var apiInstance = new FakeApi(config);
var body = body_example; // string | Input string as post body (optional)
var body = "body_example"; // string | Input string as post body (optional)
try
{
Expand Down Expand Up @@ -523,7 +523,7 @@ namespace Example
Configuration config = new Configuration();
config.BasePath = "http://petstore.swagger.io:80/v2";
var apiInstance = new FakeApi(config);
var query = query_example; // string |
var query = "query_example"; // string |
var user = new User(); // User |
try
Expand Down Expand Up @@ -669,20 +669,20 @@ namespace Example
config.Password = "YOUR_PASSWORD";

var apiInstance = new FakeApi(config);
var number = 8.14; // decimal | None
var number = 8.14D; // decimal | None
var _double = 1.2D; // double | None
var patternWithoutDelimiter = patternWithoutDelimiter_example; // string | None
var _byte = BYTE_ARRAY_DATA_HERE; // byte[] | None
var patternWithoutDelimiter = "patternWithoutDelimiter_example"; // string | None
var _byte = System.Text.Encoding.ASCII.GetBytes("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 int64 = 789L; // long? | 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)
var dateTime = 2013-10-20T19:20:30+01:00; // DateTime? | None (optional) (default to "2010-02-01T10:20:10.111110+01:00")
var password = password_example; // string | None (optional)
var callback = callback_example; // string | None (optional)
var _string = "_string_example"; // string | None (optional)
var binary = new System.IO.MemoryStream(System.IO.File.ReadAllBytes("/path/to/file.txt")); // System.IO.Stream | None (optional)
var date = DateTime.Parse("2013-10-20"); // DateTime? | None (optional)
var dateTime = DateTime.Parse(""2010-02-01T10:20:10.111110+01:00""); // DateTime? | None (optional) (default to "2010-02-01T10:20:10.111110+01:00")
var password = "password_example"; // string | None (optional)
var callback = "callback_example"; // string | None (optional)
try
{
Expand Down Expand Up @@ -767,13 +767,13 @@ namespace Example
config.BasePath = "http://petstore.swagger.io:80/v2";
var apiInstance = new FakeApi(config);
var enumHeaderStringArray = new List<string>(); // List<string> | Header parameter enum test (string array) (optional)
var enumHeaderString = enumHeaderString_example; // string | Header parameter enum test (string) (optional) (default to -efg)
var enumHeaderString = "_abc"; // string | Header parameter enum test (string) (optional) (default to -efg)
var enumQueryStringArray = new List<string>(); // 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.2D; // double? | Query parameter enum test (double) (optional)
var enumQueryString = "_abc"; // string | Query parameter enum test (string) (optional) (default to -efg)
var enumQueryInteger = 1; // int? | Query parameter enum test (double) (optional)
var enumQueryDouble = 1.1D; // 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)
var enumFormString = "_abc"; // string | Form parameter enum test (string) (optional) (default to -efg)
try
{
Expand Down Expand Up @@ -856,10 +856,10 @@ namespace Example
var apiInstance = new FakeApi(config);
var requiredStringGroup = 56; // int | Required String in group parameters
var requiredBooleanGroup = true; // bool | Required Boolean in group parameters
var requiredInt64Group = 789; // long | Required Integer in group parameters
var requiredInt64Group = 789L; // long | Required Integer in group parameters
var stringGroup = 56; // int? | String in group parameters (optional)
var booleanGroup = true; // bool? | Boolean in group parameters (optional)
var int64Group = 789; // long? | Integer in group parameters (optional)
var int64Group = 789L; // long? | Integer in group parameters (optional)
try
{
Expand Down Expand Up @@ -1000,8 +1000,8 @@ namespace Example
Configuration config = new Configuration();
config.BasePath = "http://petstore.swagger.io:80/v2";
var apiInstance = new FakeApi(config);
var param = param_example; // string | field1
var param2 = param2_example; // string | field2
var param = "param_example"; // string | field1
var param2 = "param2_example"; // string | field2
try
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ namespace Example
config.AccessToken = "YOUR_ACCESS_TOKEN";

var apiInstance = new PetApi(config);
var petId = 789; // long | Pet id to delete
var apiKey = apiKey_example; // string | (optional)
var petId = 789L; // long | Pet id to delete
var apiKey = "apiKey_example"; // string | (optional)
try
{
Expand Down Expand Up @@ -339,7 +339,7 @@ namespace Example
// config.AddApiKeyPrefix("api_key", "Bearer");
var apiInstance = new PetApi(config);
var petId = 789; // long | ID of pet to return
var petId = 789L; // long | ID of pet to return
try
{
Expand Down Expand Up @@ -486,9 +486,9 @@ namespace Example
config.AccessToken = "YOUR_ACCESS_TOKEN";

var apiInstance = new PetApi(config);
var petId = 789; // long | ID of pet that needs to be updated
var name = name_example; // string | Updated name of the pet (optional)
var status = status_example; // string | Updated status of the pet (optional)
var petId = 789L; // long | ID of pet that needs to be updated
var name = "name_example"; // string | Updated name of the pet (optional)
var status = "status_example"; // string | Updated status of the pet (optional)
try
{
Expand Down Expand Up @@ -561,9 +561,9 @@ namespace Example
config.AccessToken = "YOUR_ACCESS_TOKEN";

var apiInstance = new PetApi(config);
var petId = 789; // long | ID of pet to update
var additionalMetadata = additionalMetadata_example; // string | Additional data to pass to server (optional)
var file = BINARY_DATA_HERE; // System.IO.Stream | file to upload (optional)
var petId = 789L; // long | ID of pet to update
var additionalMetadata = "additionalMetadata_example"; // string | Additional data to pass to server (optional)
var file = new System.IO.MemoryStream(System.IO.File.ReadAllBytes("/path/to/file.txt")); // System.IO.Stream | file to upload (optional)
try
{
Expand Down Expand Up @@ -637,9 +637,9 @@ namespace Example
config.AccessToken = "YOUR_ACCESS_TOKEN";

var apiInstance = new PetApi(config);
var petId = 789; // long | ID of pet to update
var requiredFile = BINARY_DATA_HERE; // System.IO.Stream | file to upload
var additionalMetadata = additionalMetadata_example; // string | Additional data to pass to server (optional)
var petId = 789L; // long | ID of pet to update
var requiredFile = new System.IO.MemoryStream(System.IO.File.ReadAllBytes("/path/to/file.txt")); // System.IO.Stream | file to upload
var additionalMetadata = "additionalMetadata_example"; // string | Additional data to pass to server (optional)
try
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ namespace Example
Configuration config = new Configuration();
config.BasePath = "http://petstore.swagger.io:80/v2";
var apiInstance = new StoreApi(config);
var orderId = orderId_example; // string | ID of the order that needs to be deleted
var orderId = "orderId_example"; // string | ID of the order that needs to be deleted
try
{
Expand Down Expand Up @@ -178,7 +178,7 @@ namespace Example
Configuration config = new Configuration();
config.BasePath = "http://petstore.swagger.io:80/v2";
var apiInstance = new StoreApi(config);
var orderId = 789; // long | ID of pet that needs to be fetched
var orderId = 789L; // long | ID of pet that needs to be fetched
try
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ namespace Example
Configuration config = new Configuration();
config.BasePath = "http://petstore.swagger.io:80/v2";
var apiInstance = new UserApi(config);
var username = username_example; // string | The name that needs to be deleted
var username = "username_example"; // string | The name that needs to be deleted
try
{
Expand Down Expand Up @@ -314,7 +314,7 @@ namespace Example
Configuration config = new Configuration();
config.BasePath = "http://petstore.swagger.io:80/v2";
var apiInstance = new UserApi(config);
var username = username_example; // string | The name that needs to be fetched. Use user1 for testing.
var username = "username_example"; // string | The name that needs to be fetched. Use user1 for testing.
try
{
Expand Down Expand Up @@ -385,8 +385,8 @@ namespace Example
Configuration config = new Configuration();
config.BasePath = "http://petstore.swagger.io:80/v2";
var apiInstance = new UserApi(config);
var username = username_example; // string | The user name for login
var password = password_example; // string | The password for login in clear text
var username = "username_example"; // string | The user name for login
var password = "password_example"; // string | The password for login in clear text
try
{
Expand Down Expand Up @@ -523,7 +523,7 @@ namespace Example
Configuration config = new Configuration();
config.BasePath = "http://petstore.swagger.io:80/v2";
var apiInstance = new UserApi(config);
var username = username_example; // string | name that need to be deleted
var username = "username_example"; // string | name that need to be deleted
var user = new User(); // User | Updated user object
try
Expand Down
Loading

0 comments on commit 2d927a7

Please sign in to comment.