From 4d34072808f91c7e6bf5116ff8301d0bcf5c8b1f Mon Sep 17 00:00:00 2001 From: "Mateusz Szychowski (Muttley)" Date: Fri, 13 Sep 2019 14:26:06 +0200 Subject: [PATCH 1/5] [C++][Pistache] Simplified model template --- .../languages/CppPistacheServerCodegen.java | 28 +++++++++-- .../model-struct-header.mustache | 40 +++++++++++++++ .../model-struct-source.mustache | 49 +++++++++++++++++++ 3 files changed, 114 insertions(+), 3 deletions(-) create mode 100644 modules/openapi-generator/src/main/resources/cpp-pistache-server/model-struct-header.mustache create mode 100644 modules/openapi-generator/src/main/resources/cpp-pistache-server/model-struct-source.mustache 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 cf9d78ed5dd9..3de0cbf5b91e 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 @@ -27,6 +27,8 @@ import org.openapitools.codegen.*; import org.openapitools.codegen.utils.ModelUtils; import org.openapitools.codegen.utils.URLPathUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.io.File; import java.util.*; @@ -35,10 +37,15 @@ import static org.openapitools.codegen.utils.StringUtils.*; public class CppPistacheServerCodegen extends AbstractCppCodegen { + private static final Logger LOGGER = LoggerFactory.getLogger(CppPistacheServerCodegen.class); + protected String implFolder = "impl"; protected boolean isAddExternalLibs = true; + protected boolean isUseStructModel = false; public static final String OPTIONAL_EXTERNAL_LIB = "addExternalLibs"; public static final String OPTIONAL_EXTERNAL_LIB_DESC = "Add the Possibility to fetch and compile external Libraries needed by this Framework."; + public static final String OPTION_USE_STRUCT_MODEL = "useStructModel"; + public static final String OPTION_USE_STRUCT_MODEL_DESC = "Use struct-based model template instead of get/set-based model template"; public static final String HELPERS_PACKAGE_NAME = "helpersPackage"; public static final String HELPERS_PACKAGE_NAME_DESC = "Specify the package name to be used for the helpers (e.g. org.openapitools.server.helpers)."; protected final String PREFIX = ""; @@ -68,9 +75,6 @@ public CppPistacheServerCodegen() { apiPackage = "org.openapitools.server.api"; modelPackage = "org.openapitools.server.model"; - modelTemplateFiles.put("model-header.mustache", ".h"); - modelTemplateFiles.put("model-source.mustache", ".cpp"); - apiTemplateFiles.put("api-header.mustache", ".h"); apiTemplateFiles.put("api-source.mustache", ".cpp"); apiTemplateFiles.put("api-impl-header.mustache", ".h"); @@ -81,6 +85,7 @@ public CppPistacheServerCodegen() { cliOptions.clear(); addSwitch(OPTIONAL_EXTERNAL_LIB, OPTIONAL_EXTERNAL_LIB_DESC, this.isAddExternalLibs); addOption(HELPERS_PACKAGE_NAME, HELPERS_PACKAGE_NAME_DESC, this.helpersPackage); + addSwitch(OPTION_USE_STRUCT_MODEL, OPTION_USE_STRUCT_MODEL_DESC, this.isUseStructModel); reservedWords = new HashSet<>(); @@ -144,6 +149,23 @@ public void processOpts() { } else { additionalProperties.put(OPTIONAL_EXTERNAL_LIB, isAddExternalLibs); } + + setupModelTemplate(); + } + + private void setupModelTemplate() { + if (additionalProperties.containsKey(OPTION_USE_STRUCT_MODEL)) + isUseStructModel = convertPropertyToBooleanAndWriteBack(OPTION_USE_STRUCT_MODEL); + + if (isUseStructModel) { + LOGGER.info("Using struct-based model template"); + modelTemplateFiles.put("model-struct-header.mustache", ".h"); + modelTemplateFiles.put("model-struct-source.mustache", ".cpp"); + } else { + LOGGER.info("Using get/set-based model template"); + modelTemplateFiles.put("model-header.mustache", ".h"); + modelTemplateFiles.put("model-source.mustache", ".cpp"); + } } @Override diff --git a/modules/openapi-generator/src/main/resources/cpp-pistache-server/model-struct-header.mustache b/modules/openapi-generator/src/main/resources/cpp-pistache-server/model-struct-header.mustache new file mode 100644 index 000000000000..a49415db26de --- /dev/null +++ b/modules/openapi-generator/src/main/resources/cpp-pistache-server/model-struct-header.mustache @@ -0,0 +1,40 @@ +{{>licenseInfo}} +{{#models}}{{#model}}/* + * {{classname}}.h + * + * {{description}} + */ + +#ifndef {{classname}}_H_ +#define {{classname}}_H_ + +{{{defaultInclude}}} +{{#imports}}{{{this}}} +{{/imports}} +#include +#include + +{{#modelNamespaceDeclarations}} +namespace {{this}} { +{{/modelNamespaceDeclarations}} + +struct {{classname}} +{ + {{#vars}} + {{^required}}Pistache::Optional<{{/required}}{{{dataType}}}{{^required}}>{{/required}} {{baseName}}; + {{/vars}} + + nlohmann::json to_json() const; + static {{classname}} from_json(const nlohmann::json& j); +}; + +void to_json(nlohmann::json& j, const {{classname}}& o); +void from_json(const nlohmann::json& j, {{classname}}& o); + +{{#modelNamespaceDeclarations}} +} // {{this}} +{{/modelNamespaceDeclarations}} + +#endif /* {{classname}}_H_ */ +{{/model}} +{{/models}} diff --git a/modules/openapi-generator/src/main/resources/cpp-pistache-server/model-struct-source.mustache b/modules/openapi-generator/src/main/resources/cpp-pistache-server/model-struct-source.mustache new file mode 100644 index 000000000000..2e4f114cc73c --- /dev/null +++ b/modules/openapi-generator/src/main/resources/cpp-pistache-server/model-struct-source.mustache @@ -0,0 +1,49 @@ +{{>licenseInfo}} +{{#models}}{{#model}} + +#include "{{classname}}.h" + +{{#modelNamespaceDeclarations}} +namespace {{this}} { +{{/modelNamespaceDeclarations}} + +nlohmann::json {{classname}}::to_json() const +{ + nlohmann::json j; + {{#modelNamespaceDeclarations}}::{{this}}{{/modelNamespaceDeclarations}}::to_json(j, *this); + return j; +} + +{{classname}} {{classname}}::from_json(const nlohmann::json& j) +{ + {{classname}} o{}; + {{#modelNamespaceDeclarations}}::{{this}}{{/modelNamespaceDeclarations}}::from_json(j, o); + return o; +} + +void to_json(nlohmann::json& j, const {{classname}}& o) +{ + {{#vars}} + {{^required}}if (!o.{{baseName}}.isEmpty()){{/required}} + j["{{baseName}}"] = o.{{baseName}}{{^required}}.get(){{/required}}; + {{/vars}} +} + +void from_json(const nlohmann::json& j, {{classname}}& o) +{ + {{#vars}} + {{#required}}j.at("{{baseName}}").get_to(o.{{baseName}});{{/required}} + {{^required}}if (j.find("{{baseName}}") != j.end()) { + {{{dataType}}} temporary_{{baseName}}; + j.at("{{baseName}}").get_to(temporary_{{baseName}}); + o.{{baseName}} = Pistache::Some(temporary_{{baseName}}); + }{{/required}} + {{/vars}} +} + +{{#modelNamespaceDeclarations}} +} // {{this}} +{{/modelNamespaceDeclarations}} + +{{/model}} +{{/models}} From 66501287d859abde6ebcfb697dbc53450c35ff92 Mon Sep 17 00:00:00 2001 From: "Mateusz Szychowski (Muttley)" Date: Fri, 20 Sep 2019 13:44:43 +0200 Subject: [PATCH 2/5] [C++][Pistache] Fix for addExternalLibs option CMake would fail with addExternalLibs set to false since it'd try to add depenency to not-existing targets --- .../src/main/resources/cpp-pistache-server/cmake.mustache | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/openapi-generator/src/main/resources/cpp-pistache-server/cmake.mustache b/modules/openapi-generator/src/main/resources/cpp-pistache-server/cmake.mustache index a822ae28ae59..2ad6f17868fd 100644 --- a/modules/openapi-generator/src/main/resources/cpp-pistache-server/cmake.mustache +++ b/modules/openapi-generator/src/main/resources/cpp-pistache-server/cmake.mustache @@ -35,5 +35,5 @@ file(GLOB SRCS ) add_executable(${PROJECT_NAME} ${SRCS} ) -add_dependencies(${PROJECT_NAME} PISTACHE NLOHMANN) +{{#addExternalLibs}}add_dependencies(${PROJECT_NAME} PISTACHE NLOHMANN){{/addExternalLibs}} target_link_libraries(${PROJECT_NAME} pistache pthread) From 89496d30ede209aed0afdb8da8589cc4bfeec47a Mon Sep 17 00:00:00 2001 From: "Mateusz Szychowski (Muttley)" Date: Fri, 20 Sep 2019 13:45:02 +0200 Subject: [PATCH 3/5] [C++][Pistache] Update cpp-pistache-server-petstore.sh --- bin/cpp-pistache-server-petstore.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/cpp-pistache-server-petstore.sh b/bin/cpp-pistache-server-petstore.sh index d0273a9d76c0..7f2c734b6894 100755 --- a/bin/cpp-pistache-server-petstore.sh +++ b/bin/cpp-pistache-server-petstore.sh @@ -27,6 +27,6 @@ fi # if you've executed sbt assembly previously it will use that instead. export JAVA_OPTS="${JAVA_OPTS} -Xmx1024M -DloggerPath=conf/log4j.properties" -ags="generate -g cpp-pistache-server -t modules/openapi-generator/src/main/resources/cpp-pistache-server -i modules/openapi-generator/src/test/resources/2_0/petstore.yaml --additional-properties addExternalLibs=true -o samples/server/petstore/cpp-pistache $@" +ags="generate -g cpp-pistache-server -t modules/openapi-generator/src/main/resources/cpp-pistache-server -i modules/openapi-generator/src/test/resources/2_0/petstore.yaml --additional-properties addExternalLibs=true --additional-properties useStructModel=false -o samples/server/petstore/cpp-pistache $@" java $JAVA_OPTS -jar $executable $ags From 9f64f8d1057ae914e98d1d8023cc412008368e97 Mon Sep 17 00:00:00 2001 From: "Mateusz Szychowski (Muttley)" Date: Fri, 20 Sep 2019 13:45:13 +0200 Subject: [PATCH 4/5] [C++][Pistache] Update Petstore sample --- samples/server/petstore/cpp-pistache/.openapi-generator/VERSION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/server/petstore/cpp-pistache/.openapi-generator/VERSION b/samples/server/petstore/cpp-pistache/.openapi-generator/VERSION index d1a8f58b3884..0e97bd19efbf 100644 --- a/samples/server/petstore/cpp-pistache/.openapi-generator/VERSION +++ b/samples/server/petstore/cpp-pistache/.openapi-generator/VERSION @@ -1 +1 @@ -4.1.2-SNAPSHOT \ No newline at end of file +4.1.3-SNAPSHOT \ No newline at end of file From 10690f5286c3a9446844e0b489b5c763aefe6dd2 Mon Sep 17 00:00:00 2001 From: "Mateusz Szychowski (Muttley)" Date: Fri, 20 Sep 2019 15:55:11 +0200 Subject: [PATCH 5/5] [C++][Pistache] Update documentation --- docs/generators/cpp-pistache-server.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/generators/cpp-pistache-server.md b/docs/generators/cpp-pistache-server.md index ac4abee691e6..2ffeb59cc7db 100644 --- a/docs/generators/cpp-pistache-server.md +++ b/docs/generators/cpp-pistache-server.md @@ -9,3 +9,4 @@ sidebar_label: cpp-pistache-server | ------ | ----------- | ------ | ------- | |addExternalLibs|Add the Possibility to fetch and compile external Libraries needed by this Framework.| |true| |helpersPackage|Specify the package name to be used for the helpers (e.g. org.openapitools.server.helpers).| |org.openapitools.server.helpers| +|useStructModel|Use struct-based model template instead of get/set-based model template| |false|