From d7025b4ebe70d396e4353fb6541fd33d964e4495 Mon Sep 17 00:00:00 2001 From: Clemens Terasa Date: Wed, 12 Jul 2023 09:16:42 +0200 Subject: [PATCH] [C++][Pistache] Serialize integer enums if possible In OpenAPI it is possible to define an enum schema containing integers only. Similar to the following JSON snippet: ``` ... "components": { "schemas": { "size": { "type": "integer", "description": "Container size", "enum": [ 10000, 20000, 100000, 200000, 300000, 1000000, 1200000, 2500000, 5000000, 10000000 ] } } } ... ``` To correctly serialize this we need to convert to JSON integers. We can achieve this by feeding nlohmann JSON objects directly with integers instead of strings. For the C++ pistache server adapt the enum models to serialize integer values if possible. --- .../cpp-pistache-server/model-source.mustache | 8 ++++---- .../cpp-pistache-server/model-struct-source.mustache | 12 ++++++------ 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/cpp-pistache-server/model-source.mustache b/modules/openapi-generator/src/main/resources/cpp-pistache-server/model-source.mustache index 776049e6efcb..ac016229aee6 100644 --- a/modules/openapi-generator/src/main/resources/cpp-pistache-server/model-source.mustache +++ b/modules/openapi-generator/src/main/resources/cpp-pistache-server/model-source.mustache @@ -101,11 +101,11 @@ void to_json(nlohmann::json& j, const {{classname}}& o) {{#enumVars}} {{#-first}} case {{classname}}::e{{classname}}::INVALID_VALUE_OPENAPI_GENERATED: - j = "INVALID_VALUE_OPENAPI_GENERATED"; + j = {{#isInteger}}0{{/isInteger}}{{^isInteger}}"INVALID_VALUE_OPENAPI_GENERATED"{{/isInteger}}; break; {{/-first}} case {{classname}}::e{{classname}}::{{name}}: - j = "{{value}}"; + j = {{#isInteger}}{{value}}{{/isInteger}}{{^isInteger}}"{{value}}"{{/isInteger}}; break; {{/enumVars}} }{{/allowableValues}}{{/isEnum}}{{#vendorExtensions.x-is-string-enum-container}}{{#anyOf}}{{#-first}}to_json(j, o.m_value);{{/-first}}{{/anyOf}}{{/vendorExtensions.x-is-string-enum-container}} @@ -121,9 +121,9 @@ void from_json(const nlohmann::json& j, {{classname}}& o) } {{/required}} {{/vars}} {{#isEnum}}{{#allowableValues}} - auto s = j.get(); + auto s = j.get<{{#isInteger}}{{dataType}}{{/isInteger}}{{^isInteger}}std::string{{/isInteger}}>(); {{#enumVars}} - {{#-first}}if{{/-first}}{{^-first}}else if{{/-first}} (s == "{{value}}") { + {{#-first}}if{{/-first}}{{^-first}}else if{{/-first}} (s == {{#isInteger}}{{value}}{{/isInteger}}{{^isInteger}}"{{value}}"{{/isInteger}}) { o.setValue({{classname}}::e{{classname}}::{{name}}); } {{#-last}} else { std::stringstream ss; diff --git a/modules/openapi-generator/src/main/resources/cpp-pistache-server/model-struct-source.mustache b/modules/openapi-generator/src/main/resources/cpp-pistache-server/model-struct-source.mustache index 7b4d0c272a74..4d7dbfe0a902 100644 --- a/modules/openapi-generator/src/main/resources/cpp-pistache-server/model-struct-source.mustache +++ b/modules/openapi-generator/src/main/resources/cpp-pistache-server/model-struct-source.mustache @@ -96,12 +96,12 @@ void to_json(nlohmann::json& j, const {{classname}}& o) {{#enumVars}} {{#-first}} case {{classname}}::e{{classname}}::INVALID_VALUE_OPENAPI_GENERATED: - j = "INVALID_VALUE_OPENAPI_GENERATED"; - break; + j = {{#isInteger}}0{{/isInteger}}{{^isInteger}}"INVALID_VALUE_OPENAPI_GENERATED"{{/isInteger}}; + break; {{/-first}} case {{classname}}::e{{classname}}::{{name}}: - j = "{{value}}"; - break; + j = {{#isInteger}}{{value}}{{/isInteger}}{{^isInteger}}"{{value}}"{{/isInteger}}; + break; {{/enumVars}} }{{/allowableValues}}{{/isEnum}}{{#vendorExtensions.x-is-string-enum-container}}{{#anyOf}}{{#-first}}to_json(j, o.m_value);{{/-first}}{{/anyOf}}{{/vendorExtensions.x-is-string-enum-container}} } @@ -117,10 +117,10 @@ void from_json(const nlohmann::json& j, {{classname}}& o) }{{/required}} {{/vars}} {{#isEnum}}{{#allowableValues}} - auto s = j.get(); + auto s = j.get<{{#isInteger}}{{dataType}}{{/isInteger}}{{^isInteger}}std::string{{/isInteger}}>(); {{#enumVars}} {{#-first}} - if{{/-first}}{{^-first}}else if{{/-first}}(s == "{{value}}") { + if{{/-first}}{{^-first}}else if{{/-first}}(s == {{#isInteger}}{{value}}{{/isInteger}}{{^isInteger}}"{{value}}"{{/isInteger}}) { o.value = {{classname}}::e{{classname}}::{{name}}; } {{#-last}} else { std::stringstream ss;