diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CppPistacheServerCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CppPistacheServerCodegen.java index 18cfb324f9a..0aa6cb25685 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CppPistacheServerCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CppPistacheServerCodegen.java @@ -381,42 +381,79 @@ public class CppPistacheServerCodegen extends AbstractCppCodegen { @Override public String toDefaultValue(Schema p) { - if (ModelUtils.isBooleanSchema(p)) { - return "false"; + if (ModelUtils.isStringSchema(p)) { + if (p.getDefault() != null) { + return "\"" + p.getDefault().toString() + "\""; + } else { + return "\"\""; + } + } else if (ModelUtils.isBooleanSchema(p)) { + 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(getAdditionalProperties(p)); return "std::map()"; } else if (ModelUtils.isArraySchema(p)) { ArraySchema ap = (ArraySchema) p; String inner = getSchemaType(ap.getItems()); - return "std::vector<" + inner + ">()"; - } else if (!StringUtils.isEmpty(p.get$ref())) { // model - return toModelName(ModelUtils.getSimpleRef(p.get$ref())) + "()"; - } else if (ModelUtils.isStringSchema(p)) { - if (p.getDefault() == null) { - return "\"\""; - } else { - return "\"" + p.getDefault().toString() + "\""; + if (!languageSpecificPrimitives.contains(inner)) { + inner = "std::shared_ptr<" + inner + ">"; } + return "std::vector<" + inner + ">()"; + } else if (!StringUtils.isEmpty(p.get$ref())) { + return "std::make_shared<" + toModelName(ModelUtils.getSimpleRef(p.get$ref())) + ">()"; } - return ""; + return "nullptr"; } /** diff --git a/modules/openapi-generator/src/main/resources/cpp-pistache-server/api-source.mustache b/modules/openapi-generator/src/main/resources/cpp-pistache-server/api-source.mustache index dad2eb75c6c..7418caa9cdd 100644 --- a/modules/openapi-generator/src/main/resources/cpp-pistache-server/api-source.mustache +++ b/modules/openapi-generator/src/main/resources/cpp-pistache-server/api-source.mustache @@ -42,7 +42,7 @@ std::pair {{classname}}::handleParsingExcepti } catch ({{helpersNamespace}}::ValidationException &e) { return std::make_pair(Pistache::Http::Code::Bad_Request, e.what()); } catch (std::exception &e) { - return std::make_pair(Pistache::Http::Code::Internal_Server_Error, e.what()) + return std::make_pair(Pistache::Http::Code::Internal_Server_Error, e.what()); } } diff --git a/samples/server/petstore/cpp-pistache/api/PetApi.cpp b/samples/server/petstore/cpp-pistache/api/PetApi.cpp index a05048ec790..c3b19e937b7 100644 --- a/samples/server/petstore/cpp-pistache/api/PetApi.cpp +++ b/samples/server/petstore/cpp-pistache/api/PetApi.cpp @@ -55,7 +55,7 @@ std::pair PetApi::handleParsingException(cons } catch (org::openapitools::server::helpers::ValidationException &e) { return std::make_pair(Pistache::Http::Code::Bad_Request, e.what()); } catch (std::exception &e) { - return std::make_pair(Pistache::Http::Code::Internal_Server_Error, e.what()) + return std::make_pair(Pistache::Http::Code::Internal_Server_Error, e.what()); } } diff --git a/samples/server/petstore/cpp-pistache/api/StoreApi.cpp b/samples/server/petstore/cpp-pistache/api/StoreApi.cpp index 480bdcc0334..11310ecff12 100644 --- a/samples/server/petstore/cpp-pistache/api/StoreApi.cpp +++ b/samples/server/petstore/cpp-pistache/api/StoreApi.cpp @@ -51,7 +51,7 @@ std::pair StoreApi::handleParsingException(co } catch (org::openapitools::server::helpers::ValidationException &e) { return std::make_pair(Pistache::Http::Code::Bad_Request, e.what()); } catch (std::exception &e) { - return std::make_pair(Pistache::Http::Code::Internal_Server_Error, e.what()) + return std::make_pair(Pistache::Http::Code::Internal_Server_Error, e.what()); } } diff --git a/samples/server/petstore/cpp-pistache/api/UserApi.cpp b/samples/server/petstore/cpp-pistache/api/UserApi.cpp index 7b67dd0846e..2aff5aa9dda 100644 --- a/samples/server/petstore/cpp-pistache/api/UserApi.cpp +++ b/samples/server/petstore/cpp-pistache/api/UserApi.cpp @@ -55,7 +55,7 @@ std::pair UserApi::handleParsingException(con } catch (org::openapitools::server::helpers::ValidationException &e) { return std::make_pair(Pistache::Http::Code::Bad_Request, e.what()); } catch (std::exception &e) { - return std::make_pair(Pistache::Http::Code::Internal_Server_Error, e.what()) + return std::make_pair(Pistache::Http::Code::Internal_Server_Error, e.what()); } }