[C++][Pistache] Serialize integer enums if possible (#16080)

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.
This commit is contained in:
CTerasa-ep
2023-07-13 03:31:53 +02:00
committed by GitHub
parent e77f9ea783
commit 7f480cb936
2 changed files with 10 additions and 10 deletions

View File

@@ -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<std::string>();
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;

View File

@@ -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<std::string>();
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;