Skip to content

[BUG][QT5] Invalid QT5-CPP code for generated API methods when input parameter header is used #6218

@ghost

Description

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • What's the version of OpenAPI Generator used?
  • Have you search for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Bounty to sponsor the fix (example)
Description

The generated CPP QT5 code is invalid when generating code using the SPEC below, although SPEC is valid.

More precisely, the cpp-qt5-client mustache code is affected at least in file:

  • api-body.mustache / api-header.mustache

In the generated code file "TestDefaultApi.cpp", the following method is invalid due to following two errors:

  • nullptr check to a const reference
  • insert into header list with invalid type <QString, TestLocale>. Needed types are <QString, QString>. TestLocale is the generated class, and has no QString() operator.
void TestDefaultApi::getEndpoint(const TestLocale &accept_language) {
    ...
    if (accept_language != nullptr) {
        input.headers.insert("Accept-Language", accept_language);
    }
    ...
}
openapi-generator version

First appearance in 4.3.0. Problem exists also in 4.3.1 and 4.2.3. and probably also lower versions.

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: Accept-Language
        in: header
        schema:
          $ref: '#/components/schemas/Locale'
      responses:
        200:
          description: Success
components:
  schemas:
    Locale:
      type: string
      description: 'Locale'
      enum: [ de_AT, en_US ]
Command line used for generation

java -cp openapi-generator-cli-4.3.1.jar org.openapitools.codegen.OpenAPIGenerator generate -i -g cpp-qt5-client -o ./ --model-name-prefix Test

Steps to reproduce
Related issues/PRs
Suggest a fix

Two approaches came quickly into my mind:

  1. Add an "generally applicable" QString operator() to the model-header.mustache code. This code might be similar as the asJson() mustache-code parts (but only returns the plain value as QString representation if set). This QString operator() is afterwards called in the api-body.mustache part, e.g.:
{{#headerParams}}
    if (!(static_cast<QString>({{paramName}}).isEmpty())) {
        input.headers.insert("{{baseName}}", static_cast<QString>({{paramName}})); // <-- QString() operator is called
    }
{{/headerParams}}
  1. An "addHeader" method already exists in api-body.mustache, which is fine. Therefore, remove the method input parameter and the code-part below from the mustache code which creates the endpoint API call (e.g. void TestDefaultApi::getEndpoint(const TestLocale &accept_language)). But only iff the input parameter is used for inserting headers (and the code below is created):
{{#headerParams}}
...
{{/headerParams}}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions