[C++][Restbed] Fix default values for Restbed Server generator (#761)

* Start working on fixing default value in Restbed Server Api Template

* fix default value in DefaultCodegen
This commit is contained in:
stkrwork 2018-09-13 01:13:02 +02:00 committed by GitHub
parent 59ad12183f
commit f29ba97e8b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 69 additions and 27 deletions

View File

@ -2742,9 +2742,8 @@ public class DefaultCodegen implements CodegenConfig {
}
// set default value
if (parameterSchema.getDefault() != null) {
codegenParameter.defaultValue = toDefaultValue(parameterSchema);
}
codegenParameter.defaultValue = toDefaultValue(parameterSchema);
// TDOO revise collectionFormat
String collectionFormat = null;
if (ModelUtils.isArraySchema(parameterSchema)) { // for array parameter

View File

@ -25,15 +25,19 @@ import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Logger;
import org.apache.commons.lang3.StringUtils;
import org.openapitools.codegen.*;
import org.openapitools.codegen.utils.ModelUtils;
import io.swagger.v3.oas.models.media.*;
import org.slf4j.LoggerFactory;
public class CppRestbedServerCodegen extends AbstractCppCodegen {
private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(CppRestbedServerCodegen.class);
public static final String DECLSPEC = "declspec";
public static final String DEFAULT_INCLUDE = "defaultInclude";
@ -287,25 +291,63 @@ public class CppRestbedServerCodegen extends AbstractCppCodegen {
@Override
public String toDefaultValue(Schema p) {
if (ModelUtils.isStringSchema(p)) {
return "\"\"";
if (p.getDefault() != null) {
return "\"" + p.getDefault().toString() + "\"";
} else {
return "\"\"";
}
} else if (ModelUtils.isBooleanSchema(p)) {
return "false";
if (p.getDefault() != null) {
return p.getDefault().toString();
} else {
return "false";
}
} else if (ModelUtils.isDateSchema(p)) {
return "\"\"";
if (p.getDefault() != null) {
return "\"" + p.getDefault().toString() + "\"";
} else {
return "\"\"";
}
} else if (ModelUtils.isDateTimeSchema(p)) {
return "\"\"";
if (p.getDefault() != null) {
return "\"" + p.getDefault().toString() + "\"";
} else {
return "\"\"";
}
} else if (ModelUtils.isNumberSchema(p)) {
if (ModelUtils.isFloatSchema(p)) {
return "0.0f";
if (ModelUtils.isFloatSchema(p)) { // float
if (p.getDefault() != null) {
return p.getDefault().toString() + "f";
} else {
return "0.0f";
}
} else { // double
if (p.getDefault() != null) {
return p.getDefault().toString();
} else {
return "0.0";
}
}
return "0.0";
} else if (ModelUtils.isIntegerSchema(p)) {
if (ModelUtils.isLongSchema(p)) {
return "0L";
if (ModelUtils.isLongSchema(p)) { // long
if (p.getDefault() != null) {
return p.getDefault().toString() + "L";
} else {
return "0L";
}
} else { // integer
if (p.getDefault() != null) {
return p.getDefault().toString();
} else {
return "0";
}
}
return "0";
} else if (ModelUtils.isByteArraySchema(p)) {
return "\"\"";
if (p.getDefault() != null) {
return "\"" + p.getDefault().toString() + "\"";
} else {
return "\"\"";
}
} else if (ModelUtils.isMapSchema(p)) {
String inner = getSchemaType(ModelUtils.getAdditionalProperties(p));
return "std::map<std::string, " + inner + ">()";
@ -319,6 +361,7 @@ public class CppRestbedServerCodegen extends AbstractCppCodegen {
} else if (!StringUtils.isEmpty(p.get$ref())) {
return "new " + toModelName(ModelUtils.getSimpleRef(p.get$ref())) + "()";
}
return "nullptr";
}

View File

@ -75,7 +75,7 @@ void {{classname}}{{vendorExtensions.x-codegen-resourceName}}Resource::{{httpMet
// Getting the path params
{{#pathParams}}
{{#isPrimitiveType}}
const {{{dataType}}} {{{paramName}}} = request->get_path_parameter("{{paramName}}", {{#isString}}""{{/isString}}{{#isInteger}}0{{/isInteger}}{{#isLong}}0L{{/isLong}}{{#isFloat}}0.0f{{/isFloat}}{{#isDouble}}0.0{{/isDouble}});
const {{{dataType}}} {{{paramName}}} = request->get_path_parameter("{{paramName}}", {{{defaultValue}}});
{{/isPrimitiveType}}
{{/pathParams}}
{{/hasPathParams}}
@ -84,7 +84,7 @@ void {{classname}}{{vendorExtensions.x-codegen-resourceName}}Resource::{{httpMet
// Getting the query params
{{#queryParams}}
{{#isPrimitiveType}}
const {{{dataType}}} {{{paramName}}} = request->get_query_parameter("{{paramName}}", {{#isString}}""{{/isString}}{{#isInteger}}0{{/isInteger}}{{#isLong}}0L{{/isLong}}{{#isFloat}}0.0f{{/isFloat}}{{#isDouble}}0.0{{/isDouble}});
const {{{dataType}}} {{{paramName}}} = request->get_query_parameter("{{paramName}}", {{{defaultValue}}});
{{/isPrimitiveType}}
{{/queryParams}}
{{/hasQueryParams}}
@ -93,7 +93,7 @@ void {{classname}}{{vendorExtensions.x-codegen-resourceName}}Resource::{{httpMet
// Getting the headers
{{#headerParams}}
{{#isPrimitiveType}}
const {{{dataType}}} {{{paramName}}} = request->get_header("{{paramName}}", {{#isString}}""{{/isString}}{{#isInteger}}0{{/isInteger}}{{#isLong}}0L{{/isLong}}{{#isFloat}}0.0f{{/isFloat}}{{#isDouble}}0.0{{/isDouble}});
const {{{dataType}}} {{{paramName}}} = request->get_header("{{paramName}}", {{{defaultValue}}});
{{/isPrimitiveType}}
{{/headerParams}}
{{/hasHeaderParams}}
@ -140,7 +140,7 @@ void {{classname}}{{vendorExtensions.x-codegen-resourceName}}Resource::{{httpMet
// Getting the path params
{{#pathParams}}
{{#isPrimitiveType}}
const {{dataType}} {{paramName}} = request->get_path_parameter("{{paramName}}", {{#isString}}""{{/isString}}{{#isInteger}}0{{/isInteger}}{{#isLong}}0L{{/isLong}}{{#isFloat}}0.0f{{/isFloat}}{{#isDouble}}0.0{{/isDouble}});
const {{{dataType}}} {{{paramName}}} = request->get_path_parameter("{{paramName}}", {{{defaultValue}}});
{{/isPrimitiveType}}
{{/pathParams}}
{{/hasPathParams}}
@ -149,16 +149,16 @@ void {{classname}}{{vendorExtensions.x-codegen-resourceName}}Resource::{{httpMet
// Getting the query params
{{#queryParams}}
{{#isPrimitiveType}}
const {{dataType}} {{paramName}} = request->get_query_parameter("{{paramName}}", {{#isString}}""{{/isString}}{{#isInteger}}0{{/isInteger}}{{#isLong}}0L{{/isLong}}{{#isFloat}}0.0f{{/isFloat}}{{#isDouble}}0.0{{/isDouble}});
const {{{dataType}}} {{{paramName}}} = request->get_query_parameter("{{paramName}}", {{{defaultValue}}});
{{/isPrimitiveType}}
{{/queryParams}}
{{/hasQueryParams}}
{{#hasHeaderParams}}
// Getting the headers
{{#headerParams}}
{{#isPrimitiveType}}
const {{dataType}} {{paramName}} = request->get_header("{{paramName}}", {{#isString}}""{{/isString}}{{#isInteger}}0{{/isInteger}}{{#isLong}}0L{{/isLong}}{{#isFloat}}0.0f{{/isFloat}}{{#isDouble}}0.0{{/isDouble}});
const {{{dataType}}} {{{paramName}}} = request->get_header("{{paramName}}", {{{defaultValue}}});
{{/isPrimitiveType}}
{{/headerParams}}
{{/hasHeaderParams}}

View File

@ -118,7 +118,7 @@ void PetApiPetResource::PUT_method_handler(const std::shared_ptr<restbed::Sessio
std::string requestBody = restbed::String::format("%.*s\n", ( int ) body.size( ), body.data( ));
// Change the value of this variable to the appropriate response before sending the response
int status_code = 200;
@ -194,7 +194,7 @@ void PetApiPetPetIdResource::GET_method_handler(const std::shared_ptr<restbed::S
// Getting the path params
const int64_t petId = request->get_path_parameter("petId", 0L);
// Change the value of this variable to the appropriate response before sending the response
int status_code = 200;
@ -225,7 +225,7 @@ void PetApiPetPetIdResource::POST_method_handler(const std::shared_ptr<restbed::
// Getting the path params
const int64_t petId = request->get_path_parameter("petId", 0L);
// Change the value of this variable to the appropriate response before sending the response
int status_code = 200;

View File

@ -100,7 +100,7 @@ void StoreApiStoreOrderOrderIdResource::GET_method_handler(const std::shared_ptr
// Getting the path params
const int64_t orderId = request->get_path_parameter("orderId", 0L);
// Change the value of this variable to the appropriate response before sending the response
int status_code = 200;

View File

@ -253,7 +253,7 @@ void UserApiUserUsernameResource::GET_method_handler(const std::shared_ptr<restb
// Getting the path params
const std::string username = request->get_path_parameter("username", "");
// Change the value of this variable to the appropriate response before sending the response
int status_code = 200;
@ -292,7 +292,7 @@ void UserApiUserUsernameResource::PUT_method_handler(const std::shared_ptr<restb
// Getting the path params
const std::string username = request->get_path_parameter("username", "");
// Change the value of this variable to the appropriate response before sending the response
int status_code = 200;