[BUG] [C++][Qt5] Optional parameters are not optional in generated code for API
Created by: xconverge
Bug Report Checklist
-
Have you provided a full/minimal spec to reproduce the issue? -
Have you validated the input using an OpenAPI validator (example)? -
Have you tested with the latest master to confirm the issue still exists? -
Have you searched for related issues/PRs? -
What's the actual output vs expected output? -
[Optional] Sponsorship to speed up the bug fix or feature request (example)
Description
The qt5 generator does not output optional parameters to the api. All parameters are still required and the client code does not provide a way of omitting them.
openapi-generator version
5.0.1
OpenAPI declaration file content or url
openapi: 3.0.0
info:
version: 0.0.1
title: Test
paths:
/endpoint:
get:
operationId: getEndpoint
description: "-"
parameters:
- name: param1
required: true
in: query
description: required parameter 1
schema:
type: string
- name: param2
required: false
in: query
description: should be optional parameter 2
schema:
type: string
responses:
200:
description: Success
Generation Details
openapi-generator-cli generate -i test.yaml -g cpp-qt5-client --additional-properties=optionalProjectFile=false -o te
stOutput
Steps to reproduce
header:
void getEndpoint(const QString ¶m1, const QString ¶m2);
and the implementation::
void OAIDefaultApi::getEndpoint(const QString ¶m1, const QString ¶m2) {
QString fullPath = QString(_serverConfigs["getEndpoint"][_serverIndices.value("getEndpoint")].URL()+"/endpoint");
if (fullPath.indexOf("?") > 0)
fullPath.append("&");
else
fullPath.append("?");
fullPath.append(QUrl::toPercentEncoding("param1")).append("=").append(QUrl::toPercentEncoding(::OpenAPI::toStringValue(param1)));
if (fullPath.indexOf("?") > 0)
fullPath.append("&");
else
fullPath.append("?");
fullPath.append(QUrl::toPercentEncoding("param2")).append("=").append(QUrl::toPercentEncoding(::OpenAPI::toStringValue(param2)));
Related issues/PRs
Similar to these bugs/associated PRs, but for qt5 of course... https://github.com/OpenAPITools/openapi-generator/issues/2012 https://github.com/OpenAPITools/openapi-generator/issues/3052
Suggest a fix
cpp-rest-sdk-client uses boost::optional and cpp-ue4 uses TOptional, so using std::optional with a flag to utilize c++17 seems pretty clean to me
QVariant could also potentially be used but then you lose a lot of the self documenting niceties of the types
Could also just be a change to the api mustache files to utilize the required parameter from the definition, and then either create an overload or utilize some sort of default parameters and detect it and not append to the fullpath if it is set to these defaults (probably not a good idea). I think std::optional is the best answer though, even though it should have a flag.
I am willing to put some legwork into this if someone wants to push me in the right direction