From 8a7472fd1ccf035b486902916d1c0b0f814e9d91 Mon Sep 17 00:00:00 2001 From: William Cheng Date: Mon, 9 Nov 2020 18:19:23 +0800 Subject: [PATCH] add cpp-oatpp-server generator (alpha) --- bin/configs/other/cpp-oatpp-server.yaml | 4 + .../languages/CppOatppServerCodegen.java | 438 ++++++++++++++++++ .../org.openapitools.codegen.CodegenConfig | 1 + .../cpp-oatpp-server/README.mustache | 48 ++ .../cpp-oatpp-server/api-header.mustache | 72 +++ .../cpp-oatpp-server/api-impl-header.mustache | 55 +++ .../cpp-oatpp-server/api-impl-source.mustache | 34 ++ .../cpp-oatpp-server/api-source.mustache | 111 +++++ .../resources/cpp-oatpp-server/cmake.mustache | 39 ++ .../cpp-oatpp-server/helpers-header.mustache | 64 +++ .../cpp-oatpp-server/helpers-source.mustache | 86 ++++ .../cpp-oatpp-server/licenseInfo.mustache | 11 + .../cpp-oatpp-server/main-api-server.mustache | 79 ++++ .../cpp-oatpp-server/model-header.mustache | 60 +++ .../cpp-oatpp-server/model-source.mustache | 73 +++ .../model-struct-header.mustache | 43 ++ .../model-struct-source.mustache | 59 +++ .../cpp-oatpp/.openapi-generator-ignore | 23 + .../cpp-oatpp/.openapi-generator/FILES | 33 ++ .../cpp-oatpp/.openapi-generator/VERSION | 1 + .../server/petstore/cpp-oatpp/CMakeLists.txt | 37 ++ samples/server/petstore/cpp-oatpp/README.md | 48 ++ .../server/petstore/cpp-oatpp/api/PetApi.cpp | 236 ++++++++++ .../server/petstore/cpp-oatpp/api/PetApi.h | 141 ++++++ .../petstore/cpp-oatpp/api/StoreApi.cpp | 134 ++++++ .../server/petstore/cpp-oatpp/api/StoreApi.h | 101 ++++ .../server/petstore/cpp-oatpp/api/UserApi.cpp | 243 ++++++++++ .../server/petstore/cpp-oatpp/api/UserApi.h | 143 ++++++ .../petstore/cpp-oatpp/impl/PetApiImpl.cpp | 55 +++ .../petstore/cpp-oatpp/impl/PetApiImpl.h | 66 +++ .../petstore/cpp-oatpp/impl/StoreApiImpl.cpp | 43 ++ .../petstore/cpp-oatpp/impl/StoreApiImpl.h | 62 +++ .../petstore/cpp-oatpp/impl/UserApiImpl.cpp | 55 +++ .../petstore/cpp-oatpp/impl/UserApiImpl.h | 66 +++ .../petstore/cpp-oatpp/main-api-server.cpp | 95 ++++ .../petstore/cpp-oatpp/model/ApiResponse.cpp | 127 +++++ .../petstore/cpp-oatpp/model/ApiResponse.h | 82 ++++ .../petstore/cpp-oatpp/model/Category.cpp | 101 ++++ .../petstore/cpp-oatpp/model/Category.h | 73 +++ .../petstore/cpp-oatpp/model/Helpers.cpp | 98 ++++ .../server/petstore/cpp-oatpp/model/Helpers.h | 76 +++ .../cpp-oatpp/model/Inline_object.cpp | 101 ++++ .../petstore/cpp-oatpp/model/Inline_object.h | 73 +++ .../cpp-oatpp/model/Inline_object_1.cpp | 100 ++++ .../cpp-oatpp/model/Inline_object_1.h | 73 +++ .../server/petstore/cpp-oatpp/model/Order.cpp | 205 ++++++++ .../server/petstore/cpp-oatpp/model/Order.h | 109 +++++ .../server/petstore/cpp-oatpp/model/Pet.cpp | 172 +++++++ samples/server/petstore/cpp-oatpp/model/Pet.h | 108 +++++ .../server/petstore/cpp-oatpp/model/Tag.cpp | 101 ++++ samples/server/petstore/cpp-oatpp/model/Tag.h | 73 +++ .../server/petstore/cpp-oatpp/model/User.cpp | 257 ++++++++++ .../server/petstore/cpp-oatpp/model/User.h | 127 +++++ 53 files changed, 4915 insertions(+) create mode 100644 bin/configs/other/cpp-oatpp-server.yaml create mode 100644 modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CppOatppServerCodegen.java create mode 100644 modules/openapi-generator/src/main/resources/cpp-oatpp-server/README.mustache create mode 100644 modules/openapi-generator/src/main/resources/cpp-oatpp-server/api-header.mustache create mode 100644 modules/openapi-generator/src/main/resources/cpp-oatpp-server/api-impl-header.mustache create mode 100644 modules/openapi-generator/src/main/resources/cpp-oatpp-server/api-impl-source.mustache create mode 100644 modules/openapi-generator/src/main/resources/cpp-oatpp-server/api-source.mustache create mode 100644 modules/openapi-generator/src/main/resources/cpp-oatpp-server/cmake.mustache create mode 100644 modules/openapi-generator/src/main/resources/cpp-oatpp-server/helpers-header.mustache create mode 100644 modules/openapi-generator/src/main/resources/cpp-oatpp-server/helpers-source.mustache create mode 100644 modules/openapi-generator/src/main/resources/cpp-oatpp-server/licenseInfo.mustache create mode 100644 modules/openapi-generator/src/main/resources/cpp-oatpp-server/main-api-server.mustache create mode 100644 modules/openapi-generator/src/main/resources/cpp-oatpp-server/model-header.mustache create mode 100644 modules/openapi-generator/src/main/resources/cpp-oatpp-server/model-source.mustache create mode 100644 modules/openapi-generator/src/main/resources/cpp-oatpp-server/model-struct-header.mustache create mode 100644 modules/openapi-generator/src/main/resources/cpp-oatpp-server/model-struct-source.mustache create mode 100644 samples/server/petstore/cpp-oatpp/.openapi-generator-ignore create mode 100644 samples/server/petstore/cpp-oatpp/.openapi-generator/FILES create mode 100644 samples/server/petstore/cpp-oatpp/.openapi-generator/VERSION create mode 100644 samples/server/petstore/cpp-oatpp/CMakeLists.txt create mode 100644 samples/server/petstore/cpp-oatpp/README.md create mode 100644 samples/server/petstore/cpp-oatpp/api/PetApi.cpp create mode 100644 samples/server/petstore/cpp-oatpp/api/PetApi.h create mode 100644 samples/server/petstore/cpp-oatpp/api/StoreApi.cpp create mode 100644 samples/server/petstore/cpp-oatpp/api/StoreApi.h create mode 100644 samples/server/petstore/cpp-oatpp/api/UserApi.cpp create mode 100644 samples/server/petstore/cpp-oatpp/api/UserApi.h create mode 100644 samples/server/petstore/cpp-oatpp/impl/PetApiImpl.cpp create mode 100644 samples/server/petstore/cpp-oatpp/impl/PetApiImpl.h create mode 100644 samples/server/petstore/cpp-oatpp/impl/StoreApiImpl.cpp create mode 100644 samples/server/petstore/cpp-oatpp/impl/StoreApiImpl.h create mode 100644 samples/server/petstore/cpp-oatpp/impl/UserApiImpl.cpp create mode 100644 samples/server/petstore/cpp-oatpp/impl/UserApiImpl.h create mode 100644 samples/server/petstore/cpp-oatpp/main-api-server.cpp create mode 100644 samples/server/petstore/cpp-oatpp/model/ApiResponse.cpp create mode 100644 samples/server/petstore/cpp-oatpp/model/ApiResponse.h create mode 100644 samples/server/petstore/cpp-oatpp/model/Category.cpp create mode 100644 samples/server/petstore/cpp-oatpp/model/Category.h create mode 100644 samples/server/petstore/cpp-oatpp/model/Helpers.cpp create mode 100644 samples/server/petstore/cpp-oatpp/model/Helpers.h create mode 100644 samples/server/petstore/cpp-oatpp/model/Inline_object.cpp create mode 100644 samples/server/petstore/cpp-oatpp/model/Inline_object.h create mode 100644 samples/server/petstore/cpp-oatpp/model/Inline_object_1.cpp create mode 100644 samples/server/petstore/cpp-oatpp/model/Inline_object_1.h create mode 100644 samples/server/petstore/cpp-oatpp/model/Order.cpp create mode 100644 samples/server/petstore/cpp-oatpp/model/Order.h create mode 100644 samples/server/petstore/cpp-oatpp/model/Pet.cpp create mode 100644 samples/server/petstore/cpp-oatpp/model/Pet.h create mode 100644 samples/server/petstore/cpp-oatpp/model/Tag.cpp create mode 100644 samples/server/petstore/cpp-oatpp/model/Tag.h create mode 100644 samples/server/petstore/cpp-oatpp/model/User.cpp create mode 100644 samples/server/petstore/cpp-oatpp/model/User.h diff --git a/bin/configs/other/cpp-oatpp-server.yaml b/bin/configs/other/cpp-oatpp-server.yaml new file mode 100644 index 00000000000..ecc2ade996e --- /dev/null +++ b/bin/configs/other/cpp-oatpp-server.yaml @@ -0,0 +1,4 @@ +generatorName: cpp-oatpp-server +outputDir: samples/server/petstore/cpp-oatpp +inputSpec: modules/openapi-generator/src/test/resources/3_0/petstore.yaml +templateDir: modules/openapi-generator/src/main/resources/cpp-oatpp-server diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CppOatppServerCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CppOatppServerCodegen.java new file mode 100644 index 00000000000..f14d7b97b39 --- /dev/null +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CppOatppServerCodegen.java @@ -0,0 +1,438 @@ +/* + * Copyright 2018 OpenAPI-Generator Contributors (https://openapi-generator.tech) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.openapitools.codegen.languages; + +import io.swagger.v3.oas.models.Operation; +import io.swagger.v3.oas.models.media.ArraySchema; +import io.swagger.v3.oas.models.media.Schema; +import io.swagger.v3.oas.models.responses.ApiResponse; +import io.swagger.v3.oas.models.servers.Server; +import org.apache.commons.lang3.StringUtils; +import org.openapitools.codegen.*; +import org.openapitools.codegen.utils.ModelUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.File; +import java.util.*; + +import static org.openapitools.codegen.utils.StringUtils.underscore; + +public class CppOatppServerCodegen extends AbstractCppCodegen { + private static final Logger LOGGER = LoggerFactory.getLogger(CppOatppServerCodegen.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 = ""; + protected String helpersPackage = ""; + + @Override + public CodegenType getTag() { + return CodegenType.SERVER; + } + + @Override + public String getName() { + return "cpp-oatpp-server"; + } + + @Override + public String getHelp() { + // Ref: https://github.com/oatpp/oatpp + return "Generates a C++ API server (based on Oat++)"; + } + + public CppOatppServerCodegen() { + super(); + + if (StringUtils.isEmpty(modelNamePrefix)) { + modelNamePrefix = PREFIX; + } + + helpersPackage = "org.openapitools.server.helpers"; + apiPackage = "org.openapitools.server.api"; + modelPackage = "org.openapitools.server.model"; + + apiTemplateFiles.put("api-header.mustache", ".h"); + apiTemplateFiles.put("api-source.mustache", ".cpp"); + apiTemplateFiles.put("api-impl-header.mustache", ".h"); + apiTemplateFiles.put("api-impl-source.mustache", ".cpp"); + + embeddedTemplateDir = templateDir = "cpp-oatpp-server"; + + cliOptions.clear(); + addSwitch(OPTIONAL_EXTERNAL_LIB, OPTIONAL_EXTERNAL_LIB_DESC, this.isAddExternalLibs); + addOption(HELPERS_PACKAGE_NAME, HELPERS_PACKAGE_NAME_DESC, this.helpersPackage); + addOption(RESERVED_WORD_PREFIX_OPTION, RESERVED_WORD_PREFIX_DESC, this.reservedWordPrefix); + addSwitch(OPTION_USE_STRUCT_MODEL, OPTION_USE_STRUCT_MODEL_DESC, this.isUseStructModel); + addOption(VARIABLE_NAME_FIRST_CHARACTER_UPPERCASE_OPTION, + VARIABLE_NAME_FIRST_CHARACTER_UPPERCASE_DESC, + Boolean.toString(this.variableNameFirstCharacterUppercase)); + + supportingFiles.add(new SupportingFile("helpers-header.mustache", "model", modelNamePrefix + "Helpers.h")); + supportingFiles.add(new SupportingFile("helpers-source.mustache", "model", modelNamePrefix + "Helpers.cpp")); + supportingFiles.add(new SupportingFile("main-api-server.mustache", "", modelNamePrefix + "main-api-server.cpp")); + supportingFiles.add(new SupportingFile("cmake.mustache", "", "CMakeLists.txt")); + supportingFiles.add(new SupportingFile("README.mustache", "", "README.md")); + + languageSpecificPrimitives = new HashSet( + Arrays.asList("int", "char", "bool", "long", "float", "double", "int32_t", "int64_t")); + + typeMapping = new HashMap(); + typeMapping.put("date", "std::string"); + typeMapping.put("DateTime", "std::string"); + typeMapping.put("string", "std::string"); + typeMapping.put("integer", "int32_t"); + typeMapping.put("long", "int64_t"); + typeMapping.put("boolean", "bool"); + typeMapping.put("array", "std::vector"); + typeMapping.put("map", "std::map"); + typeMapping.put("file", "std::string"); + typeMapping.put("object", "Object"); + typeMapping.put("binary", "std::string"); + typeMapping.put("number", "double"); + typeMapping.put("UUID", "std::string"); + typeMapping.put("URI", "std::string"); + typeMapping.put("ByteArray", "std::string"); + + importMapping = new HashMap(); + importMapping.put("std::vector", "#include "); + importMapping.put("std::map", "#include "); + importMapping.put("std::string", "#include "); + importMapping.put("Object", "#include \"Object.h\""); + } + + @Override + public void processOpts() { + super.processOpts(); + if (additionalProperties.containsKey(HELPERS_PACKAGE_NAME)) { + helpersPackage = (String) additionalProperties.get(HELPERS_PACKAGE_NAME); + } + if (additionalProperties.containsKey("modelNamePrefix")) { + additionalProperties().put("prefix", modelNamePrefix); + supportingFiles.clear(); + supportingFiles.add(new SupportingFile("helpers-header.mustache", "model", modelNamePrefix + "Helpers.h")); + supportingFiles.add(new SupportingFile("helpers-source.mustache", "model", modelNamePrefix + "Helpers.cpp")); + supportingFiles.add(new SupportingFile("main-api-server.mustache", "", modelNamePrefix + "main-api-server.cpp")); + supportingFiles.add(new SupportingFile("cmake.mustache", "", "CMakeLists.txt")); + supportingFiles.add(new SupportingFile("README.mustache", "", "README.md")); + } + if (additionalProperties.containsKey(RESERVED_WORD_PREFIX_OPTION)) { + reservedWordPrefix = (String) additionalProperties.get(RESERVED_WORD_PREFIX_OPTION); + } + + additionalProperties.put("modelNamespaceDeclarations", modelPackage.split("\\.")); + additionalProperties.put("modelNamespace", modelPackage.replaceAll("\\.", "::")); + additionalProperties.put("apiNamespaceDeclarations", apiPackage.split("\\.")); + additionalProperties.put("apiNamespace", apiPackage.replaceAll("\\.", "::")); + additionalProperties.put("helpersNamespaceDeclarations", helpersPackage.split("\\.")); + additionalProperties.put("helpersNamespace", helpersPackage.replaceAll("\\.", "::")); + additionalProperties.put(RESERVED_WORD_PREFIX_OPTION, reservedWordPrefix); + + if (additionalProperties.containsKey(OPTIONAL_EXTERNAL_LIB)) { + setAddExternalLibs(convertPropertyToBooleanAndWriteBack(OPTIONAL_EXTERNAL_LIB)); + } else { + additionalProperties.put(OPTIONAL_EXTERNAL_LIB, isAddExternalLibs); + } + + // setup model templates + 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 + public String toModelImport(String name) { + if (importMapping.containsKey(name)) { + return importMapping.get(name); + } else { + return "#include \"" + name + ".h\""; + } + } + + + @Override + public CodegenModel fromModel(String name, Schema model) { + CodegenModel codegenModel = super.fromModel(name, model); + + Set oldImports = codegenModel.imports; + codegenModel.imports = new HashSet<>(); + for (String imp : oldImports) { + String newImp = toModelImport(imp); + if (!newImp.isEmpty()) { + codegenModel.imports.add(newImp); + } + } + + return codegenModel; + } + + @Override + public CodegenOperation fromOperation(String path, String httpMethod, Operation operation, List servers) { + CodegenOperation op = super.fromOperation(path, httpMethod, operation, servers); + + if (operation.getResponses() != null && !operation.getResponses().isEmpty()) { + ApiResponse apiResponse = findMethodResponse(operation.getResponses()); + + if (apiResponse != null) { + Schema response = ModelUtils.getSchemaFromResponse(apiResponse); + if (response != null) { + CodegenProperty cm = fromProperty("response", response); + op.vendorExtensions.put("x-codegen-response", cm); + if ("HttpContent".equals(cm.dataType)) { + op.vendorExtensions.put("x-codegen-response-ishttpcontent", true); + } + } + } + } + + String pathForOatpp = path.replaceAll("\\{(.*?)}", ":$1"); + op.vendorExtensions.put("x-codegen-oatpp-path", pathForOatpp); + + return op; + } + + @SuppressWarnings("unchecked") + @Override + public Map postProcessOperationsWithModels(Map objs, List allModels) { + Map operations = (Map) objs.get("operations"); + String classname = (String) operations.get("classname"); + operations.put("classnameSnakeUpperCase", underscore(classname).toUpperCase(Locale.ROOT)); + operations.put("classnameSnakeLowerCase", underscore(classname).toLowerCase(Locale.ROOT)); + List operationList = (List) operations.get("operation"); + for (CodegenOperation op : operationList) { + boolean consumeJson = false; + boolean isParsingSupported = true; + if (op.bodyParam != null) { + if (op.bodyParam.vendorExtensions == null) { + op.bodyParam.vendorExtensions = new HashMap<>(); + } + + boolean isStringOrDate = op.bodyParam.isString || op.bodyParam.isDate; + op.bodyParam.vendorExtensions.put("x-codegen-oatpp-is-string-or-date", isStringOrDate); + } + if (op.consumes != null) { + for (Map consume : op.consumes) { + if (consume.get("mediaType") != null && consume.get("mediaType").equals("application/json")) { + consumeJson = true; + } + } + } + + op.httpMethod = op.httpMethod.substring(0, 1).toUpperCase(Locale.ROOT) + op.httpMethod.substring(1).toLowerCase(Locale.ROOT); + + for (CodegenParameter param : op.allParams) { + if (param.isFormParam) isParsingSupported = false; + if (param.isFile) isParsingSupported = false; + if (param.isCookieParam) isParsingSupported = false; + + //TODO: This changes the info about the real type but it is needed to parse the header params + if (param.isHeaderParam) { + param.dataType = "Oatpp::Optional"; + param.baseType = "Oatpp::Optional"; + } else if (param.isQueryParam) { + if (param.isPrimitiveType) { + param.dataType = "Oatpp::Optional<" + param.dataType + ">"; + } else { + param.dataType = "Oatpp::Optional<" + param.dataType + ">"; + param.baseType = "Oatpp::Optional<" + param.baseType + ">"; + } + } + } + + if (op.vendorExtensions == null) { + op.vendorExtensions = new HashMap<>(); + } + op.vendorExtensions.put("x-codegen-oatpp-consumes-json", consumeJson); + op.vendorExtensions.put("x-codegen-oatpp-is-parsing-supported", isParsingSupported); + + // Check if any one of the operations needs a model, then at API file level, at least one model has to be included. + for (String hdr : op.imports) { + if (importMapping.containsKey(hdr)) { + continue; + } + operations.put("hasModelImport", true); + } + } + + return objs; + } + + @Override + public String toModelFilename(String name) { + return toModelName(name); + } + + @Override + public String apiFilename(String templateName, String tag) { + String result = super.apiFilename(templateName, tag); + + if (templateName.endsWith("impl-header.mustache")) { + int ix = result.lastIndexOf(File.separatorChar); + result = result.substring(0, ix) + result.substring(ix, result.length() - 2) + "Impl.h"; + result = result.replace(apiFileFolder(), implFileFolder()); + } else if (templateName.endsWith("impl-source.mustache")) { + int ix = result.lastIndexOf(File.separatorChar); + result = result.substring(0, ix) + result.substring(ix, result.length() - 4) + "Impl.cpp"; + result = result.replace(apiFileFolder(), implFileFolder()); + } + return result; + } + + @Override + public String toApiFilename(String name) { + return toApiName(name); + } + + /** + * Optional - type declaration. This is a String which is used by the + * templates to instantiate your types. There is typically special handling + * for different property types + * + * @return a string value used as the `dataType` field for model templates, + * `returnType` for api templates + */ + @Override + public String getTypeDeclaration(Schema p) { + String openAPIType = getSchemaType(p); + + if (ModelUtils.isArraySchema(p)) { + ArraySchema ap = (ArraySchema) p; + Schema inner = ap.getItems(); + return getSchemaType(p) + "<" + getTypeDeclaration(inner) + ">"; + } else if (ModelUtils.isMapSchema(p)) { + Schema inner = getAdditionalProperties(p); + return getSchemaType(p) + ""; + } else if (ModelUtils.isByteArraySchema(p)) { + return "std::string"; + } else if (ModelUtils.isStringSchema(p) + || ModelUtils.isDateSchema(p) + || ModelUtils.isDateTimeSchema(p) || ModelUtils.isFileSchema(p) + || languageSpecificPrimitives.contains(openAPIType)) { + return toModelName(openAPIType); + } + + return openAPIType; + } + + @Override + public String toDefaultValue(Schema p) { + if (ModelUtils.isBooleanSchema(p)) { + return "false"; + } else if (ModelUtils.isDateSchema(p)) { + return "\"\""; + } else if (ModelUtils.isDateTimeSchema(p)) { + return "\"\""; + } else if (ModelUtils.isNumberSchema(p)) { + if (ModelUtils.isFloatSchema(p)) { + return "0.0f"; + } + return "0.0"; + } else if (ModelUtils.isIntegerSchema(p)) { + if (ModelUtils.isLongSchema(p)) { + return "0L"; + } + return "0"; + } else if (ModelUtils.isByteArraySchema(p)) { + 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)) { + return "\"\""; + } + + return ""; + } + + /** + * Location to write model files. You can use the modelPackage() as defined + * when the class is instantiated + */ + public String modelFileFolder() { + return (outputFolder + "/model").replace("/", File.separator); + } + + /** + * Location to write api files. You can use the apiPackage() as defined when + * the class is instantiated + */ + @Override + public String apiFileFolder() { + return (outputFolder + "/api").replace("/", File.separator); + } + + private String implFileFolder() { + return (outputFolder + "/" + implFolder).replace("/", File.separator); + } + + /** + * Optional - OpenAPI type conversion. This is used to map OpenAPI types in + * a `Schema` into either language specific types via `typeMapping` or + * into complex models if there is not a mapping. + * + * @return a string value of the type or complex model for this property + */ + @Override + public String getSchemaType(Schema p) { + String openAPIType = super.getSchemaType(p); + String type = null; + if (typeMapping.containsKey(openAPIType)) { + type = typeMapping.get(openAPIType); + if (languageSpecificPrimitives.contains(type)) + return toModelName(type); + } else + type = openAPIType; + return toModelName(type); + } + + @Override + public String getTypeDeclaration(String str) { + return toModelName(str); + } + + /** + * Specify whether external libraries will be added during the generation + * + * @param value the value to be set + */ + public void setAddExternalLibs(boolean value) { + isAddExternalLibs = value; + } +} diff --git a/modules/openapi-generator/src/main/resources/META-INF/services/org.openapitools.codegen.CodegenConfig b/modules/openapi-generator/src/main/resources/META-INF/services/org.openapitools.codegen.CodegenConfig index 9a8d8ad03d2..33f771a8bd0 100644 --- a/modules/openapi-generator/src/main/resources/META-INF/services/org.openapitools.codegen.CodegenConfig +++ b/modules/openapi-generator/src/main/resources/META-INF/services/org.openapitools.codegen.CodegenConfig @@ -12,6 +12,7 @@ org.openapitools.codegen.languages.ClojureClientCodegen org.openapitools.codegen.languages.ConfluenceWikiCodegen org.openapitools.codegen.languages.CppQt5ClientCodegen org.openapitools.codegen.languages.CppQt5QHttpEngineServerCodegen +org.openapitools.codegen.languages.CppOatppServerCodegen org.openapitools.codegen.languages.CppPistacheServerCodegen org.openapitools.codegen.languages.CppRestbedServerCodegen org.openapitools.codegen.languages.CppRestSdkClientCodegen diff --git a/modules/openapi-generator/src/main/resources/cpp-oatpp-server/README.mustache b/modules/openapi-generator/src/main/resources/cpp-oatpp-server/README.mustache new file mode 100644 index 00000000000..e9bd9281ee7 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/cpp-oatpp-server/README.mustache @@ -0,0 +1,48 @@ +# REST API Server for {{appName}} + +## Overview +This API Server was generated by the [OpenAPI Generator](https://openapi-generator.tech) project. +It uses the [Oat++](https://github.com/oatpp/oatpp) Framework. + +## Files organization +The Oat++ C++ REST server generator creates three folders: +- `api`: This folder contains the handlers for each method specified in the OpenAPI definition. Every handler extracts +the path and body parameters (if any) from the requests and tries to parse and possibly validate them. +Once this step is completed, the main API class calls the corresponding abstract method that should be implemented +by the developer (a basic implementation is provided under the `impl` folder) +- `impl`: As written above, the implementation folder contains, for each API, the corresponding implementation class, +which extends the main API class and implements the abstract methods. +Every method receives the path and body parameters as constant reference variables and a reference to the response +object, that should be filled with the right response and sent at the end of the method with the command: +response.send(returnCode, responseBody, [mimeType]) +- `model`: This folder contains the corresponding class for every object schema found in the OpenAPI specification. + +The main folder contains also a file with a main that can be used to start the server. +Of course, is you should customize this file based on your needs + +## Installation +First of all, you need to download and install the libraries listed [here](#libraries-required). + +Once the libraries are installed, in order to compile and run the server please follow the steps below: +```bash +mkdir build +cd build +cmake .. +make +``` + +Once compiled run the server: + +```bash +cd build +./api-server +``` + +## Libraries required +- [Oat++](http://github.com/oatpp/oatpp) +- [JSON for Modern C++](https://github.com/nlohmann/json/#integration): Please download the `json.hpp` file and +put it under the model/nlohmann folder + +## Namespaces +{{{apiPackage}}} +{{{modelPackage}}} diff --git a/modules/openapi-generator/src/main/resources/cpp-oatpp-server/api-header.mustache b/modules/openapi-generator/src/main/resources/cpp-oatpp-server/api-header.mustache new file mode 100644 index 00000000000..34d26880fe0 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/cpp-oatpp-server/api-header.mustache @@ -0,0 +1,72 @@ +{{>licenseInfo}} +{{#operations}}/* + * {{classname}}.h + * + * {{description}} + */ + +#ifndef {{classname}}_H_ +#define {{classname}}_H_ + +{{{defaultInclude}}} +#include +#include +#include +#include +{{^hasModelImport}}#include {{/hasModelImport}} + +{{#imports}}{{{import}}} +{{/imports}} + +{{#apiNamespaceDeclarations}} +namespace {{this}} { +{{/apiNamespaceDeclarations}} + +{{#hasModelImport}} +using namespace {{modelNamespace}};{{/hasModelImport}} + +class {{declspec}} {{classname}} { +public: + {{classname}}(std::shared_ptr); + virtual ~{{classname}}() {} + void init(); + + const std::string base = "{{basePathWithoutHost}}"; + +private: + void setupRoutes(); + + {{#operation}} + void {{operationIdSnakeCase}}_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response); + {{/operation}} + void {{classnameSnakeLowerCase}}_default_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response); + + std::shared_ptr router; + {{#operation}} + + /// + /// {{summary}} + /// + /// + /// {{notes}} + /// + {{#vendorExtensions.x-codegen-oatpp-is-parsing-supported}} + {{#allParams}} + /// {{description}}{{^required}} (optional{{#defaultValue}}, default to {{.}}{{/defaultValue}}){{/required}} + {{/allParams}} + virtual void {{operationIdSnakeCase}}({{#allParams}}const {{{dataType}}} &{{paramName}}{{^-last}}, {{/-last}}{{/allParams}}{{#hasParams}}, {{/hasParams}}Pistache::Http::ResponseWriter &response) = 0; + {{/vendorExtensions.x-codegen-oatpp-is-parsing-supported}} + {{^vendorExtensions.x-codegen-oatpp-is-parsing-supported}} + virtual void {{operationIdSnakeCase}}(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter &response) = 0; + {{/vendorExtensions.x-codegen-oatpp-is-parsing-supported}} + {{/operation}} + +}; + +{{#apiNamespaceDeclarations}} +} +{{/apiNamespaceDeclarations}} + +#endif /* {{classname}}_H_ */ + +{{/operations}} diff --git a/modules/openapi-generator/src/main/resources/cpp-oatpp-server/api-impl-header.mustache b/modules/openapi-generator/src/main/resources/cpp-oatpp-server/api-impl-header.mustache new file mode 100644 index 00000000000..85f595ed7c2 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/cpp-oatpp-server/api-impl-header.mustache @@ -0,0 +1,55 @@ +{{>licenseInfo}} + +{{#operations}}/* +* {{classname}}Impl.h +* +* {{description}} +*/ + +#ifndef {{classnameSnakeUpperCase}}_IMPL_H_ +#define {{classnameSnakeUpperCase}}_IMPL_H_ + +{{{defaultInclude}}} +#include +#include +#include +#include + +#include <{{classname}}.h> + +#include + +{{#imports}}{{{import}}} +{{/imports}} + +{{#apiNamespaceDeclarations}} +namespace {{this}} { +{{/apiNamespaceDeclarations}} + +{{#hasModelImport}} +using namespace {{modelNamespace}};{{/hasModelImport}} + +class {{classname}}Impl : public {{apiNamespace}}::{{classname}} { +public: + {{classname}}Impl(std::shared_ptr); + ~{{classname}}Impl() {} + + {{#operation}} + {{#vendorExtensions.x-codegen-oatpp-is-parsing-supported}} + void {{operationIdSnakeCase}}({{#allParams}}const {{{dataType}}} &{{paramName}}{{^-last}}, {{/-last}}{{/allParams}}{{#hasParams}}, {{/hasParams}}Pistache::Http::ResponseWriter &response); + {{/vendorExtensions.x-codegen-oatpp-is-parsing-supported}} + {{^vendorExtensions.x-codegen-oatpp-is-parsing-supported}} + void {{operationIdSnakeCase}}(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter &response); + {{/vendorExtensions.x-codegen-oatpp-is-parsing-supported}} + {{/operation}} + +}; + +{{#apiNamespaceDeclarations}} +} +{{/apiNamespaceDeclarations}} + +{{/operations}} + + +#endif \ No newline at end of file diff --git a/modules/openapi-generator/src/main/resources/cpp-oatpp-server/api-impl-source.mustache b/modules/openapi-generator/src/main/resources/cpp-oatpp-server/api-impl-source.mustache new file mode 100644 index 00000000000..5f18a43524f --- /dev/null +++ b/modules/openapi-generator/src/main/resources/cpp-oatpp-server/api-impl-source.mustache @@ -0,0 +1,34 @@ +{{>licenseInfo}} +{{#operations}} + +#include "{{classname}}Impl.h" + +{{#apiNamespaceDeclarations}} +namespace {{this}} { +{{/apiNamespaceDeclarations}} + +{{#hasModelImport}} +using namespace {{modelNamespace}};{{/hasModelImport}} + +{{classname}}Impl::{{classname}}Impl(std::shared_ptr rtr) + : {{classname}}(rtr) + { } + +{{#operation}} +{{#vendorExtensions.x-codegen-oatpp-is-parsing-supported}} +void {{classname}}Impl::{{operationIdSnakeCase}}({{#allParams}}const {{{dataType}}} &{{paramName}}{{^-last}}, {{/-last}}{{/allParams}}{{#hasParams}}, {{/hasParams}}Pistache::Http::ResponseWriter &response) { + response.send(Pistache::Http::Code::Ok, "Do some magic\n"); +} +{{/vendorExtensions.x-codegen-oatpp-is-parsing-supported}} +{{^vendorExtensions.x-codegen-oatpp-is-parsing-supported}} +void {{classname}}Impl::{{operationIdSnakeCase}}(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter &response){ + response.send(Pistache::Http::Code::Ok, "Do some magic\n"); +} +{{/vendorExtensions.x-codegen-oatpp-is-parsing-supported}} +{{/operation}} + +{{#apiNamespaceDeclarations}} +} +{{/apiNamespaceDeclarations}} + +{{/operations}} \ No newline at end of file diff --git a/modules/openapi-generator/src/main/resources/cpp-oatpp-server/api-source.mustache b/modules/openapi-generator/src/main/resources/cpp-oatpp-server/api-source.mustache new file mode 100644 index 00000000000..39cf7c1cb5b --- /dev/null +++ b/modules/openapi-generator/src/main/resources/cpp-oatpp-server/api-source.mustache @@ -0,0 +1,111 @@ +{{>licenseInfo}} +{{#operations}} + +#include "{{classname}}.h" +#include "{{prefix}}Helpers.h" + +{{#apiNamespaceDeclarations}} +namespace {{this}} { +{{/apiNamespaceDeclarations}} + +using namespace {{helpersNamespace}}; +{{#hasModelImport}} +using namespace {{modelNamespace}};{{/hasModelImport}} + +{{classname}}::{{classname}}(std::shared_ptr rtr) { + router = rtr; +} + +void {{classname}}::init() { + setupRoutes(); +} + +void {{classname}}::setupRoutes() { + using namespace Pistache::Rest; + + {{#operation}} + Routes::{{httpMethod}}(*router, base + "{{{vendorExtensions.x-codegen-oatpp-path}}}", Routes::bind(&{{classname}}::{{operationIdSnakeCase}}_handler, this)); + {{/operation}} + + // Default handler, called when a route is not found + router->addCustomHandler(Routes::bind(&{{classname}}::{{classnameSnakeLowerCase}}_default_handler, this)); +} + +{{#operation}} +void {{classname}}::{{operationIdSnakeCase}}_handler(const Pistache::Rest::Request &{{#hasParams}}request{{/hasParams}}, Pistache::Http::ResponseWriter response) { + {{#vendorExtensions.x-codegen-oatpp-is-parsing-supported}} + {{#hasPathParams}} + // Getting the path params + {{#pathParams}} + auto {{paramName}} = request.param(":{{paramName}}").as<{{dataType}}>(); + {{/pathParams}} + {{/hasPathParams}}{{#hasBodyParam}} + // Getting the body param + {{#bodyParam}} + {{^isPrimitiveType}}{{^isContainer}} + {{baseType}} {{paramName}};{{/isContainer}}{{#isArray}}std::vector<{{items.baseType}}> {{paramName}};{{/isArray}}{{#isMap}}std::map {{paramName}};{{/isMap}}{{/isPrimitiveType}} + {{#isPrimitiveType}} + {{dataType}} {{paramName}}; + {{/isPrimitiveType}} + {{/bodyParam}} + {{/hasBodyParam}}{{#hasQueryParams}} + // Getting the query params + {{#queryParams}} + auto {{paramName}}Query = request.query().get("{{baseName}}"); + Pistache::Optional<{{^isContainer}}{{dataType}}{{/isContainer}}{{#isArray}}std::vector<{{items.baseType}}>{{/isArray}}> {{paramName}}; + if(!{{paramName}}Query.isEmpty()){ + {{^isContainer}}{{dataType}}{{/isContainer}}{{#isArray}}std::vector<{{items.baseType}}>{{/isArray}} valueQuery_instance; + if(fromStringValue({{paramName}}Query.get(), valueQuery_instance)){ + {{paramName}} = Pistache::Some(valueQuery_instance); + } + } + {{/queryParams}} + {{/hasQueryParams}}{{#hasHeaderParams}} + // Getting the header params + {{#headerParams}} + auto {{paramName}} = request.headers().tryGetRaw("{{baseName}}"); + {{/headerParams}} + {{/hasHeaderParams}} + + try { + {{#hasBodyParam}} + {{#bodyParam}} + {{^isPrimitiveType}} + nlohmann::json::parse(request.body()).get_to({{paramName}}); + {{/isPrimitiveType}} + {{#isPrimitiveType}} + {{paramName}} = request.body(); + {{/isPrimitiveType}} + {{/bodyParam}} + {{/hasBodyParam}} + this->{{operationIdSnakeCase}}({{#allParams}}{{paramName}}{{^-last}}, {{/-last}}{{/allParams}}{{#hasParams}}, {{/hasParams}}response); + {{/vendorExtensions.x-codegen-oatpp-is-parsing-supported}} + {{^vendorExtensions.x-codegen-oatpp-is-parsing-supported}} + try { + this->{{operationIdSnakeCase}}(request, response); + {{/vendorExtensions.x-codegen-oatpp-is-parsing-supported}} + } catch (nlohmann::detail::exception &e) { + //send a 400 error + response.send(Pistache::Http::Code::Bad_Request, e.what()); + return; + } catch (Pistache::Http::HttpError &e) { + response.send(static_cast(e.code()), e.what()); + return; + } catch (std::exception &e) { + //send a 500 error + response.send(Pistache::Http::Code::Internal_Server_Error, e.what()); + return; + } + +} +{{/operation}} + +void {{classname}}::{{classnameSnakeLowerCase}}_default_handler(const Pistache::Rest::Request &, Pistache::Http::ResponseWriter response) { + response.send(Pistache::Http::Code::Not_Found, "The requested method does not exist"); +} + +{{#apiNamespaceDeclarations}} +} +{{/apiNamespaceDeclarations}} + +{{/operations}} diff --git a/modules/openapi-generator/src/main/resources/cpp-oatpp-server/cmake.mustache b/modules/openapi-generator/src/main/resources/cpp-oatpp-server/cmake.mustache new file mode 100644 index 00000000000..8fc1b19b7e7 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/cpp-oatpp-server/cmake.mustache @@ -0,0 +1,39 @@ +cmake_minimum_required (VERSION 3.2) + +project(api-server) + +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 -pg -g3" ) + +{{#addExternalLibs}} +include(ExternalProject) + +set(EXTERNAL_INSTALL_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/external) + +ExternalProject_Add(PISTACHE + GIT_REPOSITORY https://github.com/oktal/pistache.git + CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=${EXTERNAL_INSTALL_LOCATION} +) + +ExternalProject_Add(NLOHMANN + GIT_REPOSITORY https://github.com/nlohmann/json.git + CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=${EXTERNAL_INSTALL_LOCATION} + +) +include_directories(${EXTERNAL_INSTALL_LOCATION}/include) +link_directories(${EXTERNAL_INSTALL_LOCATION}/lib) +{{/addExternalLibs}} + +include_directories(model) +include_directories(api) +include_directories(impl) + +file(GLOB SRCS + ${CMAKE_CURRENT_SOURCE_DIR}/api/*.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/impl/*.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/model/*.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp +) + +add_executable(${PROJECT_NAME} ${SRCS} ) +{{#addExternalLibs}}add_dependencies(${PROJECT_NAME} PISTACHE NLOHMANN){{/addExternalLibs}} +target_link_libraries(${PROJECT_NAME} pistache pthread) diff --git a/modules/openapi-generator/src/main/resources/cpp-oatpp-server/helpers-header.mustache b/modules/openapi-generator/src/main/resources/cpp-oatpp-server/helpers-header.mustache new file mode 100644 index 00000000000..c39cca04fe1 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/cpp-oatpp-server/helpers-header.mustache @@ -0,0 +1,64 @@ +{{>licenseInfo}} +/* + * {{prefix}}Helpers.h + * + * This is the helper class for models and primitives + */ + +#ifndef {{prefix}}Helpers_H_ +#define {{prefix}}Helpers_H_ + +#include +#include +#include +#include +#include + +{{#helpersNamespaceDeclarations}} +namespace {{this}} { +{{/helpersNamespaceDeclarations}} + + std::string toStringValue(const std::string &value); + std::string toStringValue(const int32_t &value); + std::string toStringValue(const int64_t &value); + std::string toStringValue(const bool &value); + std::string toStringValue(const float &value); + std::string toStringValue(const double &value); + + bool fromStringValue(const std::string &inStr, std::string &value); + bool fromStringValue(const std::string &inStr, int32_t &value); + bool fromStringValue(const std::string &inStr, int64_t &value); + bool fromStringValue(const std::string &inStr, bool &value); + bool fromStringValue(const std::string &inStr, float &value); + bool fromStringValue(const std::string &inStr, double &value); + template + bool fromStringValue(const std::vector &inStr, std::vector &value){ + try{ + for(auto & item : inStr){ + T itemValue; + if(fromStringValue(item, itemValue)){ + value.push_back(itemValue); + } + } + } + catch(...){ + return false; + } + return value.size() > 0; + } + template + bool fromStringValue(const std::string &inStr, std::vector &value, char separator = ','){ + std::vector inStrings; + std::istringstream f(inStr); + std::string s; + while (std::getline(f, s, separator)) { + inStrings.push_back(s); + } + return fromStringValue(inStrings, value); + } + +{{#helpersNamespaceDeclarations}} +} +{{/helpersNamespaceDeclarations}} + +#endif // {{prefix}}Helpers_H_ \ No newline at end of file diff --git a/modules/openapi-generator/src/main/resources/cpp-oatpp-server/helpers-source.mustache b/modules/openapi-generator/src/main/resources/cpp-oatpp-server/helpers-source.mustache new file mode 100644 index 00000000000..82b4e2d81ef --- /dev/null +++ b/modules/openapi-generator/src/main/resources/cpp-oatpp-server/helpers-source.mustache @@ -0,0 +1,86 @@ +{{>licenseInfo}} +#include "{{prefix}}Helpers.h" + +{{#helpersNamespaceDeclarations}} +namespace {{this}} { +{{/helpersNamespaceDeclarations}} + + +std::string toStringValue(const std::string &value){ + return std::string(value); +} + +std::string toStringValue(const int32_t &value){ + return std::to_string(value); +} + +std::string toStringValue(const int64_t &value){ + return std::to_string(value); +} + +std::string toStringValue(const bool &value){ + return value?std::string("true"):std::string("false"); +} + +std::string toStringValue(const float &value){ + return std::to_string(value); +} + +std::string toStringValue(const double &value){ + return std::to_string(value); +} + +bool fromStringValue(const std::string &inStr, std::string &value){ + value = std::string(inStr); + return true; +} + +bool fromStringValue(const std::string &inStr, int32_t &value){ + try { + value = std::stoi( inStr ); + } + catch (const std::invalid_argument&) { + return false; + } + return true; +} + +bool fromStringValue(const std::string &inStr, int64_t &value){ + try { + value = std::stol( inStr ); + } + catch (const std::invalid_argument&) { + return false; + } + return true; +} + +bool fromStringValue(const std::string &inStr, bool &value){ + bool result = true; + inStr == "true"?value = true: inStr == "false"?value = false: result = false; + return result; +} + +bool fromStringValue(const std::string &inStr, float &value){ + try { + value = std::stof( inStr ); + } + catch (const std::invalid_argument&) { + return false; + } + return true; +} + +bool fromStringValue(const std::string &inStr, double &value){ + try { + value = std::stod( inStr ); + } + catch (const std::invalid_argument&) { + return false; + } + return true; +} + +{{#helpersNamespaceDeclarations}} +} +{{/helpersNamespaceDeclarations}} \ No newline at end of file diff --git a/modules/openapi-generator/src/main/resources/cpp-oatpp-server/licenseInfo.mustache b/modules/openapi-generator/src/main/resources/cpp-oatpp-server/licenseInfo.mustache new file mode 100644 index 00000000000..3a547de74bb --- /dev/null +++ b/modules/openapi-generator/src/main/resources/cpp-oatpp-server/licenseInfo.mustache @@ -0,0 +1,11 @@ +/** +* {{{appName}}} +* {{{appDescription}}} +* +* {{#version}}The version of the OpenAPI document: {{{version}}}{{/version}} +* {{#infoEmail}}Contact: {{{infoEmail}}}{{/infoEmail}} +* +* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech). +* https://openapi-generator.tech +* Do not edit the class manually. +*/ \ No newline at end of file diff --git a/modules/openapi-generator/src/main/resources/cpp-oatpp-server/main-api-server.mustache b/modules/openapi-generator/src/main/resources/cpp-oatpp-server/main-api-server.mustache new file mode 100644 index 00000000000..01d7882fa98 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/cpp-oatpp-server/main-api-server.mustache @@ -0,0 +1,79 @@ +{{>licenseInfo}} + + +#include "pistache/endpoint.h" +#include "pistache/http.h" +#include "pistache/router.h" +#ifdef __linux__ +#include +#include +#include +#endif +{{#apiInfo}}{{#apis}}{{#operations}} +#include "{{classname}}Impl.h"{{/operations}}{{/apis}}{{/apiInfo}} + +#define PISTACHE_SERVER_THREADS 2 +#define PISTACHE_SERVER_MAX_REQUEST_SIZE 32768 +#define PISTACHE_SERVER_MAX_RESPONSE_SIZE 32768 + +static Pistache::Http::Endpoint *httpEndpoint; +#ifdef __linux__ +static void sigHandler [[noreturn]] (int sig){ + switch(sig){ + case SIGINT: + case SIGQUIT: + case SIGTERM: + case SIGHUP: + default: + httpEndpoint->shutdown(); + break; + } + exit(0); +} + +static void setUpUnixSignals(std::vector quitSignals) { + sigset_t blocking_mask; + sigemptyset(&blocking_mask); + for (auto sig : quitSignals) + sigaddset(&blocking_mask, sig); + + struct sigaction sa; + sa.sa_handler = sigHandler; + sa.sa_mask = blocking_mask; + sa.sa_flags = 0; + + for (auto sig : quitSignals) + sigaction(sig, &sa, nullptr); +} +#endif + +using namespace {{apiNamespace}}; + +int main() { +#ifdef __linux__ + std::vector sigs{SIGQUIT, SIGINT, SIGTERM, SIGHUP}; + setUpUnixSignals(sigs); +#endif + Pistache::Address addr(Pistache::Ipv4::any(), Pistache::Port({{#serverPort}}{{serverPort}}{{/serverPort}}{{^serverPort}}8080{{/serverPort}})); + + httpEndpoint = new Pistache::Http::Endpoint((addr)); + auto router = std::make_shared(); + + auto opts = Pistache::Http::Endpoint::options() + .threads(PISTACHE_SERVER_THREADS); + opts.flags(Pistache::Tcp::Options::ReuseAddr); + opts.maxRequestSize(PISTACHE_SERVER_MAX_REQUEST_SIZE); + opts.maxResponseSize(PISTACHE_SERVER_MAX_RESPONSE_SIZE); + httpEndpoint->init(opts); + + {{#apiInfo}}{{#apis}}{{#operations}} + {{classname}}Impl {{classname}}server(router); + {{classname}}server.init();{{/operations}}{{/apis}}{{/apiInfo}} + + httpEndpoint->setHandler(router->handler()); + httpEndpoint->serve(); + + httpEndpoint->shutdown(); + +} + diff --git a/modules/openapi-generator/src/main/resources/cpp-oatpp-server/model-header.mustache b/modules/openapi-generator/src/main/resources/cpp-oatpp-server/model-header.mustache new file mode 100644 index 00000000000..b999e5997b4 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/cpp-oatpp-server/model-header.mustache @@ -0,0 +1,60 @@ +{{>licenseInfo}} +{{#models}}{{#model}}/* + * {{classname}}.h + * + * {{description}} + */ + +#ifndef {{classname}}_H_ +#define {{classname}}_H_ + +{{{defaultInclude}}} +{{#imports}}{{{this}}} +{{/imports}} +#include + +{{#modelNamespaceDeclarations}} +namespace {{this}} { +{{/modelNamespaceDeclarations}} + +/// +/// {{description}} +/// +class {{declspec}} {{classname}} +{ +public: + {{classname}}(); + virtual ~{{classname}}(); + + void validate(); + + ///////////////////////////////////////////// + /// {{classname}} members + + {{#vars}} + /// + /// {{description}} + /// + {{{dataType}}}{{#isContainer}}&{{/isContainer}} {{getter}}(){{^isContainer}} const{{/isContainer}}; + void {{setter}}({{{dataType}}} const{{^isPrimitiveType}}&{{/isPrimitiveType}} value);{{^required}} + bool {{nameInCamelCase}}IsSet() const; + void unset{{name}}();{{/required}} + {{/vars}} + + friend void to_json(nlohmann::json& j, const {{classname}}& o); + friend void from_json(const nlohmann::json& j, {{classname}}& o); +protected: + {{#vars}} + {{{dataType}}} m_{{name}}; + {{^required}} + bool m_{{name}}IsSet;{{/required}} + {{/vars}} +}; + +{{#modelNamespaceDeclarations}} +} +{{/modelNamespaceDeclarations}} + +#endif /* {{classname}}_H_ */ +{{/model}} +{{/models}} diff --git a/modules/openapi-generator/src/main/resources/cpp-oatpp-server/model-source.mustache b/modules/openapi-generator/src/main/resources/cpp-oatpp-server/model-source.mustache new file mode 100644 index 00000000000..03b8d574df4 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/cpp-oatpp-server/model-source.mustache @@ -0,0 +1,73 @@ +{{>licenseInfo}} +{{#models}}{{#model}} + +#include "{{classname}}.h" + +{{#modelNamespaceDeclarations}} +namespace {{this}} { +{{/modelNamespaceDeclarations}} + +{{classname}}::{{classname}}() +{ + {{#vars}}{{^isContainer}}{{#isPrimitiveType}}m_{{name}} = {{{defaultValue}}}; + {{/isPrimitiveType}}{{^isPrimitiveType}}{{#isString}}m_{{name}} = {{{defaultValue}}}; + {{/isString}}{{#isDateTime}}m_{{name}} = {{{defaultValue}}}; + {{/isDateTime}}{{/isPrimitiveType}}{{/isContainer}}{{^required}}m_{{name}}IsSet = false; + {{/required}}{{/vars}} +} + +{{classname}}::~{{classname}}() +{ +} + +void {{classname}}::validate() +{ + // TODO: implement validation +} + +void to_json(nlohmann::json& j, const {{classname}}& o) +{ + j = nlohmann::json(); + {{#vars}} + {{#required}}j["{{baseName}}"] = o.m_{{name}};{{/required}}{{^required}}if(o.{{nameInCamelCase}}IsSet(){{#isContainer}} || !o.m_{{name}}.empty(){{/isContainer}}) + j["{{baseName}}"] = o.m_{{name}};{{/required}} + {{/vars}} +} + +void from_json(const nlohmann::json& j, {{classname}}& o) +{ + {{#vars}} + {{#required}}j.at("{{baseName}}").get_to(o.m_{{name}});{{/required}}{{^required}}if(j.find("{{baseName}}") != j.end()) + { + j.at("{{baseName}}").get_to(o.m_{{name}}); + o.m_{{name}}IsSet = true; + } {{/required}} + {{/vars}} +} + +{{#vars}}{{{dataType}}}{{#isContainer}}&{{/isContainer}} {{classname}}::{{getter}}(){{^isContainer}} const{{/isContainer}} +{ + return m_{{name}}; +} +void {{classname}}::{{setter}}({{{dataType}}} const{{^isPrimitiveType}}&{{/isPrimitiveType}} value) +{ + m_{{name}} = value;{{^required}} + m_{{name}}IsSet = true;{{/required}} +} +{{^required}}bool {{classname}}::{{nameInCamelCase}}IsSet() const +{ + return m_{{name}}IsSet; +} +void {{classname}}::unset{{name}}() +{ + m_{{name}}IsSet = false; +} +{{/required}} +{{/vars}} + +{{#modelNamespaceDeclarations}} +} +{{/modelNamespaceDeclarations}} + +{{/model}} +{{/models}} diff --git a/modules/openapi-generator/src/main/resources/cpp-oatpp-server/model-struct-header.mustache b/modules/openapi-generator/src/main/resources/cpp-oatpp-server/model-struct-header.mustache new file mode 100644 index 00000000000..bcc64257e40 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/cpp-oatpp-server/model-struct-header.mustache @@ -0,0 +1,43 @@ +{{>licenseInfo}} +{{#models}}{{#model}}/* + * {{classname}}.h + * + * {{description}} + */ + +#ifndef {{classname}}_H_ +#define {{classname}}_H_ + +{{{defaultInclude}}} +{{#imports}}{{{this}}} +{{/imports}} +#include +{{#hasOptional}}#include {{/hasOptional}} + +{{#modelNamespaceDeclarations}} +namespace {{this}} { +{{/modelNamespaceDeclarations}} + +struct {{classname}} +{ + {{#vars}} + {{^required}}Pistache::Optional<{{/required}}{{{dataType}}}{{^required}}>{{/required}} {{name}}; + {{/vars}} + + bool operator==(const {{classname}}& other) const; + bool operator!=(const {{classname}}& other) const; + + 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-oatpp-server/model-struct-source.mustache b/modules/openapi-generator/src/main/resources/cpp-oatpp-server/model-struct-source.mustache new file mode 100644 index 00000000000..52d3526e867 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/cpp-oatpp-server/model-struct-source.mustache @@ -0,0 +1,59 @@ +{{>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; +} + +bool {{classname}}::operator==(const {{classname}}& other) const +{ + return {{#vars}}{{name}} == other.{{name}}{{^-last}} && {{/-last}}{{/vars}}; +} + +bool {{classname}}::operator!=(const {{classname}}& other) const +{ + return !(*this == other); +} + +void to_json(nlohmann::json& j, const {{classname}}& o) +{ + {{#vars}} + {{^required}}if (!o.{{name}}.isEmpty()){{/required}} + j["{{baseName}}"] = o.{{name}}{{^required}}.get(){{/required}}; + {{/vars}} +} + +void from_json(const nlohmann::json& j, {{classname}}& o) +{ + {{#vars}} + {{#required}}j.at("{{baseName}}").get_to(o.{{name}});{{/required}} + {{^required}}if (j.find("{{baseName}}") != j.end()) { + {{{dataType}}} temporary_{{name}}; + j.at("{{baseName}}").get_to(temporary_{{name}}); + o.{{name}} = Pistache::Some(temporary_{{name}}); + }{{/required}} + {{/vars}} +} + +{{#modelNamespaceDeclarations}} +} // {{this}} +{{/modelNamespaceDeclarations}} + +{{/model}} +{{/models}} diff --git a/samples/server/petstore/cpp-oatpp/.openapi-generator-ignore b/samples/server/petstore/cpp-oatpp/.openapi-generator-ignore new file mode 100644 index 00000000000..7484ee590a3 --- /dev/null +++ b/samples/server/petstore/cpp-oatpp/.openapi-generator-ignore @@ -0,0 +1,23 @@ +# OpenAPI Generator Ignore +# Generated by openapi-generator https://github.com/openapitools/openapi-generator + +# Use this file to prevent files from being overwritten by the generator. +# The patterns follow closely to .gitignore or .dockerignore. + +# As an example, the C# client generator defines ApiClient.cs. +# You can make changes and tell OpenAPI Generator to ignore just this file by uncommenting the following line: +#ApiClient.cs + +# You can match any string of characters against a directory, file or extension with a single asterisk (*): +#foo/*/qux +# The above matches foo/bar/qux and foo/baz/qux, but not foo/bar/baz/qux + +# You can recursively match patterns against a directory, file or extension with a double asterisk (**): +#foo/**/qux +# This matches foo/bar/qux, foo/baz/qux, and foo/bar/baz/qux + +# You can also negate patterns with an exclamation (!). +# For example, you can ignore all files in a docs folder with the file extension .md: +#docs/*.md +# Then explicitly reverse the ignore rule for a single file: +#!docs/README.md diff --git a/samples/server/petstore/cpp-oatpp/.openapi-generator/FILES b/samples/server/petstore/cpp-oatpp/.openapi-generator/FILES new file mode 100644 index 00000000000..6610edacc77 --- /dev/null +++ b/samples/server/petstore/cpp-oatpp/.openapi-generator/FILES @@ -0,0 +1,33 @@ +CMakeLists.txt +README.md +api/PetApi.cpp +api/PetApi.h +api/StoreApi.cpp +api/StoreApi.h +api/UserApi.cpp +api/UserApi.h +impl/PetApiImpl.cpp +impl/PetApiImpl.h +impl/StoreApiImpl.cpp +impl/StoreApiImpl.h +impl/UserApiImpl.cpp +impl/UserApiImpl.h +main-api-server.cpp +model/ApiResponse.cpp +model/ApiResponse.h +model/Category.cpp +model/Category.h +model/Helpers.cpp +model/Helpers.h +model/Inline_object.cpp +model/Inline_object.h +model/Inline_object_1.cpp +model/Inline_object_1.h +model/Order.cpp +model/Order.h +model/Pet.cpp +model/Pet.h +model/Tag.cpp +model/Tag.h +model/User.cpp +model/User.h diff --git a/samples/server/petstore/cpp-oatpp/.openapi-generator/VERSION b/samples/server/petstore/cpp-oatpp/.openapi-generator/VERSION new file mode 100644 index 00000000000..d99e7162d01 --- /dev/null +++ b/samples/server/petstore/cpp-oatpp/.openapi-generator/VERSION @@ -0,0 +1 @@ +5.0.0-SNAPSHOT \ No newline at end of file diff --git a/samples/server/petstore/cpp-oatpp/CMakeLists.txt b/samples/server/petstore/cpp-oatpp/CMakeLists.txt new file mode 100644 index 00000000000..992a04bf681 --- /dev/null +++ b/samples/server/petstore/cpp-oatpp/CMakeLists.txt @@ -0,0 +1,37 @@ +cmake_minimum_required (VERSION 3.2) + +project(api-server) + +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 -pg -g3" ) + +include(ExternalProject) + +set(EXTERNAL_INSTALL_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/external) + +ExternalProject_Add(PISTACHE + GIT_REPOSITORY https://github.com/oktal/pistache.git + CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=${EXTERNAL_INSTALL_LOCATION} +) + +ExternalProject_Add(NLOHMANN + GIT_REPOSITORY https://github.com/nlohmann/json.git + CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=${EXTERNAL_INSTALL_LOCATION} + +) +include_directories(${EXTERNAL_INSTALL_LOCATION}/include) +link_directories(${EXTERNAL_INSTALL_LOCATION}/lib) + +include_directories(model) +include_directories(api) +include_directories(impl) + +file(GLOB SRCS + ${CMAKE_CURRENT_SOURCE_DIR}/api/*.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/impl/*.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/model/*.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp +) + +add_executable(${PROJECT_NAME} ${SRCS} ) +add_dependencies(${PROJECT_NAME} PISTACHE NLOHMANN) +target_link_libraries(${PROJECT_NAME} pistache pthread) diff --git a/samples/server/petstore/cpp-oatpp/README.md b/samples/server/petstore/cpp-oatpp/README.md new file mode 100644 index 00000000000..c0089015d48 --- /dev/null +++ b/samples/server/petstore/cpp-oatpp/README.md @@ -0,0 +1,48 @@ +# REST API Server for OpenAPI Petstore + +## Overview +This API Server was generated by the [OpenAPI Generator](https://openapi-generator.tech) project. +It uses the [Oat++](https://github.com/oatpp/oatpp) Framework. + +## Files organization +The Oat++ C++ REST server generator creates three folders: +- `api`: This folder contains the handlers for each method specified in the OpenAPI definition. Every handler extracts +the path and body parameters (if any) from the requests and tries to parse and possibly validate them. +Once this step is completed, the main API class calls the corresponding abstract method that should be implemented +by the developer (a basic implementation is provided under the `impl` folder) +- `impl`: As written above, the implementation folder contains, for each API, the corresponding implementation class, +which extends the main API class and implements the abstract methods. +Every method receives the path and body parameters as constant reference variables and a reference to the response +object, that should be filled with the right response and sent at the end of the method with the command: +response.send(returnCode, responseBody, [mimeType]) +- `model`: This folder contains the corresponding class for every object schema found in the OpenAPI specification. + +The main folder contains also a file with a main that can be used to start the server. +Of course, is you should customize this file based on your needs + +## Installation +First of all, you need to download and install the libraries listed [here](#libraries-required). + +Once the libraries are installed, in order to compile and run the server please follow the steps below: +```bash +mkdir build +cd build +cmake .. +make +``` + +Once compiled run the server: + +```bash +cd build +./api-server +``` + +## Libraries required +- [Oat++](http://github.com/oatpp/oatpp) +- [JSON for Modern C++](https://github.com/nlohmann/json/#integration): Please download the `json.hpp` file and +put it under the model/nlohmann folder + +## Namespaces +org.openapitools.server.api +org.openapitools.server.model diff --git a/samples/server/petstore/cpp-oatpp/api/PetApi.cpp b/samples/server/petstore/cpp-oatpp/api/PetApi.cpp new file mode 100644 index 00000000000..4c9cf01961d --- /dev/null +++ b/samples/server/petstore/cpp-oatpp/api/PetApi.cpp @@ -0,0 +1,236 @@ +/** +* OpenAPI Petstore +* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters. +* +* The version of the OpenAPI document: 1.0.0 +* +* +* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech). +* https://openapi-generator.tech +* Do not edit the class manually. +*/ + +#include "PetApi.h" +#include "Helpers.h" + +namespace org { +namespace openapitools { +namespace server { +namespace api { + +using namespace org::openapitools::server::helpers; +using namespace org::openapitools::server::model; + +PetApi::PetApi(std::shared_ptr rtr) { + router = rtr; +} + +void PetApi::init() { + setupRoutes(); +} + +void PetApi::setupRoutes() { + using namespace Pistache::Rest; + + Routes::Post(*router, base + "/pet", Routes::bind(&PetApi::add_pet_handler, this)); + Routes::Delete(*router, base + "/pet/:petId", Routes::bind(&PetApi::delete_pet_handler, this)); + Routes::Get(*router, base + "/pet/findByStatus", Routes::bind(&PetApi::find_pets_by_status_handler, this)); + Routes::Get(*router, base + "/pet/findByTags", Routes::bind(&PetApi::find_pets_by_tags_handler, this)); + Routes::Get(*router, base + "/pet/:petId", Routes::bind(&PetApi::get_pet_by_id_handler, this)); + Routes::Put(*router, base + "/pet", Routes::bind(&PetApi::update_pet_handler, this)); + Routes::Post(*router, base + "/pet/:petId", Routes::bind(&PetApi::update_pet_with_form_handler, this)); + Routes::Post(*router, base + "/pet/:petId/uploadImage", Routes::bind(&PetApi::upload_file_handler, this)); + + // Default handler, called when a route is not found + router->addCustomHandler(Routes::bind(&PetApi::pet_api_default_handler, this)); +} + +void PetApi::add_pet_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response) { + + // Getting the body param + + Pet pet; + + try { + nlohmann::json::parse(request.body()).get_to(pet); + this->add_pet(pet, response); + } catch (nlohmann::detail::exception &e) { + //send a 400 error + response.send(Pistache::Http::Code::Bad_Request, e.what()); + return; + } catch (Pistache::Http::HttpError &e) { + response.send(static_cast(e.code()), e.what()); + return; + } catch (std::exception &e) { + //send a 500 error + response.send(Pistache::Http::Code::Internal_Server_Error, e.what()); + return; + } + +} +void PetApi::delete_pet_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response) { + // Getting the path params + auto petId = request.param(":petId").as(); + + // Getting the header params + auto apiKey = request.headers().tryGetRaw("api_key"); + + try { + this->delete_pet(petId, apiKey, response); + } catch (nlohmann::detail::exception &e) { + //send a 400 error + response.send(Pistache::Http::Code::Bad_Request, e.what()); + return; + } catch (Pistache::Http::HttpError &e) { + response.send(static_cast(e.code()), e.what()); + return; + } catch (std::exception &e) { + //send a 500 error + response.send(Pistache::Http::Code::Internal_Server_Error, e.what()); + return; + } + +} +void PetApi::find_pets_by_status_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response) { + + // Getting the query params + auto statusQuery = request.query().get("status"); + Pistache::Optional> status; + if(!statusQuery.isEmpty()){ + std::vector valueQuery_instance; + if(fromStringValue(statusQuery.get(), valueQuery_instance)){ + status = Pistache::Some(valueQuery_instance); + } + } + + try { + this->find_pets_by_status(status, response); + } catch (nlohmann::detail::exception &e) { + //send a 400 error + response.send(Pistache::Http::Code::Bad_Request, e.what()); + return; + } catch (Pistache::Http::HttpError &e) { + response.send(static_cast(e.code()), e.what()); + return; + } catch (std::exception &e) { + //send a 500 error + response.send(Pistache::Http::Code::Internal_Server_Error, e.what()); + return; + } + +} +void PetApi::find_pets_by_tags_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response) { + + // Getting the query params + auto tagsQuery = request.query().get("tags"); + Pistache::Optional> tags; + if(!tagsQuery.isEmpty()){ + std::vector valueQuery_instance; + if(fromStringValue(tagsQuery.get(), valueQuery_instance)){ + tags = Pistache::Some(valueQuery_instance); + } + } + + try { + this->find_pets_by_tags(tags, response); + } catch (nlohmann::detail::exception &e) { + //send a 400 error + response.send(Pistache::Http::Code::Bad_Request, e.what()); + return; + } catch (Pistache::Http::HttpError &e) { + response.send(static_cast(e.code()), e.what()); + return; + } catch (std::exception &e) { + //send a 500 error + response.send(Pistache::Http::Code::Internal_Server_Error, e.what()); + return; + } + +} +void PetApi::get_pet_by_id_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response) { + // Getting the path params + auto petId = request.param(":petId").as(); + + try { + this->get_pet_by_id(petId, response); + } catch (nlohmann::detail::exception &e) { + //send a 400 error + response.send(Pistache::Http::Code::Bad_Request, e.what()); + return; + } catch (Pistache::Http::HttpError &e) { + response.send(static_cast(e.code()), e.what()); + return; + } catch (std::exception &e) { + //send a 500 error + response.send(Pistache::Http::Code::Internal_Server_Error, e.what()); + return; + } + +} +void PetApi::update_pet_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response) { + + // Getting the body param + + Pet pet; + + try { + nlohmann::json::parse(request.body()).get_to(pet); + this->update_pet(pet, response); + } catch (nlohmann::detail::exception &e) { + //send a 400 error + response.send(Pistache::Http::Code::Bad_Request, e.what()); + return; + } catch (Pistache::Http::HttpError &e) { + response.send(static_cast(e.code()), e.what()); + return; + } catch (std::exception &e) { + //send a 500 error + response.send(Pistache::Http::Code::Internal_Server_Error, e.what()); + return; + } + +} +void PetApi::update_pet_with_form_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response) { + try { + this->update_pet_with_form(request, response); + } catch (nlohmann::detail::exception &e) { + //send a 400 error + response.send(Pistache::Http::Code::Bad_Request, e.what()); + return; + } catch (Pistache::Http::HttpError &e) { + response.send(static_cast(e.code()), e.what()); + return; + } catch (std::exception &e) { + //send a 500 error + response.send(Pistache::Http::Code::Internal_Server_Error, e.what()); + return; + } + +} +void PetApi::upload_file_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response) { + try { + this->upload_file(request, response); + } catch (nlohmann::detail::exception &e) { + //send a 400 error + response.send(Pistache::Http::Code::Bad_Request, e.what()); + return; + } catch (Pistache::Http::HttpError &e) { + response.send(static_cast(e.code()), e.what()); + return; + } catch (std::exception &e) { + //send a 500 error + response.send(Pistache::Http::Code::Internal_Server_Error, e.what()); + return; + } + +} + +void PetApi::pet_api_default_handler(const Pistache::Rest::Request &, Pistache::Http::ResponseWriter response) { + response.send(Pistache::Http::Code::Not_Found, "The requested method does not exist"); +} + +} +} +} +} + diff --git a/samples/server/petstore/cpp-oatpp/api/PetApi.h b/samples/server/petstore/cpp-oatpp/api/PetApi.h new file mode 100644 index 00000000000..03973d6741f --- /dev/null +++ b/samples/server/petstore/cpp-oatpp/api/PetApi.h @@ -0,0 +1,141 @@ +/** +* OpenAPI Petstore +* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters. +* +* The version of the OpenAPI document: 1.0.0 +* +* +* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech). +* https://openapi-generator.tech +* Do not edit the class manually. +*/ +/* + * PetApi.h + * + * + */ + +#ifndef PetApi_H_ +#define PetApi_H_ + + +#include +#include +#include +#include + + +#include "ApiResponse.h" +#include "Pet.h" +#include + +namespace org { +namespace openapitools { +namespace server { +namespace api { + +using namespace org::openapitools::server::model; + +class PetApi { +public: + PetApi(std::shared_ptr); + virtual ~PetApi() {} + void init(); + + const std::string base = "/v2"; + +private: + void setupRoutes(); + + void add_pet_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response); + void delete_pet_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response); + void find_pets_by_status_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response); + void find_pets_by_tags_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response); + void get_pet_by_id_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response); + void update_pet_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response); + void update_pet_with_form_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response); + void upload_file_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response); + void pet_api_default_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response); + + std::shared_ptr router; + + /// + /// Add a new pet to the store + /// + /// + /// + /// + /// Pet object that needs to be added to the store + virtual void add_pet(const Pet &pet, Pistache::Http::ResponseWriter &response) = 0; + + /// + /// Deletes a pet + /// + /// + /// + /// + /// Pet id to delete + /// (optional, default to "") + virtual void delete_pet(const int64_t &petId, const Oatpp::Optional &apiKey, Pistache::Http::ResponseWriter &response) = 0; + + /// + /// Finds Pets by status + /// + /// + /// Multiple status values can be provided with comma separated strings + /// + /// Status values that need to be considered for filter + virtual void find_pets_by_status(const Oatpp::Optional> &status, Pistache::Http::ResponseWriter &response) = 0; + + /// + /// Finds Pets by tags + /// + /// + /// Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing. + /// + /// Tags to filter by + virtual void find_pets_by_tags(const Oatpp::Optional> &tags, Pistache::Http::ResponseWriter &response) = 0; + + /// + /// Find pet by ID + /// + /// + /// Returns a single pet + /// + /// ID of pet to return + virtual void get_pet_by_id(const int64_t &petId, Pistache::Http::ResponseWriter &response) = 0; + + /// + /// Update an existing pet + /// + /// + /// + /// + /// Pet object that needs to be added to the store + virtual void update_pet(const Pet &pet, Pistache::Http::ResponseWriter &response) = 0; + + /// + /// Updates a pet in the store with form data + /// + /// + /// + /// + virtual void update_pet_with_form(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter &response) = 0; + + /// + /// uploads an image + /// + /// + /// + /// + virtual void upload_file(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter &response) = 0; + +}; + +} +} +} +} + +#endif /* PetApi_H_ */ + diff --git a/samples/server/petstore/cpp-oatpp/api/StoreApi.cpp b/samples/server/petstore/cpp-oatpp/api/StoreApi.cpp new file mode 100644 index 00000000000..140c2c101bd --- /dev/null +++ b/samples/server/petstore/cpp-oatpp/api/StoreApi.cpp @@ -0,0 +1,134 @@ +/** +* OpenAPI Petstore +* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters. +* +* The version of the OpenAPI document: 1.0.0 +* +* +* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech). +* https://openapi-generator.tech +* Do not edit the class manually. +*/ + +#include "StoreApi.h" +#include "Helpers.h" + +namespace org { +namespace openapitools { +namespace server { +namespace api { + +using namespace org::openapitools::server::helpers; +using namespace org::openapitools::server::model; + +StoreApi::StoreApi(std::shared_ptr rtr) { + router = rtr; +} + +void StoreApi::init() { + setupRoutes(); +} + +void StoreApi::setupRoutes() { + using namespace Pistache::Rest; + + Routes::Delete(*router, base + "/store/order/:orderId", Routes::bind(&StoreApi::delete_order_handler, this)); + Routes::Get(*router, base + "/store/inventory", Routes::bind(&StoreApi::get_inventory_handler, this)); + Routes::Get(*router, base + "/store/order/:orderId", Routes::bind(&StoreApi::get_order_by_id_handler, this)); + Routes::Post(*router, base + "/store/order", Routes::bind(&StoreApi::place_order_handler, this)); + + // Default handler, called when a route is not found + router->addCustomHandler(Routes::bind(&StoreApi::store_api_default_handler, this)); +} + +void StoreApi::delete_order_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response) { + // Getting the path params + auto orderId = request.param(":orderId").as(); + + try { + this->delete_order(orderId, response); + } catch (nlohmann::detail::exception &e) { + //send a 400 error + response.send(Pistache::Http::Code::Bad_Request, e.what()); + return; + } catch (Pistache::Http::HttpError &e) { + response.send(static_cast(e.code()), e.what()); + return; + } catch (std::exception &e) { + //send a 500 error + response.send(Pistache::Http::Code::Internal_Server_Error, e.what()); + return; + } + +} +void StoreApi::get_inventory_handler(const Pistache::Rest::Request &, Pistache::Http::ResponseWriter response) { + + try { + this->get_inventory(response); + } catch (nlohmann::detail::exception &e) { + //send a 400 error + response.send(Pistache::Http::Code::Bad_Request, e.what()); + return; + } catch (Pistache::Http::HttpError &e) { + response.send(static_cast(e.code()), e.what()); + return; + } catch (std::exception &e) { + //send a 500 error + response.send(Pistache::Http::Code::Internal_Server_Error, e.what()); + return; + } + +} +void StoreApi::get_order_by_id_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response) { + // Getting the path params + auto orderId = request.param(":orderId").as(); + + try { + this->get_order_by_id(orderId, response); + } catch (nlohmann::detail::exception &e) { + //send a 400 error + response.send(Pistache::Http::Code::Bad_Request, e.what()); + return; + } catch (Pistache::Http::HttpError &e) { + response.send(static_cast(e.code()), e.what()); + return; + } catch (std::exception &e) { + //send a 500 error + response.send(Pistache::Http::Code::Internal_Server_Error, e.what()); + return; + } + +} +void StoreApi::place_order_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response) { + + // Getting the body param + + Order order; + + try { + nlohmann::json::parse(request.body()).get_to(order); + this->place_order(order, response); + } catch (nlohmann::detail::exception &e) { + //send a 400 error + response.send(Pistache::Http::Code::Bad_Request, e.what()); + return; + } catch (Pistache::Http::HttpError &e) { + response.send(static_cast(e.code()), e.what()); + return; + } catch (std::exception &e) { + //send a 500 error + response.send(Pistache::Http::Code::Internal_Server_Error, e.what()); + return; + } + +} + +void StoreApi::store_api_default_handler(const Pistache::Rest::Request &, Pistache::Http::ResponseWriter response) { + response.send(Pistache::Http::Code::Not_Found, "The requested method does not exist"); +} + +} +} +} +} + diff --git a/samples/server/petstore/cpp-oatpp/api/StoreApi.h b/samples/server/petstore/cpp-oatpp/api/StoreApi.h new file mode 100644 index 00000000000..9c11a8304b2 --- /dev/null +++ b/samples/server/petstore/cpp-oatpp/api/StoreApi.h @@ -0,0 +1,101 @@ +/** +* OpenAPI Petstore +* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters. +* +* The version of the OpenAPI document: 1.0.0 +* +* +* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech). +* https://openapi-generator.tech +* Do not edit the class manually. +*/ +/* + * StoreApi.h + * + * + */ + +#ifndef StoreApi_H_ +#define StoreApi_H_ + + +#include +#include +#include +#include + + +#include "Order.h" +#include +#include + +namespace org { +namespace openapitools { +namespace server { +namespace api { + +using namespace org::openapitools::server::model; + +class StoreApi { +public: + StoreApi(std::shared_ptr); + virtual ~StoreApi() {} + void init(); + + const std::string base = "/v2"; + +private: + void setupRoutes(); + + void delete_order_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response); + void get_inventory_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response); + void get_order_by_id_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response); + void place_order_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response); + void store_api_default_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response); + + std::shared_ptr router; + + /// + /// Delete purchase order by ID + /// + /// + /// For valid response try integer IDs with value < 1000. Anything above 1000 or nonintegers will generate API errors + /// + /// ID of the order that needs to be deleted + virtual void delete_order(const std::string &orderId, Pistache::Http::ResponseWriter &response) = 0; + + /// + /// Returns pet inventories by status + /// + /// + /// Returns a map of status codes to quantities + /// + virtual void get_inventory(Pistache::Http::ResponseWriter &response) = 0; + + /// + /// Find purchase order by ID + /// + /// + /// For valid response try integer IDs with value <= 5 or > 10. Other values will generated exceptions + /// + /// ID of pet that needs to be fetched + virtual void get_order_by_id(const int64_t &orderId, Pistache::Http::ResponseWriter &response) = 0; + + /// + /// Place an order for a pet + /// + /// + /// + /// + /// order placed for purchasing the pet + virtual void place_order(const Order &order, Pistache::Http::ResponseWriter &response) = 0; + +}; + +} +} +} +} + +#endif /* StoreApi_H_ */ + diff --git a/samples/server/petstore/cpp-oatpp/api/UserApi.cpp b/samples/server/petstore/cpp-oatpp/api/UserApi.cpp new file mode 100644 index 00000000000..9ce7b4e0aa0 --- /dev/null +++ b/samples/server/petstore/cpp-oatpp/api/UserApi.cpp @@ -0,0 +1,243 @@ +/** +* OpenAPI Petstore +* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters. +* +* The version of the OpenAPI document: 1.0.0 +* +* +* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech). +* https://openapi-generator.tech +* Do not edit the class manually. +*/ + +#include "UserApi.h" +#include "Helpers.h" + +namespace org { +namespace openapitools { +namespace server { +namespace api { + +using namespace org::openapitools::server::helpers; +using namespace org::openapitools::server::model; + +UserApi::UserApi(std::shared_ptr rtr) { + router = rtr; +} + +void UserApi::init() { + setupRoutes(); +} + +void UserApi::setupRoutes() { + using namespace Pistache::Rest; + + Routes::Post(*router, base + "/user", Routes::bind(&UserApi::create_user_handler, this)); + Routes::Post(*router, base + "/user/createWithArray", Routes::bind(&UserApi::create_users_with_array_input_handler, this)); + Routes::Post(*router, base + "/user/createWithList", Routes::bind(&UserApi::create_users_with_list_input_handler, this)); + Routes::Delete(*router, base + "/user/:username", Routes::bind(&UserApi::delete_user_handler, this)); + Routes::Get(*router, base + "/user/:username", Routes::bind(&UserApi::get_user_by_name_handler, this)); + Routes::Get(*router, base + "/user/login", Routes::bind(&UserApi::login_user_handler, this)); + Routes::Get(*router, base + "/user/logout", Routes::bind(&UserApi::logout_user_handler, this)); + Routes::Put(*router, base + "/user/:username", Routes::bind(&UserApi::update_user_handler, this)); + + // Default handler, called when a route is not found + router->addCustomHandler(Routes::bind(&UserApi::user_api_default_handler, this)); +} + +void UserApi::create_user_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response) { + + // Getting the body param + + User user; + + try { + nlohmann::json::parse(request.body()).get_to(user); + this->create_user(user, response); + } catch (nlohmann::detail::exception &e) { + //send a 400 error + response.send(Pistache::Http::Code::Bad_Request, e.what()); + return; + } catch (Pistache::Http::HttpError &e) { + response.send(static_cast(e.code()), e.what()); + return; + } catch (std::exception &e) { + //send a 500 error + response.send(Pistache::Http::Code::Internal_Server_Error, e.what()); + return; + } + +} +void UserApi::create_users_with_array_input_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response) { + + // Getting the body param + std::vector user; + + try { + nlohmann::json::parse(request.body()).get_to(user); + this->create_users_with_array_input(user, response); + } catch (nlohmann::detail::exception &e) { + //send a 400 error + response.send(Pistache::Http::Code::Bad_Request, e.what()); + return; + } catch (Pistache::Http::HttpError &e) { + response.send(static_cast(e.code()), e.what()); + return; + } catch (std::exception &e) { + //send a 500 error + response.send(Pistache::Http::Code::Internal_Server_Error, e.what()); + return; + } + +} +void UserApi::create_users_with_list_input_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response) { + + // Getting the body param + std::vector user; + + try { + nlohmann::json::parse(request.body()).get_to(user); + this->create_users_with_list_input(user, response); + } catch (nlohmann::detail::exception &e) { + //send a 400 error + response.send(Pistache::Http::Code::Bad_Request, e.what()); + return; + } catch (Pistache::Http::HttpError &e) { + response.send(static_cast(e.code()), e.what()); + return; + } catch (std::exception &e) { + //send a 500 error + response.send(Pistache::Http::Code::Internal_Server_Error, e.what()); + return; + } + +} +void UserApi::delete_user_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response) { + // Getting the path params + auto username = request.param(":username").as(); + + try { + this->delete_user(username, response); + } catch (nlohmann::detail::exception &e) { + //send a 400 error + response.send(Pistache::Http::Code::Bad_Request, e.what()); + return; + } catch (Pistache::Http::HttpError &e) { + response.send(static_cast(e.code()), e.what()); + return; + } catch (std::exception &e) { + //send a 500 error + response.send(Pistache::Http::Code::Internal_Server_Error, e.what()); + return; + } + +} +void UserApi::get_user_by_name_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response) { + // Getting the path params + auto username = request.param(":username").as(); + + try { + this->get_user_by_name(username, response); + } catch (nlohmann::detail::exception &e) { + //send a 400 error + response.send(Pistache::Http::Code::Bad_Request, e.what()); + return; + } catch (Pistache::Http::HttpError &e) { + response.send(static_cast(e.code()), e.what()); + return; + } catch (std::exception &e) { + //send a 500 error + response.send(Pistache::Http::Code::Internal_Server_Error, e.what()); + return; + } + +} +void UserApi::login_user_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response) { + + // Getting the query params + auto usernameQuery = request.query().get("username"); + Pistache::Optional username; + if(!usernameQuery.isEmpty()){ + std::string valueQuery_instance; + if(fromStringValue(usernameQuery.get(), valueQuery_instance)){ + username = Pistache::Some(valueQuery_instance); + } + } + auto passwordQuery = request.query().get("password"); + Pistache::Optional password; + if(!passwordQuery.isEmpty()){ + std::string valueQuery_instance; + if(fromStringValue(passwordQuery.get(), valueQuery_instance)){ + password = Pistache::Some(valueQuery_instance); + } + } + + try { + this->login_user(username, password, response); + } catch (nlohmann::detail::exception &e) { + //send a 400 error + response.send(Pistache::Http::Code::Bad_Request, e.what()); + return; + } catch (Pistache::Http::HttpError &e) { + response.send(static_cast(e.code()), e.what()); + return; + } catch (std::exception &e) { + //send a 500 error + response.send(Pistache::Http::Code::Internal_Server_Error, e.what()); + return; + } + +} +void UserApi::logout_user_handler(const Pistache::Rest::Request &, Pistache::Http::ResponseWriter response) { + + try { + this->logout_user(response); + } catch (nlohmann::detail::exception &e) { + //send a 400 error + response.send(Pistache::Http::Code::Bad_Request, e.what()); + return; + } catch (Pistache::Http::HttpError &e) { + response.send(static_cast(e.code()), e.what()); + return; + } catch (std::exception &e) { + //send a 500 error + response.send(Pistache::Http::Code::Internal_Server_Error, e.what()); + return; + } + +} +void UserApi::update_user_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response) { + // Getting the path params + auto username = request.param(":username").as(); + + // Getting the body param + + User user; + + try { + nlohmann::json::parse(request.body()).get_to(user); + this->update_user(username, user, response); + } catch (nlohmann::detail::exception &e) { + //send a 400 error + response.send(Pistache::Http::Code::Bad_Request, e.what()); + return; + } catch (Pistache::Http::HttpError &e) { + response.send(static_cast(e.code()), e.what()); + return; + } catch (std::exception &e) { + //send a 500 error + response.send(Pistache::Http::Code::Internal_Server_Error, e.what()); + return; + } + +} + +void UserApi::user_api_default_handler(const Pistache::Rest::Request &, Pistache::Http::ResponseWriter response) { + response.send(Pistache::Http::Code::Not_Found, "The requested method does not exist"); +} + +} +} +} +} + diff --git a/samples/server/petstore/cpp-oatpp/api/UserApi.h b/samples/server/petstore/cpp-oatpp/api/UserApi.h new file mode 100644 index 00000000000..e926253cf48 --- /dev/null +++ b/samples/server/petstore/cpp-oatpp/api/UserApi.h @@ -0,0 +1,143 @@ +/** +* OpenAPI Petstore +* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters. +* +* The version of the OpenAPI document: 1.0.0 +* +* +* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech). +* https://openapi-generator.tech +* Do not edit the class manually. +*/ +/* + * UserApi.h + * + * + */ + +#ifndef UserApi_H_ +#define UserApi_H_ + + +#include +#include +#include +#include + + +#include "User.h" +#include +#include + +namespace org { +namespace openapitools { +namespace server { +namespace api { + +using namespace org::openapitools::server::model; + +class UserApi { +public: + UserApi(std::shared_ptr); + virtual ~UserApi() {} + void init(); + + const std::string base = "/v2"; + +private: + void setupRoutes(); + + void create_user_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response); + void create_users_with_array_input_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response); + void create_users_with_list_input_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response); + void delete_user_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response); + void get_user_by_name_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response); + void login_user_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response); + void logout_user_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response); + void update_user_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response); + void user_api_default_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response); + + std::shared_ptr router; + + /// + /// Create user + /// + /// + /// This can only be done by the logged in user. + /// + /// Created user object + virtual void create_user(const User &user, Pistache::Http::ResponseWriter &response) = 0; + + /// + /// Creates list of users with given input array + /// + /// + /// + /// + /// List of user object + virtual void create_users_with_array_input(const std::vector &user, Pistache::Http::ResponseWriter &response) = 0; + + /// + /// Creates list of users with given input array + /// + /// + /// + /// + /// List of user object + virtual void create_users_with_list_input(const std::vector &user, Pistache::Http::ResponseWriter &response) = 0; + + /// + /// Delete user + /// + /// + /// This can only be done by the logged in user. + /// + /// The name that needs to be deleted + virtual void delete_user(const std::string &username, Pistache::Http::ResponseWriter &response) = 0; + + /// + /// Get user by user name + /// + /// + /// + /// + /// The name that needs to be fetched. Use user1 for testing. + virtual void get_user_by_name(const std::string &username, Pistache::Http::ResponseWriter &response) = 0; + + /// + /// Logs user into the system + /// + /// + /// + /// + /// The user name for login + /// The password for login in clear text + virtual void login_user(const Oatpp::Optional &username, const Oatpp::Optional &password, Pistache::Http::ResponseWriter &response) = 0; + + /// + /// Logs out current logged in user session + /// + /// + /// + /// + virtual void logout_user(Pistache::Http::ResponseWriter &response) = 0; + + /// + /// Updated user + /// + /// + /// This can only be done by the logged in user. + /// + /// name that need to be deleted + /// Updated user object + virtual void update_user(const std::string &username, const User &user, Pistache::Http::ResponseWriter &response) = 0; + +}; + +} +} +} +} + +#endif /* UserApi_H_ */ + diff --git a/samples/server/petstore/cpp-oatpp/impl/PetApiImpl.cpp b/samples/server/petstore/cpp-oatpp/impl/PetApiImpl.cpp new file mode 100644 index 00000000000..fb41d03d5d4 --- /dev/null +++ b/samples/server/petstore/cpp-oatpp/impl/PetApiImpl.cpp @@ -0,0 +1,55 @@ +/** +* OpenAPI Petstore +* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters. +* +* The version of the OpenAPI document: 1.0.0 +* +* +* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech). +* https://openapi-generator.tech +* Do not edit the class manually. +*/ + +#include "PetApiImpl.h" + +namespace org { +namespace openapitools { +namespace server { +namespace api { + +using namespace org::openapitools::server::model; + +PetApiImpl::PetApiImpl(std::shared_ptr rtr) + : PetApi(rtr) + { } + +void PetApiImpl::add_pet(const Pet &pet, Pistache::Http::ResponseWriter &response) { + response.send(Pistache::Http::Code::Ok, "Do some magic\n"); +} +void PetApiImpl::delete_pet(const int64_t &petId, const Oatpp::Optional &apiKey, Pistache::Http::ResponseWriter &response) { + response.send(Pistache::Http::Code::Ok, "Do some magic\n"); +} +void PetApiImpl::find_pets_by_status(const Oatpp::Optional> &status, Pistache::Http::ResponseWriter &response) { + response.send(Pistache::Http::Code::Ok, "Do some magic\n"); +} +void PetApiImpl::find_pets_by_tags(const Oatpp::Optional> &tags, Pistache::Http::ResponseWriter &response) { + response.send(Pistache::Http::Code::Ok, "Do some magic\n"); +} +void PetApiImpl::get_pet_by_id(const int64_t &petId, Pistache::Http::ResponseWriter &response) { + response.send(Pistache::Http::Code::Ok, "Do some magic\n"); +} +void PetApiImpl::update_pet(const Pet &pet, Pistache::Http::ResponseWriter &response) { + response.send(Pistache::Http::Code::Ok, "Do some magic\n"); +} +void PetApiImpl::update_pet_with_form(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter &response){ + response.send(Pistache::Http::Code::Ok, "Do some magic\n"); +} +void PetApiImpl::upload_file(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter &response){ + response.send(Pistache::Http::Code::Ok, "Do some magic\n"); +} + +} +} +} +} + diff --git a/samples/server/petstore/cpp-oatpp/impl/PetApiImpl.h b/samples/server/petstore/cpp-oatpp/impl/PetApiImpl.h new file mode 100644 index 00000000000..393b458d5b0 --- /dev/null +++ b/samples/server/petstore/cpp-oatpp/impl/PetApiImpl.h @@ -0,0 +1,66 @@ +/** +* OpenAPI Petstore +* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters. +* +* The version of the OpenAPI document: 1.0.0 +* +* +* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech). +* https://openapi-generator.tech +* Do not edit the class manually. +*/ + +/* +* PetApiImpl.h +* +* +*/ + +#ifndef PET_API_IMPL_H_ +#define PET_API_IMPL_H_ + + +#include +#include +#include +#include + +#include + +#include + +#include "ApiResponse.h" +#include "Pet.h" +#include + +namespace org { +namespace openapitools { +namespace server { +namespace api { + +using namespace org::openapitools::server::model; + +class PetApiImpl : public org::openapitools::server::api::PetApi { +public: + PetApiImpl(std::shared_ptr); + ~PetApiImpl() {} + + void add_pet(const Pet &pet, Pistache::Http::ResponseWriter &response); + void delete_pet(const int64_t &petId, const Oatpp::Optional &apiKey, Pistache::Http::ResponseWriter &response); + void find_pets_by_status(const Oatpp::Optional> &status, Pistache::Http::ResponseWriter &response); + void find_pets_by_tags(const Oatpp::Optional> &tags, Pistache::Http::ResponseWriter &response); + void get_pet_by_id(const int64_t &petId, Pistache::Http::ResponseWriter &response); + void update_pet(const Pet &pet, Pistache::Http::ResponseWriter &response); + void update_pet_with_form(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter &response); + void upload_file(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter &response); + +}; + +} +} +} +} + + + +#endif \ No newline at end of file diff --git a/samples/server/petstore/cpp-oatpp/impl/StoreApiImpl.cpp b/samples/server/petstore/cpp-oatpp/impl/StoreApiImpl.cpp new file mode 100644 index 00000000000..221c99be9b0 --- /dev/null +++ b/samples/server/petstore/cpp-oatpp/impl/StoreApiImpl.cpp @@ -0,0 +1,43 @@ +/** +* OpenAPI Petstore +* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters. +* +* The version of the OpenAPI document: 1.0.0 +* +* +* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech). +* https://openapi-generator.tech +* Do not edit the class manually. +*/ + +#include "StoreApiImpl.h" + +namespace org { +namespace openapitools { +namespace server { +namespace api { + +using namespace org::openapitools::server::model; + +StoreApiImpl::StoreApiImpl(std::shared_ptr rtr) + : StoreApi(rtr) + { } + +void StoreApiImpl::delete_order(const std::string &orderId, Pistache::Http::ResponseWriter &response) { + response.send(Pistache::Http::Code::Ok, "Do some magic\n"); +} +void StoreApiImpl::get_inventory(Pistache::Http::ResponseWriter &response) { + response.send(Pistache::Http::Code::Ok, "Do some magic\n"); +} +void StoreApiImpl::get_order_by_id(const int64_t &orderId, Pistache::Http::ResponseWriter &response) { + response.send(Pistache::Http::Code::Ok, "Do some magic\n"); +} +void StoreApiImpl::place_order(const Order &order, Pistache::Http::ResponseWriter &response) { + response.send(Pistache::Http::Code::Ok, "Do some magic\n"); +} + +} +} +} +} + diff --git a/samples/server/petstore/cpp-oatpp/impl/StoreApiImpl.h b/samples/server/petstore/cpp-oatpp/impl/StoreApiImpl.h new file mode 100644 index 00000000000..502afe0f347 --- /dev/null +++ b/samples/server/petstore/cpp-oatpp/impl/StoreApiImpl.h @@ -0,0 +1,62 @@ +/** +* OpenAPI Petstore +* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters. +* +* The version of the OpenAPI document: 1.0.0 +* +* +* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech). +* https://openapi-generator.tech +* Do not edit the class manually. +*/ + +/* +* StoreApiImpl.h +* +* +*/ + +#ifndef STORE_API_IMPL_H_ +#define STORE_API_IMPL_H_ + + +#include +#include +#include +#include + +#include + +#include + +#include "Order.h" +#include +#include + +namespace org { +namespace openapitools { +namespace server { +namespace api { + +using namespace org::openapitools::server::model; + +class StoreApiImpl : public org::openapitools::server::api::StoreApi { +public: + StoreApiImpl(std::shared_ptr); + ~StoreApiImpl() {} + + void delete_order(const std::string &orderId, Pistache::Http::ResponseWriter &response); + void get_inventory(Pistache::Http::ResponseWriter &response); + void get_order_by_id(const int64_t &orderId, Pistache::Http::ResponseWriter &response); + void place_order(const Order &order, Pistache::Http::ResponseWriter &response); + +}; + +} +} +} +} + + + +#endif \ No newline at end of file diff --git a/samples/server/petstore/cpp-oatpp/impl/UserApiImpl.cpp b/samples/server/petstore/cpp-oatpp/impl/UserApiImpl.cpp new file mode 100644 index 00000000000..38f6103a0a0 --- /dev/null +++ b/samples/server/petstore/cpp-oatpp/impl/UserApiImpl.cpp @@ -0,0 +1,55 @@ +/** +* OpenAPI Petstore +* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters. +* +* The version of the OpenAPI document: 1.0.0 +* +* +* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech). +* https://openapi-generator.tech +* Do not edit the class manually. +*/ + +#include "UserApiImpl.h" + +namespace org { +namespace openapitools { +namespace server { +namespace api { + +using namespace org::openapitools::server::model; + +UserApiImpl::UserApiImpl(std::shared_ptr rtr) + : UserApi(rtr) + { } + +void UserApiImpl::create_user(const User &user, Pistache::Http::ResponseWriter &response) { + response.send(Pistache::Http::Code::Ok, "Do some magic\n"); +} +void UserApiImpl::create_users_with_array_input(const std::vector &user, Pistache::Http::ResponseWriter &response) { + response.send(Pistache::Http::Code::Ok, "Do some magic\n"); +} +void UserApiImpl::create_users_with_list_input(const std::vector &user, Pistache::Http::ResponseWriter &response) { + response.send(Pistache::Http::Code::Ok, "Do some magic\n"); +} +void UserApiImpl::delete_user(const std::string &username, Pistache::Http::ResponseWriter &response) { + response.send(Pistache::Http::Code::Ok, "Do some magic\n"); +} +void UserApiImpl::get_user_by_name(const std::string &username, Pistache::Http::ResponseWriter &response) { + response.send(Pistache::Http::Code::Ok, "Do some magic\n"); +} +void UserApiImpl::login_user(const Oatpp::Optional &username, const Oatpp::Optional &password, Pistache::Http::ResponseWriter &response) { + response.send(Pistache::Http::Code::Ok, "Do some magic\n"); +} +void UserApiImpl::logout_user(Pistache::Http::ResponseWriter &response) { + response.send(Pistache::Http::Code::Ok, "Do some magic\n"); +} +void UserApiImpl::update_user(const std::string &username, const User &user, Pistache::Http::ResponseWriter &response) { + response.send(Pistache::Http::Code::Ok, "Do some magic\n"); +} + +} +} +} +} + diff --git a/samples/server/petstore/cpp-oatpp/impl/UserApiImpl.h b/samples/server/petstore/cpp-oatpp/impl/UserApiImpl.h new file mode 100644 index 00000000000..04c17ff6d94 --- /dev/null +++ b/samples/server/petstore/cpp-oatpp/impl/UserApiImpl.h @@ -0,0 +1,66 @@ +/** +* OpenAPI Petstore +* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters. +* +* The version of the OpenAPI document: 1.0.0 +* +* +* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech). +* https://openapi-generator.tech +* Do not edit the class manually. +*/ + +/* +* UserApiImpl.h +* +* +*/ + +#ifndef USER_API_IMPL_H_ +#define USER_API_IMPL_H_ + + +#include +#include +#include +#include + +#include + +#include + +#include "User.h" +#include +#include + +namespace org { +namespace openapitools { +namespace server { +namespace api { + +using namespace org::openapitools::server::model; + +class UserApiImpl : public org::openapitools::server::api::UserApi { +public: + UserApiImpl(std::shared_ptr); + ~UserApiImpl() {} + + void create_user(const User &user, Pistache::Http::ResponseWriter &response); + void create_users_with_array_input(const std::vector &user, Pistache::Http::ResponseWriter &response); + void create_users_with_list_input(const std::vector &user, Pistache::Http::ResponseWriter &response); + void delete_user(const std::string &username, Pistache::Http::ResponseWriter &response); + void get_user_by_name(const std::string &username, Pistache::Http::ResponseWriter &response); + void login_user(const Oatpp::Optional &username, const Oatpp::Optional &password, Pistache::Http::ResponseWriter &response); + void logout_user(Pistache::Http::ResponseWriter &response); + void update_user(const std::string &username, const User &user, Pistache::Http::ResponseWriter &response); + +}; + +} +} +} +} + + + +#endif \ No newline at end of file diff --git a/samples/server/petstore/cpp-oatpp/main-api-server.cpp b/samples/server/petstore/cpp-oatpp/main-api-server.cpp new file mode 100644 index 00000000000..00799608e73 --- /dev/null +++ b/samples/server/petstore/cpp-oatpp/main-api-server.cpp @@ -0,0 +1,95 @@ +/** +* OpenAPI Petstore +* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters. +* +* The version of the OpenAPI document: 1.0.0 +* +* +* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech). +* https://openapi-generator.tech +* Do not edit the class manually. +*/ + + +#include "pistache/endpoint.h" +#include "pistache/http.h" +#include "pistache/router.h" +#ifdef __linux__ +#include +#include +#include +#endif + +#include "PetApiImpl.h" +#include "StoreApiImpl.h" +#include "UserApiImpl.h" + +#define PISTACHE_SERVER_THREADS 2 +#define PISTACHE_SERVER_MAX_REQUEST_SIZE 32768 +#define PISTACHE_SERVER_MAX_RESPONSE_SIZE 32768 + +static Pistache::Http::Endpoint *httpEndpoint; +#ifdef __linux__ +static void sigHandler [[noreturn]] (int sig){ + switch(sig){ + case SIGINT: + case SIGQUIT: + case SIGTERM: + case SIGHUP: + default: + httpEndpoint->shutdown(); + break; + } + exit(0); +} + +static void setUpUnixSignals(std::vector quitSignals) { + sigset_t blocking_mask; + sigemptyset(&blocking_mask); + for (auto sig : quitSignals) + sigaddset(&blocking_mask, sig); + + struct sigaction sa; + sa.sa_handler = sigHandler; + sa.sa_mask = blocking_mask; + sa.sa_flags = 0; + + for (auto sig : quitSignals) + sigaction(sig, &sa, nullptr); +} +#endif + +using namespace org::openapitools::server::api; + +int main() { +#ifdef __linux__ + std::vector sigs{SIGQUIT, SIGINT, SIGTERM, SIGHUP}; + setUpUnixSignals(sigs); +#endif + Pistache::Address addr(Pistache::Ipv4::any(), Pistache::Port(8080)); + + httpEndpoint = new Pistache::Http::Endpoint((addr)); + auto router = std::make_shared(); + + auto opts = Pistache::Http::Endpoint::options() + .threads(PISTACHE_SERVER_THREADS); + opts.flags(Pistache::Tcp::Options::ReuseAddr); + opts.maxRequestSize(PISTACHE_SERVER_MAX_REQUEST_SIZE); + opts.maxResponseSize(PISTACHE_SERVER_MAX_RESPONSE_SIZE); + httpEndpoint->init(opts); + + + PetApiImpl PetApiserver(router); + PetApiserver.init(); + StoreApiImpl StoreApiserver(router); + StoreApiserver.init(); + UserApiImpl UserApiserver(router); + UserApiserver.init(); + + httpEndpoint->setHandler(router->handler()); + httpEndpoint->serve(); + + httpEndpoint->shutdown(); + +} + diff --git a/samples/server/petstore/cpp-oatpp/model/ApiResponse.cpp b/samples/server/petstore/cpp-oatpp/model/ApiResponse.cpp new file mode 100644 index 00000000000..77cd466e145 --- /dev/null +++ b/samples/server/petstore/cpp-oatpp/model/ApiResponse.cpp @@ -0,0 +1,127 @@ +/** +* OpenAPI Petstore +* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters. +* +* The version of the OpenAPI document: 1.0.0 +* +* +* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech). +* https://openapi-generator.tech +* Do not edit the class manually. +*/ + + +#include "ApiResponse.h" + +namespace org { +namespace openapitools { +namespace server { +namespace model { + +ApiResponse::ApiResponse() +{ + m_Code = 0; + m_CodeIsSet = false; + m_Type = ""; + m_TypeIsSet = false; + m_Message = ""; + m_MessageIsSet = false; + +} + +ApiResponse::~ApiResponse() +{ +} + +void ApiResponse::validate() +{ + // TODO: implement validation +} + +void to_json(nlohmann::json& j, const ApiResponse& o) +{ + j = nlohmann::json(); + if(o.codeIsSet()) + j["code"] = o.m_Code; + if(o.typeIsSet()) + j["type"] = o.m_Type; + if(o.messageIsSet()) + j["message"] = o.m_Message; +} + +void from_json(const nlohmann::json& j, ApiResponse& o) +{ + if(j.find("code") != j.end()) + { + j.at("code").get_to(o.m_Code); + o.m_CodeIsSet = true; + } + if(j.find("type") != j.end()) + { + j.at("type").get_to(o.m_Type); + o.m_TypeIsSet = true; + } + if(j.find("message") != j.end()) + { + j.at("message").get_to(o.m_Message); + o.m_MessageIsSet = true; + } +} + +int32_t ApiResponse::getCode() const +{ + return m_Code; +} +void ApiResponse::setCode(int32_t const value) +{ + m_Code = value; + m_CodeIsSet = true; +} +bool ApiResponse::codeIsSet() const +{ + return m_CodeIsSet; +} +void ApiResponse::unsetCode() +{ + m_CodeIsSet = false; +} +std::string ApiResponse::getType() const +{ + return m_Type; +} +void ApiResponse::setType(std::string const& value) +{ + m_Type = value; + m_TypeIsSet = true; +} +bool ApiResponse::typeIsSet() const +{ + return m_TypeIsSet; +} +void ApiResponse::unsetType() +{ + m_TypeIsSet = false; +} +std::string ApiResponse::getMessage() const +{ + return m_Message; +} +void ApiResponse::setMessage(std::string const& value) +{ + m_Message = value; + m_MessageIsSet = true; +} +bool ApiResponse::messageIsSet() const +{ + return m_MessageIsSet; +} +void ApiResponse::unsetMessage() +{ + m_MessageIsSet = false; +} + +} +} +} +} + diff --git a/samples/server/petstore/cpp-oatpp/model/ApiResponse.h b/samples/server/petstore/cpp-oatpp/model/ApiResponse.h new file mode 100644 index 00000000000..7ecb6c9a82e --- /dev/null +++ b/samples/server/petstore/cpp-oatpp/model/ApiResponse.h @@ -0,0 +1,82 @@ +/** +* OpenAPI Petstore +* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters. +* +* The version of the OpenAPI document: 1.0.0 +* +* +* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech). +* https://openapi-generator.tech +* Do not edit the class manually. +*/ +/* + * ApiResponse.h + * + * Describes the result of uploading an image resource + */ + +#ifndef ApiResponse_H_ +#define ApiResponse_H_ + + +#include +#include + +namespace org { +namespace openapitools { +namespace server { +namespace model { + +/// +/// Describes the result of uploading an image resource +/// +class ApiResponse +{ +public: + ApiResponse(); + virtual ~ApiResponse(); + + void validate(); + + ///////////////////////////////////////////// + /// ApiResponse members + + /// + /// + /// + int32_t getCode() const; + void setCode(int32_t const value); + bool codeIsSet() const; + void unsetCode(); + /// + /// + /// + std::string getType() const; + void setType(std::string const& value); + bool typeIsSet() const; + void unsetType(); + /// + /// + /// + std::string getMessage() const; + void setMessage(std::string const& value); + bool messageIsSet() const; + void unsetMessage(); + + friend void to_json(nlohmann::json& j, const ApiResponse& o); + friend void from_json(const nlohmann::json& j, ApiResponse& o); +protected: + int32_t m_Code; + bool m_CodeIsSet; + std::string m_Type; + bool m_TypeIsSet; + std::string m_Message; + bool m_MessageIsSet; +}; + +} +} +} +} + +#endif /* ApiResponse_H_ */ diff --git a/samples/server/petstore/cpp-oatpp/model/Category.cpp b/samples/server/petstore/cpp-oatpp/model/Category.cpp new file mode 100644 index 00000000000..43ff1d49000 --- /dev/null +++ b/samples/server/petstore/cpp-oatpp/model/Category.cpp @@ -0,0 +1,101 @@ +/** +* OpenAPI Petstore +* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters. +* +* The version of the OpenAPI document: 1.0.0 +* +* +* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech). +* https://openapi-generator.tech +* Do not edit the class manually. +*/ + + +#include "Category.h" + +namespace org { +namespace openapitools { +namespace server { +namespace model { + +Category::Category() +{ + m_Id = 0L; + m_IdIsSet = false; + m_Name = ""; + m_NameIsSet = false; + +} + +Category::~Category() +{ +} + +void Category::validate() +{ + // TODO: implement validation +} + +void to_json(nlohmann::json& j, const Category& o) +{ + j = nlohmann::json(); + if(o.idIsSet()) + j["id"] = o.m_Id; + if(o.nameIsSet()) + j["name"] = o.m_Name; +} + +void from_json(const nlohmann::json& j, Category& o) +{ + if(j.find("id") != j.end()) + { + j.at("id").get_to(o.m_Id); + o.m_IdIsSet = true; + } + if(j.find("name") != j.end()) + { + j.at("name").get_to(o.m_Name); + o.m_NameIsSet = true; + } +} + +int64_t Category::getId() const +{ + return m_Id; +} +void Category::setId(int64_t const value) +{ + m_Id = value; + m_IdIsSet = true; +} +bool Category::idIsSet() const +{ + return m_IdIsSet; +} +void Category::unsetId() +{ + m_IdIsSet = false; +} +std::string Category::getName() const +{ + return m_Name; +} +void Category::setName(std::string const& value) +{ + m_Name = value; + m_NameIsSet = true; +} +bool Category::nameIsSet() const +{ + return m_NameIsSet; +} +void Category::unsetName() +{ + m_NameIsSet = false; +} + +} +} +} +} + diff --git a/samples/server/petstore/cpp-oatpp/model/Category.h b/samples/server/petstore/cpp-oatpp/model/Category.h new file mode 100644 index 00000000000..fa12fdc0de8 --- /dev/null +++ b/samples/server/petstore/cpp-oatpp/model/Category.h @@ -0,0 +1,73 @@ +/** +* OpenAPI Petstore +* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters. +* +* The version of the OpenAPI document: 1.0.0 +* +* +* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech). +* https://openapi-generator.tech +* Do not edit the class manually. +*/ +/* + * Category.h + * + * A category for a pet + */ + +#ifndef Category_H_ +#define Category_H_ + + +#include +#include + +namespace org { +namespace openapitools { +namespace server { +namespace model { + +/// +/// A category for a pet +/// +class Category +{ +public: + Category(); + virtual ~Category(); + + void validate(); + + ///////////////////////////////////////////// + /// Category members + + /// + /// + /// + int64_t getId() const; + void setId(int64_t const value); + bool idIsSet() const; + void unsetId(); + /// + /// + /// + std::string getName() const; + void setName(std::string const& value); + bool nameIsSet() const; + void unsetName(); + + friend void to_json(nlohmann::json& j, const Category& o); + friend void from_json(const nlohmann::json& j, Category& o); +protected: + int64_t m_Id; + bool m_IdIsSet; + std::string m_Name; + bool m_NameIsSet; +}; + +} +} +} +} + +#endif /* Category_H_ */ diff --git a/samples/server/petstore/cpp-oatpp/model/Helpers.cpp b/samples/server/petstore/cpp-oatpp/model/Helpers.cpp new file mode 100644 index 00000000000..a7774194c82 --- /dev/null +++ b/samples/server/petstore/cpp-oatpp/model/Helpers.cpp @@ -0,0 +1,98 @@ +/** +* OpenAPI Petstore +* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters. +* +* The version of the OpenAPI document: 1.0.0 +* +* +* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech). +* https://openapi-generator.tech +* Do not edit the class manually. +*/ +#include "Helpers.h" + +namespace org { +namespace openapitools { +namespace server { +namespace helpers { + + +std::string toStringValue(const std::string &value){ + return std::string(value); +} + +std::string toStringValue(const int32_t &value){ + return std::to_string(value); +} + +std::string toStringValue(const int64_t &value){ + return std::to_string(value); +} + +std::string toStringValue(const bool &value){ + return value?std::string("true"):std::string("false"); +} + +std::string toStringValue(const float &value){ + return std::to_string(value); +} + +std::string toStringValue(const double &value){ + return std::to_string(value); +} + +bool fromStringValue(const std::string &inStr, std::string &value){ + value = std::string(inStr); + return true; +} + +bool fromStringValue(const std::string &inStr, int32_t &value){ + try { + value = std::stoi( inStr ); + } + catch (const std::invalid_argument&) { + return false; + } + return true; +} + +bool fromStringValue(const std::string &inStr, int64_t &value){ + try { + value = std::stol( inStr ); + } + catch (const std::invalid_argument&) { + return false; + } + return true; +} + +bool fromStringValue(const std::string &inStr, bool &value){ + bool result = true; + inStr == "true"?value = true: inStr == "false"?value = false: result = false; + return result; +} + +bool fromStringValue(const std::string &inStr, float &value){ + try { + value = std::stof( inStr ); + } + catch (const std::invalid_argument&) { + return false; + } + return true; +} + +bool fromStringValue(const std::string &inStr, double &value){ + try { + value = std::stod( inStr ); + } + catch (const std::invalid_argument&) { + return false; + } + return true; +} + +} +} +} +} diff --git a/samples/server/petstore/cpp-oatpp/model/Helpers.h b/samples/server/petstore/cpp-oatpp/model/Helpers.h new file mode 100644 index 00000000000..9c8c02c4ae6 --- /dev/null +++ b/samples/server/petstore/cpp-oatpp/model/Helpers.h @@ -0,0 +1,76 @@ +/** +* OpenAPI Petstore +* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters. +* +* The version of the OpenAPI document: 1.0.0 +* +* +* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech). +* https://openapi-generator.tech +* Do not edit the class manually. +*/ +/* + * Helpers.h + * + * This is the helper class for models and primitives + */ + +#ifndef Helpers_H_ +#define Helpers_H_ + +#include +#include +#include +#include +#include + +namespace org { +namespace openapitools { +namespace server { +namespace helpers { + + std::string toStringValue(const std::string &value); + std::string toStringValue(const int32_t &value); + std::string toStringValue(const int64_t &value); + std::string toStringValue(const bool &value); + std::string toStringValue(const float &value); + std::string toStringValue(const double &value); + + bool fromStringValue(const std::string &inStr, std::string &value); + bool fromStringValue(const std::string &inStr, int32_t &value); + bool fromStringValue(const std::string &inStr, int64_t &value); + bool fromStringValue(const std::string &inStr, bool &value); + bool fromStringValue(const std::string &inStr, float &value); + bool fromStringValue(const std::string &inStr, double &value); + template + bool fromStringValue(const std::vector &inStr, std::vector &value){ + try{ + for(auto & item : inStr){ + T itemValue; + if(fromStringValue(item, itemValue)){ + value.push_back(itemValue); + } + } + } + catch(...){ + return false; + } + return value.size() > 0; + } + template + bool fromStringValue(const std::string &inStr, std::vector &value, char separator = ','){ + std::vector inStrings; + std::istringstream f(inStr); + std::string s; + while (std::getline(f, s, separator)) { + inStrings.push_back(s); + } + return fromStringValue(inStrings, value); + } + +} +} +} +} + +#endif // Helpers_H_ \ No newline at end of file diff --git a/samples/server/petstore/cpp-oatpp/model/Inline_object.cpp b/samples/server/petstore/cpp-oatpp/model/Inline_object.cpp new file mode 100644 index 00000000000..89638a84566 --- /dev/null +++ b/samples/server/petstore/cpp-oatpp/model/Inline_object.cpp @@ -0,0 +1,101 @@ +/** +* OpenAPI Petstore +* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters. +* +* The version of the OpenAPI document: 1.0.0 +* +* +* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech). +* https://openapi-generator.tech +* Do not edit the class manually. +*/ + + +#include "Inline_object.h" + +namespace org { +namespace openapitools { +namespace server { +namespace model { + +Inline_object::Inline_object() +{ + m_Name = ""; + m_NameIsSet = false; + m_Status = ""; + m_StatusIsSet = false; + +} + +Inline_object::~Inline_object() +{ +} + +void Inline_object::validate() +{ + // TODO: implement validation +} + +void to_json(nlohmann::json& j, const Inline_object& o) +{ + j = nlohmann::json(); + if(o.nameIsSet()) + j["name"] = o.m_Name; + if(o.statusIsSet()) + j["status"] = o.m_Status; +} + +void from_json(const nlohmann::json& j, Inline_object& o) +{ + if(j.find("name") != j.end()) + { + j.at("name").get_to(o.m_Name); + o.m_NameIsSet = true; + } + if(j.find("status") != j.end()) + { + j.at("status").get_to(o.m_Status); + o.m_StatusIsSet = true; + } +} + +std::string Inline_object::getName() const +{ + return m_Name; +} +void Inline_object::setName(std::string const& value) +{ + m_Name = value; + m_NameIsSet = true; +} +bool Inline_object::nameIsSet() const +{ + return m_NameIsSet; +} +void Inline_object::unsetName() +{ + m_NameIsSet = false; +} +std::string Inline_object::getStatus() const +{ + return m_Status; +} +void Inline_object::setStatus(std::string const& value) +{ + m_Status = value; + m_StatusIsSet = true; +} +bool Inline_object::statusIsSet() const +{ + return m_StatusIsSet; +} +void Inline_object::unsetStatus() +{ + m_StatusIsSet = false; +} + +} +} +} +} + diff --git a/samples/server/petstore/cpp-oatpp/model/Inline_object.h b/samples/server/petstore/cpp-oatpp/model/Inline_object.h new file mode 100644 index 00000000000..f407884cd88 --- /dev/null +++ b/samples/server/petstore/cpp-oatpp/model/Inline_object.h @@ -0,0 +1,73 @@ +/** +* OpenAPI Petstore +* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters. +* +* The version of the OpenAPI document: 1.0.0 +* +* +* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech). +* https://openapi-generator.tech +* Do not edit the class manually. +*/ +/* + * Inline_object.h + * + * + */ + +#ifndef Inline_object_H_ +#define Inline_object_H_ + + +#include +#include + +namespace org { +namespace openapitools { +namespace server { +namespace model { + +/// +/// +/// +class Inline_object +{ +public: + Inline_object(); + virtual ~Inline_object(); + + void validate(); + + ///////////////////////////////////////////// + /// Inline_object members + + /// + /// Updated name of the pet + /// + std::string getName() const; + void setName(std::string const& value); + bool nameIsSet() const; + void unsetName(); + /// + /// Updated status of the pet + /// + std::string getStatus() const; + void setStatus(std::string const& value); + bool statusIsSet() const; + void unsetStatus(); + + friend void to_json(nlohmann::json& j, const Inline_object& o); + friend void from_json(const nlohmann::json& j, Inline_object& o); +protected: + std::string m_Name; + bool m_NameIsSet; + std::string m_Status; + bool m_StatusIsSet; +}; + +} +} +} +} + +#endif /* Inline_object_H_ */ diff --git a/samples/server/petstore/cpp-oatpp/model/Inline_object_1.cpp b/samples/server/petstore/cpp-oatpp/model/Inline_object_1.cpp new file mode 100644 index 00000000000..0da3bdba2b8 --- /dev/null +++ b/samples/server/petstore/cpp-oatpp/model/Inline_object_1.cpp @@ -0,0 +1,100 @@ +/** +* OpenAPI Petstore +* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters. +* +* The version of the OpenAPI document: 1.0.0 +* +* +* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech). +* https://openapi-generator.tech +* Do not edit the class manually. +*/ + + +#include "Inline_object_1.h" + +namespace org { +namespace openapitools { +namespace server { +namespace model { + +Inline_object_1::Inline_object_1() +{ + m_AdditionalMetadata = ""; + m_AdditionalMetadataIsSet = false; + m_fileIsSet = false; + +} + +Inline_object_1::~Inline_object_1() +{ +} + +void Inline_object_1::validate() +{ + // TODO: implement validation +} + +void to_json(nlohmann::json& j, const Inline_object_1& o) +{ + j = nlohmann::json(); + if(o.additionalMetadataIsSet()) + j["additionalMetadata"] = o.m_AdditionalMetadata; + if(o.fileIsSet()) + j["file"] = o.m_file; +} + +void from_json(const nlohmann::json& j, Inline_object_1& o) +{ + if(j.find("additionalMetadata") != j.end()) + { + j.at("additionalMetadata").get_to(o.m_AdditionalMetadata); + o.m_AdditionalMetadataIsSet = true; + } + if(j.find("file") != j.end()) + { + j.at("file").get_to(o.m_file); + o.m_fileIsSet = true; + } +} + +std::string Inline_object_1::getAdditionalMetadata() const +{ + return m_AdditionalMetadata; +} +void Inline_object_1::setAdditionalMetadata(std::string const& value) +{ + m_AdditionalMetadata = value; + m_AdditionalMetadataIsSet = true; +} +bool Inline_object_1::additionalMetadataIsSet() const +{ + return m_AdditionalMetadataIsSet; +} +void Inline_object_1::unsetAdditionalMetadata() +{ + m_AdditionalMetadataIsSet = false; +} +std::string Inline_object_1::getFile() const +{ + return m_file; +} +void Inline_object_1::setFile(std::string const& value) +{ + m_file = value; + m_fileIsSet = true; +} +bool Inline_object_1::fileIsSet() const +{ + return m_fileIsSet; +} +void Inline_object_1::unsetfile() +{ + m_fileIsSet = false; +} + +} +} +} +} + diff --git a/samples/server/petstore/cpp-oatpp/model/Inline_object_1.h b/samples/server/petstore/cpp-oatpp/model/Inline_object_1.h new file mode 100644 index 00000000000..a5d90b1754e --- /dev/null +++ b/samples/server/petstore/cpp-oatpp/model/Inline_object_1.h @@ -0,0 +1,73 @@ +/** +* OpenAPI Petstore +* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters. +* +* The version of the OpenAPI document: 1.0.0 +* +* +* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech). +* https://openapi-generator.tech +* Do not edit the class manually. +*/ +/* + * Inline_object_1.h + * + * + */ + +#ifndef Inline_object_1_H_ +#define Inline_object_1_H_ + + +#include +#include + +namespace org { +namespace openapitools { +namespace server { +namespace model { + +/// +/// +/// +class Inline_object_1 +{ +public: + Inline_object_1(); + virtual ~Inline_object_1(); + + void validate(); + + ///////////////////////////////////////////// + /// Inline_object_1 members + + /// + /// Additional data to pass to server + /// + std::string getAdditionalMetadata() const; + void setAdditionalMetadata(std::string const& value); + bool additionalMetadataIsSet() const; + void unsetAdditionalMetadata(); + /// + /// file to upload + /// + std::string getFile() const; + void setFile(std::string const& value); + bool fileIsSet() const; + void unsetfile(); + + friend void to_json(nlohmann::json& j, const Inline_object_1& o); + friend void from_json(const nlohmann::json& j, Inline_object_1& o); +protected: + std::string m_AdditionalMetadata; + bool m_AdditionalMetadataIsSet; + std::string m_file; + bool m_fileIsSet; +}; + +} +} +} +} + +#endif /* Inline_object_1_H_ */ diff --git a/samples/server/petstore/cpp-oatpp/model/Order.cpp b/samples/server/petstore/cpp-oatpp/model/Order.cpp new file mode 100644 index 00000000000..82efbce6cb2 --- /dev/null +++ b/samples/server/petstore/cpp-oatpp/model/Order.cpp @@ -0,0 +1,205 @@ +/** +* OpenAPI Petstore +* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters. +* +* The version of the OpenAPI document: 1.0.0 +* +* +* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech). +* https://openapi-generator.tech +* Do not edit the class manually. +*/ + + +#include "Order.h" + +namespace org { +namespace openapitools { +namespace server { +namespace model { + +Order::Order() +{ + m_Id = 0L; + m_IdIsSet = false; + m_PetId = 0L; + m_PetIdIsSet = false; + m_Quantity = 0; + m_QuantityIsSet = false; + m_ShipDate = ""; + m_ShipDateIsSet = false; + m_Status = ""; + m_StatusIsSet = false; + m_Complete = false; + m_CompleteIsSet = false; + +} + +Order::~Order() +{ +} + +void Order::validate() +{ + // TODO: implement validation +} + +void to_json(nlohmann::json& j, const Order& o) +{ + j = nlohmann::json(); + if(o.idIsSet()) + j["id"] = o.m_Id; + if(o.petIdIsSet()) + j["petId"] = o.m_PetId; + if(o.quantityIsSet()) + j["quantity"] = o.m_Quantity; + if(o.shipDateIsSet()) + j["shipDate"] = o.m_ShipDate; + if(o.statusIsSet()) + j["status"] = o.m_Status; + if(o.completeIsSet()) + j["complete"] = o.m_Complete; +} + +void from_json(const nlohmann::json& j, Order& o) +{ + if(j.find("id") != j.end()) + { + j.at("id").get_to(o.m_Id); + o.m_IdIsSet = true; + } + if(j.find("petId") != j.end()) + { + j.at("petId").get_to(o.m_PetId); + o.m_PetIdIsSet = true; + } + if(j.find("quantity") != j.end()) + { + j.at("quantity").get_to(o.m_Quantity); + o.m_QuantityIsSet = true; + } + if(j.find("shipDate") != j.end()) + { + j.at("shipDate").get_to(o.m_ShipDate); + o.m_ShipDateIsSet = true; + } + if(j.find("status") != j.end()) + { + j.at("status").get_to(o.m_Status); + o.m_StatusIsSet = true; + } + if(j.find("complete") != j.end()) + { + j.at("complete").get_to(o.m_Complete); + o.m_CompleteIsSet = true; + } +} + +int64_t Order::getId() const +{ + return m_Id; +} +void Order::setId(int64_t const value) +{ + m_Id = value; + m_IdIsSet = true; +} +bool Order::idIsSet() const +{ + return m_IdIsSet; +} +void Order::unsetId() +{ + m_IdIsSet = false; +} +int64_t Order::getPetId() const +{ + return m_PetId; +} +void Order::setPetId(int64_t const value) +{ + m_PetId = value; + m_PetIdIsSet = true; +} +bool Order::petIdIsSet() const +{ + return m_PetIdIsSet; +} +void Order::unsetPetId() +{ + m_PetIdIsSet = false; +} +int32_t Order::getQuantity() const +{ + return m_Quantity; +} +void Order::setQuantity(int32_t const value) +{ + m_Quantity = value; + m_QuantityIsSet = true; +} +bool Order::quantityIsSet() const +{ + return m_QuantityIsSet; +} +void Order::unsetQuantity() +{ + m_QuantityIsSet = false; +} +std::string Order::getShipDate() const +{ + return m_ShipDate; +} +void Order::setShipDate(std::string const& value) +{ + m_ShipDate = value; + m_ShipDateIsSet = true; +} +bool Order::shipDateIsSet() const +{ + return m_ShipDateIsSet; +} +void Order::unsetShipDate() +{ + m_ShipDateIsSet = false; +} +std::string Order::getStatus() const +{ + return m_Status; +} +void Order::setStatus(std::string const& value) +{ + m_Status = value; + m_StatusIsSet = true; +} +bool Order::statusIsSet() const +{ + return m_StatusIsSet; +} +void Order::unsetStatus() +{ + m_StatusIsSet = false; +} +bool Order::isComplete() const +{ + return m_Complete; +} +void Order::setComplete(bool const value) +{ + m_Complete = value; + m_CompleteIsSet = true; +} +bool Order::completeIsSet() const +{ + return m_CompleteIsSet; +} +void Order::unsetComplete() +{ + m_CompleteIsSet = false; +} + +} +} +} +} + diff --git a/samples/server/petstore/cpp-oatpp/model/Order.h b/samples/server/petstore/cpp-oatpp/model/Order.h new file mode 100644 index 00000000000..d900b633f32 --- /dev/null +++ b/samples/server/petstore/cpp-oatpp/model/Order.h @@ -0,0 +1,109 @@ +/** +* OpenAPI Petstore +* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters. +* +* The version of the OpenAPI document: 1.0.0 +* +* +* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech). +* https://openapi-generator.tech +* Do not edit the class manually. +*/ +/* + * Order.h + * + * An order for a pets from the pet store + */ + +#ifndef Order_H_ +#define Order_H_ + + +#include +#include + +namespace org { +namespace openapitools { +namespace server { +namespace model { + +/// +/// An order for a pets from the pet store +/// +class Order +{ +public: + Order(); + virtual ~Order(); + + void validate(); + + ///////////////////////////////////////////// + /// Order members + + /// + /// + /// + int64_t getId() const; + void setId(int64_t const value); + bool idIsSet() const; + void unsetId(); + /// + /// + /// + int64_t getPetId() const; + void setPetId(int64_t const value); + bool petIdIsSet() const; + void unsetPetId(); + /// + /// + /// + int32_t getQuantity() const; + void setQuantity(int32_t const value); + bool quantityIsSet() const; + void unsetQuantity(); + /// + /// + /// + std::string getShipDate() const; + void setShipDate(std::string const& value); + bool shipDateIsSet() const; + void unsetShipDate(); + /// + /// Order Status + /// + std::string getStatus() const; + void setStatus(std::string const& value); + bool statusIsSet() const; + void unsetStatus(); + /// + /// + /// + bool isComplete() const; + void setComplete(bool const value); + bool completeIsSet() const; + void unsetComplete(); + + friend void to_json(nlohmann::json& j, const Order& o); + friend void from_json(const nlohmann::json& j, Order& o); +protected: + int64_t m_Id; + bool m_IdIsSet; + int64_t m_PetId; + bool m_PetIdIsSet; + int32_t m_Quantity; + bool m_QuantityIsSet; + std::string m_ShipDate; + bool m_ShipDateIsSet; + std::string m_Status; + bool m_StatusIsSet; + bool m_Complete; + bool m_CompleteIsSet; +}; + +} +} +} +} + +#endif /* Order_H_ */ diff --git a/samples/server/petstore/cpp-oatpp/model/Pet.cpp b/samples/server/petstore/cpp-oatpp/model/Pet.cpp new file mode 100644 index 00000000000..0ff016b545f --- /dev/null +++ b/samples/server/petstore/cpp-oatpp/model/Pet.cpp @@ -0,0 +1,172 @@ +/** +* OpenAPI Petstore +* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters. +* +* The version of the OpenAPI document: 1.0.0 +* +* +* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech). +* https://openapi-generator.tech +* Do not edit the class manually. +*/ + + +#include "Pet.h" + +namespace org { +namespace openapitools { +namespace server { +namespace model { + +Pet::Pet() +{ + m_Id = 0L; + m_IdIsSet = false; + m_CategoryIsSet = false; + m_Name = ""; + m_TagsIsSet = false; + m_Status = ""; + m_StatusIsSet = false; + +} + +Pet::~Pet() +{ +} + +void Pet::validate() +{ + // TODO: implement validation +} + +void to_json(nlohmann::json& j, const Pet& o) +{ + j = nlohmann::json(); + if(o.idIsSet()) + j["id"] = o.m_Id; + if(o.categoryIsSet()) + j["category"] = o.m_Category; + j["name"] = o.m_Name; + j["photoUrls"] = o.m_PhotoUrls; + if(o.tagsIsSet() || !o.m_Tags.empty()) + j["tags"] = o.m_Tags; + if(o.statusIsSet()) + j["status"] = o.m_Status; +} + +void from_json(const nlohmann::json& j, Pet& o) +{ + if(j.find("id") != j.end()) + { + j.at("id").get_to(o.m_Id); + o.m_IdIsSet = true; + } + if(j.find("category") != j.end()) + { + j.at("category").get_to(o.m_Category); + o.m_CategoryIsSet = true; + } + j.at("name").get_to(o.m_Name); + j.at("photoUrls").get_to(o.m_PhotoUrls); + if(j.find("tags") != j.end()) + { + j.at("tags").get_to(o.m_Tags); + o.m_TagsIsSet = true; + } + if(j.find("status") != j.end()) + { + j.at("status").get_to(o.m_Status); + o.m_StatusIsSet = true; + } +} + +int64_t Pet::getId() const +{ + return m_Id; +} +void Pet::setId(int64_t const value) +{ + m_Id = value; + m_IdIsSet = true; +} +bool Pet::idIsSet() const +{ + return m_IdIsSet; +} +void Pet::unsetId() +{ + m_IdIsSet = false; +} +Category Pet::getCategory() const +{ + return m_Category; +} +void Pet::setCategory(Category const& value) +{ + m_Category = value; + m_CategoryIsSet = true; +} +bool Pet::categoryIsSet() const +{ + return m_CategoryIsSet; +} +void Pet::unsetCategory() +{ + m_CategoryIsSet = false; +} +std::string Pet::getName() const +{ + return m_Name; +} +void Pet::setName(std::string const& value) +{ + m_Name = value; +} +std::vector& Pet::getPhotoUrls() +{ + return m_PhotoUrls; +} +void Pet::setPhotoUrls(std::vector const& value) +{ + m_PhotoUrls = value; +} +std::vector& Pet::getTags() +{ + return m_Tags; +} +void Pet::setTags(std::vector const& value) +{ + m_Tags = value; + m_TagsIsSet = true; +} +bool Pet::tagsIsSet() const +{ + return m_TagsIsSet; +} +void Pet::unsetTags() +{ + m_TagsIsSet = false; +} +std::string Pet::getStatus() const +{ + return m_Status; +} +void Pet::setStatus(std::string const& value) +{ + m_Status = value; + m_StatusIsSet = true; +} +bool Pet::statusIsSet() const +{ + return m_StatusIsSet; +} +void Pet::unsetStatus() +{ + m_StatusIsSet = false; +} + +} +} +} +} + diff --git a/samples/server/petstore/cpp-oatpp/model/Pet.h b/samples/server/petstore/cpp-oatpp/model/Pet.h new file mode 100644 index 00000000000..eddf475f6ee --- /dev/null +++ b/samples/server/petstore/cpp-oatpp/model/Pet.h @@ -0,0 +1,108 @@ +/** +* OpenAPI Petstore +* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters. +* +* The version of the OpenAPI document: 1.0.0 +* +* +* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech). +* https://openapi-generator.tech +* Do not edit the class manually. +*/ +/* + * Pet.h + * + * A pet for sale in the pet store + */ + +#ifndef Pet_H_ +#define Pet_H_ + + +#include "Tag.h" +#include +#include "Category.h" +#include +#include + +namespace org { +namespace openapitools { +namespace server { +namespace model { + +/// +/// A pet for sale in the pet store +/// +class Pet +{ +public: + Pet(); + virtual ~Pet(); + + void validate(); + + ///////////////////////////////////////////// + /// Pet members + + /// + /// + /// + int64_t getId() const; + void setId(int64_t const value); + bool idIsSet() const; + void unsetId(); + /// + /// + /// + Category getCategory() const; + void setCategory(Category const& value); + bool categoryIsSet() const; + void unsetCategory(); + /// + /// + /// + std::string getName() const; + void setName(std::string const& value); + /// + /// + /// + std::vector& getPhotoUrls(); + void setPhotoUrls(std::vector const& value); + /// + /// + /// + std::vector& getTags(); + void setTags(std::vector const& value); + bool tagsIsSet() const; + void unsetTags(); + /// + /// pet status in the store + /// + std::string getStatus() const; + void setStatus(std::string const& value); + bool statusIsSet() const; + void unsetStatus(); + + friend void to_json(nlohmann::json& j, const Pet& o); + friend void from_json(const nlohmann::json& j, Pet& o); +protected: + int64_t m_Id; + bool m_IdIsSet; + Category m_Category; + bool m_CategoryIsSet; + std::string m_Name; + + std::vector m_PhotoUrls; + + std::vector m_Tags; + bool m_TagsIsSet; + std::string m_Status; + bool m_StatusIsSet; +}; + +} +} +} +} + +#endif /* Pet_H_ */ diff --git a/samples/server/petstore/cpp-oatpp/model/Tag.cpp b/samples/server/petstore/cpp-oatpp/model/Tag.cpp new file mode 100644 index 00000000000..7f56fb2bcca --- /dev/null +++ b/samples/server/petstore/cpp-oatpp/model/Tag.cpp @@ -0,0 +1,101 @@ +/** +* OpenAPI Petstore +* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters. +* +* The version of the OpenAPI document: 1.0.0 +* +* +* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech). +* https://openapi-generator.tech +* Do not edit the class manually. +*/ + + +#include "Tag.h" + +namespace org { +namespace openapitools { +namespace server { +namespace model { + +Tag::Tag() +{ + m_Id = 0L; + m_IdIsSet = false; + m_Name = ""; + m_NameIsSet = false; + +} + +Tag::~Tag() +{ +} + +void Tag::validate() +{ + // TODO: implement validation +} + +void to_json(nlohmann::json& j, const Tag& o) +{ + j = nlohmann::json(); + if(o.idIsSet()) + j["id"] = o.m_Id; + if(o.nameIsSet()) + j["name"] = o.m_Name; +} + +void from_json(const nlohmann::json& j, Tag& o) +{ + if(j.find("id") != j.end()) + { + j.at("id").get_to(o.m_Id); + o.m_IdIsSet = true; + } + if(j.find("name") != j.end()) + { + j.at("name").get_to(o.m_Name); + o.m_NameIsSet = true; + } +} + +int64_t Tag::getId() const +{ + return m_Id; +} +void Tag::setId(int64_t const value) +{ + m_Id = value; + m_IdIsSet = true; +} +bool Tag::idIsSet() const +{ + return m_IdIsSet; +} +void Tag::unsetId() +{ + m_IdIsSet = false; +} +std::string Tag::getName() const +{ + return m_Name; +} +void Tag::setName(std::string const& value) +{ + m_Name = value; + m_NameIsSet = true; +} +bool Tag::nameIsSet() const +{ + return m_NameIsSet; +} +void Tag::unsetName() +{ + m_NameIsSet = false; +} + +} +} +} +} + diff --git a/samples/server/petstore/cpp-oatpp/model/Tag.h b/samples/server/petstore/cpp-oatpp/model/Tag.h new file mode 100644 index 00000000000..4b422931d77 --- /dev/null +++ b/samples/server/petstore/cpp-oatpp/model/Tag.h @@ -0,0 +1,73 @@ +/** +* OpenAPI Petstore +* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters. +* +* The version of the OpenAPI document: 1.0.0 +* +* +* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech). +* https://openapi-generator.tech +* Do not edit the class manually. +*/ +/* + * Tag.h + * + * A tag for a pet + */ + +#ifndef Tag_H_ +#define Tag_H_ + + +#include +#include + +namespace org { +namespace openapitools { +namespace server { +namespace model { + +/// +/// A tag for a pet +/// +class Tag +{ +public: + Tag(); + virtual ~Tag(); + + void validate(); + + ///////////////////////////////////////////// + /// Tag members + + /// + /// + /// + int64_t getId() const; + void setId(int64_t const value); + bool idIsSet() const; + void unsetId(); + /// + /// + /// + std::string getName() const; + void setName(std::string const& value); + bool nameIsSet() const; + void unsetName(); + + friend void to_json(nlohmann::json& j, const Tag& o); + friend void from_json(const nlohmann::json& j, Tag& o); +protected: + int64_t m_Id; + bool m_IdIsSet; + std::string m_Name; + bool m_NameIsSet; +}; + +} +} +} +} + +#endif /* Tag_H_ */ diff --git a/samples/server/petstore/cpp-oatpp/model/User.cpp b/samples/server/petstore/cpp-oatpp/model/User.cpp new file mode 100644 index 00000000000..20ae06352f2 --- /dev/null +++ b/samples/server/petstore/cpp-oatpp/model/User.cpp @@ -0,0 +1,257 @@ +/** +* OpenAPI Petstore +* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters. +* +* The version of the OpenAPI document: 1.0.0 +* +* +* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech). +* https://openapi-generator.tech +* Do not edit the class manually. +*/ + + +#include "User.h" + +namespace org { +namespace openapitools { +namespace server { +namespace model { + +User::User() +{ + m_Id = 0L; + m_IdIsSet = false; + m_Username = ""; + m_UsernameIsSet = false; + m_FirstName = ""; + m_FirstNameIsSet = false; + m_LastName = ""; + m_LastNameIsSet = false; + m_Email = ""; + m_EmailIsSet = false; + m_Password = ""; + m_PasswordIsSet = false; + m_Phone = ""; + m_PhoneIsSet = false; + m_UserStatus = 0; + m_UserStatusIsSet = false; + +} + +User::~User() +{ +} + +void User::validate() +{ + // TODO: implement validation +} + +void to_json(nlohmann::json& j, const User& o) +{ + j = nlohmann::json(); + if(o.idIsSet()) + j["id"] = o.m_Id; + if(o.usernameIsSet()) + j["username"] = o.m_Username; + if(o.firstNameIsSet()) + j["firstName"] = o.m_FirstName; + if(o.lastNameIsSet()) + j["lastName"] = o.m_LastName; + if(o.emailIsSet()) + j["email"] = o.m_Email; + if(o.passwordIsSet()) + j["password"] = o.m_Password; + if(o.phoneIsSet()) + j["phone"] = o.m_Phone; + if(o.userStatusIsSet()) + j["userStatus"] = o.m_UserStatus; +} + +void from_json(const nlohmann::json& j, User& o) +{ + if(j.find("id") != j.end()) + { + j.at("id").get_to(o.m_Id); + o.m_IdIsSet = true; + } + if(j.find("username") != j.end()) + { + j.at("username").get_to(o.m_Username); + o.m_UsernameIsSet = true; + } + if(j.find("firstName") != j.end()) + { + j.at("firstName").get_to(o.m_FirstName); + o.m_FirstNameIsSet = true; + } + if(j.find("lastName") != j.end()) + { + j.at("lastName").get_to(o.m_LastName); + o.m_LastNameIsSet = true; + } + if(j.find("email") != j.end()) + { + j.at("email").get_to(o.m_Email); + o.m_EmailIsSet = true; + } + if(j.find("password") != j.end()) + { + j.at("password").get_to(o.m_Password); + o.m_PasswordIsSet = true; + } + if(j.find("phone") != j.end()) + { + j.at("phone").get_to(o.m_Phone); + o.m_PhoneIsSet = true; + } + if(j.find("userStatus") != j.end()) + { + j.at("userStatus").get_to(o.m_UserStatus); + o.m_UserStatusIsSet = true; + } +} + +int64_t User::getId() const +{ + return m_Id; +} +void User::setId(int64_t const value) +{ + m_Id = value; + m_IdIsSet = true; +} +bool User::idIsSet() const +{ + return m_IdIsSet; +} +void User::unsetId() +{ + m_IdIsSet = false; +} +std::string User::getUsername() const +{ + return m_Username; +} +void User::setUsername(std::string const& value) +{ + m_Username = value; + m_UsernameIsSet = true; +} +bool User::usernameIsSet() const +{ + return m_UsernameIsSet; +} +void User::unsetUsername() +{ + m_UsernameIsSet = false; +} +std::string User::getFirstName() const +{ + return m_FirstName; +} +void User::setFirstName(std::string const& value) +{ + m_FirstName = value; + m_FirstNameIsSet = true; +} +bool User::firstNameIsSet() const +{ + return m_FirstNameIsSet; +} +void User::unsetFirstName() +{ + m_FirstNameIsSet = false; +} +std::string User::getLastName() const +{ + return m_LastName; +} +void User::setLastName(std::string const& value) +{ + m_LastName = value; + m_LastNameIsSet = true; +} +bool User::lastNameIsSet() const +{ + return m_LastNameIsSet; +} +void User::unsetLastName() +{ + m_LastNameIsSet = false; +} +std::string User::getEmail() const +{ + return m_Email; +} +void User::setEmail(std::string const& value) +{ + m_Email = value; + m_EmailIsSet = true; +} +bool User::emailIsSet() const +{ + return m_EmailIsSet; +} +void User::unsetEmail() +{ + m_EmailIsSet = false; +} +std::string User::getPassword() const +{ + return m_Password; +} +void User::setPassword(std::string const& value) +{ + m_Password = value; + m_PasswordIsSet = true; +} +bool User::passwordIsSet() const +{ + return m_PasswordIsSet; +} +void User::unsetPassword() +{ + m_PasswordIsSet = false; +} +std::string User::getPhone() const +{ + return m_Phone; +} +void User::setPhone(std::string const& value) +{ + m_Phone = value; + m_PhoneIsSet = true; +} +bool User::phoneIsSet() const +{ + return m_PhoneIsSet; +} +void User::unsetPhone() +{ + m_PhoneIsSet = false; +} +int32_t User::getUserStatus() const +{ + return m_UserStatus; +} +void User::setUserStatus(int32_t const value) +{ + m_UserStatus = value; + m_UserStatusIsSet = true; +} +bool User::userStatusIsSet() const +{ + return m_UserStatusIsSet; +} +void User::unsetUserStatus() +{ + m_UserStatusIsSet = false; +} + +} +} +} +} + diff --git a/samples/server/petstore/cpp-oatpp/model/User.h b/samples/server/petstore/cpp-oatpp/model/User.h new file mode 100644 index 00000000000..1f4c17ce6a8 --- /dev/null +++ b/samples/server/petstore/cpp-oatpp/model/User.h @@ -0,0 +1,127 @@ +/** +* OpenAPI Petstore +* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters. +* +* The version of the OpenAPI document: 1.0.0 +* +* +* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech). +* https://openapi-generator.tech +* Do not edit the class manually. +*/ +/* + * User.h + * + * A User who is purchasing from the pet store + */ + +#ifndef User_H_ +#define User_H_ + + +#include +#include + +namespace org { +namespace openapitools { +namespace server { +namespace model { + +/// +/// A User who is purchasing from the pet store +/// +class User +{ +public: + User(); + virtual ~User(); + + void validate(); + + ///////////////////////////////////////////// + /// User members + + /// + /// + /// + int64_t getId() const; + void setId(int64_t const value); + bool idIsSet() const; + void unsetId(); + /// + /// + /// + std::string getUsername() const; + void setUsername(std::string const& value); + bool usernameIsSet() const; + void unsetUsername(); + /// + /// + /// + std::string getFirstName() const; + void setFirstName(std::string const& value); + bool firstNameIsSet() const; + void unsetFirstName(); + /// + /// + /// + std::string getLastName() const; + void setLastName(std::string const& value); + bool lastNameIsSet() const; + void unsetLastName(); + /// + /// + /// + std::string getEmail() const; + void setEmail(std::string const& value); + bool emailIsSet() const; + void unsetEmail(); + /// + /// + /// + std::string getPassword() const; + void setPassword(std::string const& value); + bool passwordIsSet() const; + void unsetPassword(); + /// + /// + /// + std::string getPhone() const; + void setPhone(std::string const& value); + bool phoneIsSet() const; + void unsetPhone(); + /// + /// User Status + /// + int32_t getUserStatus() const; + void setUserStatus(int32_t const value); + bool userStatusIsSet() const; + void unsetUserStatus(); + + friend void to_json(nlohmann::json& j, const User& o); + friend void from_json(const nlohmann::json& j, User& o); +protected: + int64_t m_Id; + bool m_IdIsSet; + std::string m_Username; + bool m_UsernameIsSet; + std::string m_FirstName; + bool m_FirstNameIsSet; + std::string m_LastName; + bool m_LastNameIsSet; + std::string m_Email; + bool m_EmailIsSet; + std::string m_Password; + bool m_PasswordIsSet; + std::string m_Phone; + bool m_PhoneIsSet; + int32_t m_UserStatus; + bool m_UserStatusIsSet; +}; + +} +} +} +} + +#endif /* User_H_ */