question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[BUG] [C++][Qt5] Optional parameters are not optional in generated code for API

See original GitHub issue

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 &param1, const QString &param2);

and the implementation::


void OAIDefaultApi::getEndpoint(const QString &param1, const QString &param2) {
    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

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:13 (12 by maintainers)

github_iconTop GitHub Comments

1reaction
MartinDelillecommented, Feb 15, 2021

Your work would be more readable if you’ll generate the sample code by typing bin/generate-samples.sh bin/configs/cpp-qt5-client.yaml from the root directory.

1reaction
etherealjoycommented, Feb 15, 2021

If you know the data type you are expecting, you can probe the QVariant and get the type info. Not super intuitive, but at least no breaking changes.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to deal with optional parameters in browser API when ...
Example: The Error constructor ( new Error([message[, fileName[, lineNumber]]]) ) has two optional parameters (fileName and lineNumber) that I ...
Read more >
Using Optional and Nullable Properties in API Requests
Learn how optional and nullable properties can be used flexibly in combinations in each parameter of your API requests made from a SDK....
Read more >
Migrating from Swagger Codegen - OpenAPI Generator
If your API client is using named parameters in the function call (e.g. Perl required & optional parameters, Ruby optional parameters), you will ......
Read more >
Optional parameters should be passed to "base" calls
Generally, writing the least code that will readably do the job is a good thing, so omitting default parameter values seems to make...
Read more >
System Requirements Meshmixer 3.5
Added a hotkey for toggling printer bed rendering. Bug fixes: applying Offset to a mesh with boundary; silent install for Windows; using '<'...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found