Skip to content

Commit

Permalink
fix default value in DefaultCodegen
Browse files Browse the repository at this point in the history
  • Loading branch information
wing328 authored and stkrwork committed Sep 10, 2018
1 parent 95bce7b commit ce69006
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2742,9 +2742,8 @@ public CodegenParameter fromParameter(Parameter parameter, Set<String> imports)
}

// 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,19 @@
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";

Expand Down Expand Up @@ -287,25 +291,63 @@ public String getTypeDeclaration(Schema p) {
@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 + ">()";
Expand All @@ -319,6 +361,7 @@ public String toDefaultValue(Schema p) {
} else if (!StringUtils.isEmpty(p.get$ref())) {
return "new " + toModelName(ModelUtils.getSimpleRef(p.get$ref())) + "()";
}

return "nullptr";
}

Expand Down
6 changes: 3 additions & 3 deletions samples/server/petstore/cpp-restbed/api/PetApi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion samples/server/petstore/cpp-restbed/api/StoreApi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions samples/server/petstore/cpp-restbed/api/UserApi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit ce69006

Please sign in to comment.