Skip to content

Commit

Permalink
[C++][Pistache] Serialize integer enums if possible (#16080)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
CTerasa-ep committed Jul 13, 2023
1 parent e77f9ea commit 7f480cb
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
}
Expand All @@ -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;
Expand Down

0 comments on commit 7f480cb

Please sign in to comment.