Skip to content

Commit 52a112d

Browse files
stkrworkwing328
authored andcommitted
[C++][Restbed] Fix default value for Restbed (#1186)
* Start working on fixing default value in Restbed Server Api Template * fix default value in DefaultCodegen * Revert "fix default value in DefaultCodegen" This reverts commit ce69006. * fix default value in cpprest * update cpp restbed samples
1 parent e85c527 commit 52a112d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+123
-80
lines changed

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;
33+
import org.slf4j.LoggerFactory;
3234

3335
import io.swagger.v3.oas.models.media.*;
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

modules/openapi-generator/src/main/resources/cpp-restbed-server/api-source.mustache

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ void {{classname}}{{vendorExtensions.x-codegen-resourceName}}Resource::{{httpMet
7575
// Getting the path params
7676
{{#pathParams}}
7777
{{#isPrimitiveType}}
78-
const {{{dataType}}} {{{paramName}}} = request->get_path_parameter("{{paramName}}", {{#isString}}""{{/isString}}{{#isInteger}}0{{/isInteger}}{{#isLong}}0L{{/isLong}}{{#isFloat}}0.0f{{/isFloat}}{{#isDouble}}0.0{{/isDouble}});
78+
const {{{dataType}}} {{{paramName}}} = request->get_path_parameter("{{paramName}}", {{{defaultValue}}});
7979
{{/isPrimitiveType}}
8080
{{/pathParams}}
8181
{{/hasPathParams}}
@@ -84,7 +84,7 @@ void {{classname}}{{vendorExtensions.x-codegen-resourceName}}Resource::{{httpMet
8484
// Getting the query params
8585
{{#queryParams}}
8686
{{#isPrimitiveType}}
87-
const {{{dataType}}} {{{paramName}}} = request->get_query_parameter("{{paramName}}", {{#isString}}""{{/isString}}{{#isInteger}}0{{/isInteger}}{{#isLong}}0L{{/isLong}}{{#isFloat}}0.0f{{/isFloat}}{{#isDouble}}0.0{{/isDouble}});
87+
const {{{dataType}}} {{{paramName}}} = request->get_query_parameter("{{paramName}}", {{{defaultValue}}});
8888
{{/isPrimitiveType}}
8989
{{/queryParams}}
9090
{{/hasQueryParams}}
@@ -93,7 +93,7 @@ void {{classname}}{{vendorExtensions.x-codegen-resourceName}}Resource::{{httpMet
9393
// Getting the headers
9494
{{#headerParams}}
9595
{{#isPrimitiveType}}
96-
const {{{dataType}}} {{{paramName}}} = request->get_header("{{paramName}}", {{#isString}}""{{/isString}}{{#isInteger}}0{{/isInteger}}{{#isLong}}0L{{/isLong}}{{#isFloat}}0.0f{{/isFloat}}{{#isDouble}}0.0{{/isDouble}});
96+
const {{{dataType}}} {{{paramName}}} = request->get_header("{{paramName}}", {{{defaultValue}}});
9797
{{/isPrimitiveType}}
9898
{{/headerParams}}
9999
{{/hasHeaderParams}}
@@ -140,7 +140,7 @@ void {{classname}}{{vendorExtensions.x-codegen-resourceName}}Resource::{{httpMet
140140
// Getting the path params
141141
{{#pathParams}}
142142
{{#isPrimitiveType}}
143-
const {{dataType}} {{paramName}} = request->get_path_parameter("{{paramName}}", {{#isString}}""{{/isString}}{{#isInteger}}0{{/isInteger}}{{#isLong}}0L{{/isLong}}{{#isFloat}}0.0f{{/isFloat}}{{#isDouble}}0.0{{/isDouble}});
143+
const {{{dataType}}} {{{paramName}}} = request->get_path_parameter("{{paramName}}", {{{defaultValue}}});
144144
{{/isPrimitiveType}}
145145
{{/pathParams}}
146146
{{/hasPathParams}}
@@ -149,16 +149,16 @@ void {{classname}}{{vendorExtensions.x-codegen-resourceName}}Resource::{{httpMet
149149
// Getting the query params
150150
{{#queryParams}}
151151
{{#isPrimitiveType}}
152-
const {{dataType}} {{paramName}} = request->get_query_parameter("{{paramName}}", {{#isString}}""{{/isString}}{{#isInteger}}0{{/isInteger}}{{#isLong}}0L{{/isLong}}{{#isFloat}}0.0f{{/isFloat}}{{#isDouble}}0.0{{/isDouble}});
152+
const {{{dataType}}} {{{paramName}}} = request->get_query_parameter("{{paramName}}", {{{defaultValue}}});
153153
{{/isPrimitiveType}}
154154
{{/queryParams}}
155155
{{/hasQueryParams}}
156-
156+
157157
{{#hasHeaderParams}}
158158
// Getting the headers
159159
{{#headerParams}}
160160
{{#isPrimitiveType}}
161-
const {{dataType}} {{paramName}} = request->get_header("{{paramName}}", {{#isString}}""{{/isString}}{{#isInteger}}0{{/isInteger}}{{#isLong}}0L{{/isLong}}{{#isFloat}}0.0f{{/isFloat}}{{#isDouble}}0.0{{/isDouble}});
161+
const {{{dataType}}} {{{paramName}}} = request->get_header("{{paramName}}", {{{defaultValue}}});
162162
{{/isPrimitiveType}}
163163
{{/headerParams}}
164164
{{/hasHeaderParams}}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3.3.0-SNAPSHOT
1+
3.3.1-SNAPSHOT

samples/client/petstore/cpp-restsdk/ApiClient.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*
55
* OpenAPI spec version: 1.0.0
66
*
7-
* NOTE: This class is auto generated by OpenAPI-Generator 3.3.0-SNAPSHOT.
7+
* NOTE: This class is auto generated by OpenAPI-Generator 3.3.1-SNAPSHOT.
88
* https://openapi-generator.tech
99
* Do not edit the class manually.
1010
*/

samples/client/petstore/cpp-restsdk/ApiClient.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*
55
* OpenAPI spec version: 1.0.0
66
*
7-
* NOTE: This class is auto generated by OpenAPI-Generator 3.3.0-SNAPSHOT.
7+
* NOTE: This class is auto generated by OpenAPI-Generator 3.3.1-SNAPSHOT.
88
* https://openapi-generator.tech
99
* Do not edit the class manually.
1010
*/

samples/client/petstore/cpp-restsdk/ApiConfiguration.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*
55
* OpenAPI spec version: 1.0.0
66
*
7-
* NOTE: This class is auto generated by OpenAPI-Generator 3.3.0-SNAPSHOT.
7+
* NOTE: This class is auto generated by OpenAPI-Generator 3.3.1-SNAPSHOT.
88
* https://openapi-generator.tech
99
* Do not edit the class manually.
1010
*/

samples/client/petstore/cpp-restsdk/ApiConfiguration.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*
55
* OpenAPI spec version: 1.0.0
66
*
7-
* NOTE: This class is auto generated by OpenAPI-Generator 3.3.0-SNAPSHOT.
7+
* NOTE: This class is auto generated by OpenAPI-Generator 3.3.1-SNAPSHOT.
88
* https://openapi-generator.tech
99
* Do not edit the class manually.
1010
*/

samples/client/petstore/cpp-restsdk/ApiException.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*
55
* OpenAPI spec version: 1.0.0
66
*
7-
* NOTE: This class is auto generated by OpenAPI-Generator 3.3.0-SNAPSHOT.
7+
* NOTE: This class is auto generated by OpenAPI-Generator 3.3.1-SNAPSHOT.
88
* https://openapi-generator.tech
99
* Do not edit the class manually.
1010
*/

samples/client/petstore/cpp-restsdk/ApiException.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*
55
* OpenAPI spec version: 1.0.0
66
*
7-
* NOTE: This class is auto generated by OpenAPI-Generator 3.3.0-SNAPSHOT.
7+
* NOTE: This class is auto generated by OpenAPI-Generator 3.3.1-SNAPSHOT.
88
* https://openapi-generator.tech
99
* Do not edit the class manually.
1010
*/

samples/client/petstore/cpp-restsdk/HttpContent.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*
55
* OpenAPI spec version: 1.0.0
66
*
7-
* NOTE: This class is auto generated by OpenAPI-Generator 3.3.0-SNAPSHOT.
7+
* NOTE: This class is auto generated by OpenAPI-Generator 3.3.1-SNAPSHOT.
88
* https://openapi-generator.tech
99
* Do not edit the class manually.
1010
*/

0 commit comments

Comments
 (0)