OpenAPITools / OpenAPITools/openapi-generator
[REQ][C++][Pistache] Use std::optional in models
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
Is your feature request related to a problem? Please describe.
The current code generation creates non-required parameters inside model classes with a stored value, and an additional boolean indicating whether the value is set or not.
Additional functions for the values are also generated, such as valueIsSet and unsetValue.
Describe the solution you'd like
Fairly recently the C++ standard for the pistache server was raised to C++ 17, which brings in std::optional<T>.
We could use this instead of a custom solution in the models, to increase the generated code's readability.
It would also allow us to make the value itself public, the code could look like this for example:
MyType o; // generated class
o.someValue = std::nullopt; // no value
o.someValue = std::make_optional("Hello"); // optional string, with a value
// checking for the value
if (o.someValue.has_value()) { }
// alternatively
if (o.someValue) { }
// Additonal nice to have features from std::optional
std::string value = o.someValue.value_or("no value was set!");
Describe alternatives you've considered
Since this could be a breaking change, depending on implementation, I would be a bit cautious with implementing it.
Additional context
See #9251
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
No files or tests are named. Start by locating the C++ Pistache model-generation entry point and reviewing #9251, then determine the compatibility impact of replacing stored values and presence flags with std::optional. Done means generated non-required model fields use std::optional with the requested value-checking behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp
- Domain
- api, backend
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 32/100