Skip to content

Commit 093b99b

Browse files
author
rva8kor
committed
Improved the following from branch3:
1. Handled void return types, if there is no response schema defined. 2. Handled multiple success response types (2xx status codes) by generating a variant type that includes all possible success response types. 3. Updated the api-source.mustache and api-header.mustache templates to accommodate these changes.
1 parent 1b62241 commit 093b99b

36 files changed

Lines changed: 955 additions & 276 deletions

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

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -351,8 +351,8 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<Mo
351351
op.vendorExtensions.put("pathParams", op.pathParams);
352352

353353
// Process responses
354-
355354
List<String> errorTypes = new ArrayList<>();
355+
List<String> successTypes = new ArrayList<>();
356356
List<Map<String, Object>> successCodeToTypes = new ArrayList<>();
357357
List<Map<String, Object>> errorCodeToTypes = new ArrayList<>();
358358
boolean isSuccessResponseType = false;
@@ -362,26 +362,23 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<Mo
362362
boolean hasAnyResponseSchema = false;
363363

364364
if (resp.code != null) {
365-
if (resp.code.equals("200") && resp.baseType != null) {
365+
// Collect all 2xx response types as successTypes
366+
if (resp.code.startsWith("2") && resp.baseType != null) {
366367
hasAnyResponseSchema = true;
367368
String successType;
368369
String successConstName;
369-
// boolean successTypeIsEnum = false;
370370
if (!typeMapping.containsKey(resp.baseType)) {
371-
// String className = stripPathFromClassName(resp.baseType).get("className");
372371
String className = toPascalCase(resp.baseType);
373372
successType = namespaceFiltered + "::" + className;
374373
modelsUsed.add(className);
375374
successConstName = HTTP_RESPONSE_PREFIX + StringUtils.underscore(className).toUpperCase(Locale.ROOT);
376375
} else {
377-
// Primitive type - use the mapped C++ type
378376
successType = typeMapping.get(resp.baseType);
379377
successConstName = HTTP_RESPONSE_PREFIX + "PRIMITIVE_"+StringUtils.underscore(resp.baseType).toUpperCase(Locale.ROOT);
380378
isSuccessResponsePrimitive = true;
381379
}
382-
if (successType != null && !successType.isEmpty()) {
383-
op.vendorExtensions.put("successType", successType);
384-
}
380+
successTypes.add(successType);
381+
op.vendorExtensions.put("successTypes", successTypes);
385382
if(successConstName != null && !successConstName.isEmpty() && successType!= null && !successType.isEmpty()) {
386383
String uniqueKey = successType + ":" + successConstName;
387384
final String finalSuccessConstName = successConstName;
@@ -396,7 +393,6 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<Mo
396393
Map<String, Object> successItem = new HashMap<>();
397394
successItem.put("successType", successType);
398395
successItem.put("successConstName", successConstName);
399-
// successItem.put("successTypeIsEnum", successTypeIsEnum);
400396
successCodeToTypes.add(successItem);
401397
}
402398
} else {
@@ -430,15 +426,12 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<Mo
430426
opStatusCodeConsts.add(item);
431427
}
432428
errorTypes.add(errorType);
429+
op.vendorExtensions.put("errorTypes", errorTypes);
433430
Map<String, Object> errorItem = new HashMap<>();
434431
errorItem.put("errorType", errorType);
435432
errorItem.put("errorConstName", errorConstName);
436433
// errorItem.put("errorTypeIsEnum", errorTypeIsEnum);
437434
errorCodeToTypes.add(errorItem);
438-
}
439-
if (errorTypes != null && !errorTypes.isEmpty()) {
440-
op.vendorExtensions.put("errorTypes", errorTypes);
441-
}
442435
}
443436
}
444437
if (hasAnyResponseSchema) {

modules/openapi-generator/src/main/resources/cpp-httplib-server/api-header.mustache

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,11 @@ public:
5252
/**
5353
* @brief Response type for {{handlerFunctionName}}.
5454
*/
55-
using {{responseType}} = std::variant<
56-
{{#successType}}{{successType}}{{/successType}}{{^errorTypes}}>;{{/errorTypes}}
57-
{{#errorTypes}} , {{{.}}}{{#-last}}>;{{/-last}}
55+
using {{responseType}} = std::variant<
56+
{{#successType}} {{successType}}{{#errorTypes}}{{#-first}},{{/-first}}{{/errorTypes}}{{^errorTypes}}{{^-last}},{{/-last}}{{#-last}}>;{{/-last}}{{/errorTypes}}
57+
{{/successType}}
58+
{{#errorTypes}}
59+
{{.}}{{#-last}}>;{{/-last}}{{^ -last}},{{/ -last}}
5860
{{/errorTypes}}
5961

6062
{{/hasAnyResponseSchema}}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,13 @@ void {{apiClassnameInPascalCase}}::registerRoutes(httplib::Server& svr) {
101101
{{#hasAnyRequestSchema}}
102102
auto params = parse{{operationIdPascalCase}}Params(req);
103103
{{/hasAnyRequestSchema}}
104-
auto result = {{handlerFunctionName}}({{#hasAnyRequestSchema}}params{{/hasAnyRequestSchema}});
105104
{{#hasAnyResponseSchema}}
105+
auto result = {{handlerFunctionName}}({{#hasAnyRequestSchema}}params{{/hasAnyRequestSchema}});
106106
handle{{responseType}}(result, res);
107107
{{/hasAnyResponseSchema}}
108+
{{^hasAnyResponseSchema}}
109+
{{handlerFunctionName}}({{#hasAnyRequestSchema}}params{{/hasAnyRequestSchema}});
110+
{{/hasAnyResponseSchema}}
108111
} {{#requestModel}}catch (const nlohmann::json::parse_error& e) {
109112
nlohmann::json errorJson = { {"message", "Invalid JSON: " + std::string(e.what())} };
110113
res.set_content(errorJson.dump(), "application/json");
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# OpenAPI Generator Ignore
2+
# Generated by openapi-generator https://github.com/openapitools/openapi-generator
3+
4+
# Use this file to prevent files from being overwritten by the generator.
5+
# The patterns follow closely to .gitignore or .dockerignore.
6+
7+
# As an example, the C# client generator defines ApiClient.cs.
8+
# You can make changes and tell OpenAPI Generator to ignore just this file by uncommenting the following line:
9+
#ApiClient.cs
10+
11+
# You can match any string of characters against a directory, file or extension with a single asterisk (*):
12+
#foo/*/qux
13+
# The above matches foo/bar/qux and foo/baz/qux, but not foo/bar/baz/qux
14+
15+
# You can recursively match patterns against a directory, file or extension with a double asterisk (**):
16+
#foo/**/qux
17+
# This matches foo/bar/qux, foo/baz/qux, and foo/bar/baz/qux
18+
19+
# You can also negate patterns with an exclamation (!).
20+
# For example, you can ignore all files in a docs folder with the file extension .md:
21+
#docs/*.md
22+
# Then explicitly reverse the ignore rule for a single file:
23+
#!docs/README.md
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
CMakeLists.txt
2+
LICENSE
3+
README.md
4+
api/PetApi.cpp
5+
api/PetApi.h
6+
api/StoreApi.cpp
7+
api/StoreApi.h
8+
api/UserApi.cpp
9+
api/UserApi.h
10+
models/ApiResponse.cpp
11+
models/ApiResponse.h
12+
models/Category.cpp
13+
models/Category.h
14+
models/Order.cpp
15+
models/Order.h
16+
models/Pet.cpp
17+
models/Pet.h
18+
models/PetError400.cpp
19+
models/PetError400.h
20+
models/PetError500.cpp
21+
models/PetError500.h
22+
models/PetList.cpp
23+
models/PetList.h
24+
models/PetListFilter.cpp
25+
models/PetListFilter.h
26+
models/PetListPagination.cpp
27+
models/PetListPagination.h
28+
models/Tag.cpp
29+
models/Tag.h
30+
models/User.cpp
31+
models/User.h
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
7.18.0-SNAPSHOT
Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
cmake_minimum_required(VERSION 3.10)
2-
project(sample LANGUAGES CXX)
2+
project(cpp_httplib_server LANGUAGES CXX)
33

4-
set(TARGET_NAME sample_openapi_lib)
4+
find_package(nlohmann_json REQUIRED)
5+
find_package(httplib REQUIRED)
6+
find_package(OpenSSL REQUIRED)
7+
find_package(ZLIB REQUIRED)
8+
9+
set(TARGET_NAME cpp_httplib_server_openapi_lib)
510
set(CMAKE_CXX_STANDARD 17)
611
set(CMAKE_CXX_STANDARD_REQUIRED ON)
12+
713
file(GLOB API_SRCS
814
${CMAKE_CURRENT_SOURCE_DIR}/api/*.h
915
${CMAKE_CURRENT_SOURCE_DIR}/api/*.cpp
@@ -15,5 +21,11 @@ file(GLOB MODEL_SRCS
1521
add_library(${TARGET_NAME} ${API_SRCS} ${MODEL_SRCS})
1622
target_include_directories (${TARGET_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
1723
# Make sure these libraries/headers are available in the build environment before linking
18-
# Required libraries/headers are httplib,ssl,nlohmann::json
19-
target_link_libraries(${TARGET_NAME} httplib ssl nlohmann_json::nlohmann_json)
24+
# Required libraries/headers are httplib,ssl,nlohmann::json,zlib
25+
target_link_libraries(${TARGET_NAME}
26+
PRIVATE
27+
httplib::httplib
28+
OpenSSL::SSL
29+
OpenSSL::Crypto
30+
nlohmann_json::nlohmann_json
31+
ZLIB::ZLIB)

0 commit comments

Comments
 (0)