Skip to content

Commit ce69006

Browse files
wing328stkrwork
authored andcommitted
fix default value in DefaultCodegen
1 parent 95bce7b commit ce69006

File tree

5 files changed

+62
-20
lines changed

5 files changed

+62
-20
lines changed

modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2742,9 +2742,8 @@ public CodegenParameter fromParameter(Parameter parameter, Set<String> imports)
27422742
}
27432743

27442744
// set default value
2745-
if (parameterSchema.getDefault() != null) {
2746-
codegenParameter.defaultValue = toDefaultValue(parameterSchema);
2747-
}
2745+
codegenParameter.defaultValue = toDefaultValue(parameterSchema);
2746+
27482747
// TDOO revise collectionFormat
27492748
String collectionFormat = null;
27502749
if (ModelUtils.isArraySchema(parameterSchema)) { // for array parameter

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CppRestbedServerCodegen.java

Lines changed: 54 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,19 @@
2525
import java.util.List;
2626
import java.util.Map;
2727
import java.util.Set;
28+
import java.util.logging.Logger;
2829

2930
import org.apache.commons.lang3.StringUtils;
3031
import org.openapitools.codegen.*;
3132
import org.openapitools.codegen.utils.ModelUtils;
3233

3334
import io.swagger.v3.oas.models.media.*;
35+
import org.slf4j.LoggerFactory;
3436

3537
public class CppRestbedServerCodegen extends AbstractCppCodegen {
3638

39+
private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(CppRestbedServerCodegen.class);
40+
3741
public static final String DECLSPEC = "declspec";
3842
public static final String DEFAULT_INCLUDE = "defaultInclude";
3943

@@ -287,25 +291,63 @@ public String getTypeDeclaration(Schema p) {
287291
@Override
288292
public String toDefaultValue(Schema p) {
289293
if (ModelUtils.isStringSchema(p)) {
290-
return "\"\"";
294+
if (p.getDefault() != null) {
295+
return "\"" + p.getDefault().toString() + "\"";
296+
} else {
297+
return "\"\"";
298+
}
291299
} else if (ModelUtils.isBooleanSchema(p)) {
292-
return "false";
300+
if (p.getDefault() != null) {
301+
return p.getDefault().toString();
302+
} else {
303+
return "false";
304+
}
293305
} else if (ModelUtils.isDateSchema(p)) {
294-
return "\"\"";
306+
if (p.getDefault() != null) {
307+
return "\"" + p.getDefault().toString() + "\"";
308+
} else {
309+
return "\"\"";
310+
}
295311
} else if (ModelUtils.isDateTimeSchema(p)) {
296-
return "\"\"";
312+
if (p.getDefault() != null) {
313+
return "\"" + p.getDefault().toString() + "\"";
314+
} else {
315+
return "\"\"";
316+
}
297317
} else if (ModelUtils.isNumberSchema(p)) {
298-
if (ModelUtils.isFloatSchema(p)) {
299-
return "0.0f";
318+
if (ModelUtils.isFloatSchema(p)) { // float
319+
if (p.getDefault() != null) {
320+
return p.getDefault().toString() + "f";
321+
} else {
322+
return "0.0f";
323+
}
324+
} else { // double
325+
if (p.getDefault() != null) {
326+
return p.getDefault().toString();
327+
} else {
328+
return "0.0";
329+
}
300330
}
301-
return "0.0";
302331
} else if (ModelUtils.isIntegerSchema(p)) {
303-
if (ModelUtils.isLongSchema(p)) {
304-
return "0L";
332+
if (ModelUtils.isLongSchema(p)) { // long
333+
if (p.getDefault() != null) {
334+
return p.getDefault().toString() + "L";
335+
} else {
336+
return "0L";
337+
}
338+
} else { // integer
339+
if (p.getDefault() != null) {
340+
return p.getDefault().toString();
341+
} else {
342+
return "0";
343+
}
305344
}
306-
return "0";
307345
} else if (ModelUtils.isByteArraySchema(p)) {
308-
return "\"\"";
346+
if (p.getDefault() != null) {
347+
return "\"" + p.getDefault().toString() + "\"";
348+
} else {
349+
return "\"\"";
350+
}
309351
} else if (ModelUtils.isMapSchema(p)) {
310352
String inner = getSchemaType(ModelUtils.getAdditionalProperties(p));
311353
return "std::map<std::string, " + inner + ">()";
@@ -319,6 +361,7 @@ public String toDefaultValue(Schema p) {
319361
} else if (!StringUtils.isEmpty(p.get$ref())) {
320362
return "new " + toModelName(ModelUtils.getSimpleRef(p.get$ref())) + "()";
321363
}
364+
322365
return "nullptr";
323366
}
324367

samples/server/petstore/cpp-restbed/api/PetApi.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ void PetApiPetResource::PUT_method_handler(const std::shared_ptr<restbed::Sessio
118118
std::string requestBody = restbed::String::format("%.*s\n", ( int ) body.size( ), body.data( ));
119119

120120

121-
121+
122122

123123
// Change the value of this variable to the appropriate response before sending the response
124124
int status_code = 200;
@@ -194,7 +194,7 @@ void PetApiPetPetIdResource::GET_method_handler(const std::shared_ptr<restbed::S
194194
// Getting the path params
195195
const int64_t petId = request->get_path_parameter("petId", 0L);
196196

197-
197+
198198

199199
// Change the value of this variable to the appropriate response before sending the response
200200
int status_code = 200;
@@ -225,7 +225,7 @@ void PetApiPetPetIdResource::POST_method_handler(const std::shared_ptr<restbed::
225225
// Getting the path params
226226
const int64_t petId = request->get_path_parameter("petId", 0L);
227227

228-
228+
229229

230230
// Change the value of this variable to the appropriate response before sending the response
231231
int status_code = 200;

samples/server/petstore/cpp-restbed/api/StoreApi.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ void StoreApiStoreOrderOrderIdResource::GET_method_handler(const std::shared_ptr
100100
// Getting the path params
101101
const int64_t orderId = request->get_path_parameter("orderId", 0L);
102102

103-
103+
104104

105105
// Change the value of this variable to the appropriate response before sending the response
106106
int status_code = 200;

samples/server/petstore/cpp-restbed/api/UserApi.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ void UserApiUserUsernameResource::GET_method_handler(const std::shared_ptr<restb
253253
// Getting the path params
254254
const std::string username = request->get_path_parameter("username", "");
255255

256-
256+
257257

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

295-
295+
296296

297297
// Change the value of this variable to the appropriate response before sending the response
298298
int status_code = 200;

0 commit comments

Comments
 (0)