Skip to content

[BUG][cpp-qt-client] OAuth code block emitted multiple times when multiple OAuth2 security schemes are defined #23011

@andrea-masciotta-idsg

Description

@andrea-masciotta-idsg

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator?
  • 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?
Description

When an OpenAPI spec defines multiple OAuth2 security schemes as alternatives at the global level, the cpp-qt-client generator emits the OAuth authentication code block once per scheme, causing C++ compilation errors due to variable redefinition.

For example, if the spec has:

security:
  - oauth_realm_a: []
  - oauth_realm_b: []

The generator loops over authMethods and emits the OAuth setup code twice in the same function scope, redeclaring variables like scope and token.

openapi-generator version

Tested on:

  • 7.15.0
  • 7.20.0

Both exhibit the same behavior.

OpenAPI declaration file content or url

Minimal reproducible example:

openapi: 3.0.4
info:
  title: Test API
  version: 1.0.0
servers:
  - url: http://localhost:8080/api
security:
  - oauth_primary: []
  - oauth_secondary: []

paths:
  /test:
    get:
      operationId: testEndpoint
      responses:
        "200":
          description: OK

components:
  securitySchemes:
    oauth_primary:
      type: oauth2
      description: Primary OAuth server
      flows:
        authorizationCode:
          authorizationUrl: http://localhost/auth/primary/authorize
          tokenUrl: http://localhost/auth/primary/token
          scopes: {}
    oauth_secondary:
      type: oauth2
      description: Secondary OAuth server
      flows:
        authorizationCode:
          authorizationUrl: http://localhost/auth/secondary/authorize
          tokenUrl: http://localhost/auth/secondary/token
          scopes: {}
Generation Details
openapi-generator-cli generate -g cpp-qt-client -i test-api.yaml -o generated/
Steps to reproduce
  1. Create the YAML file above
  2. Generate cpp-qt-client code
  3. Attempt to compile the generated code
Actual Output

The generated OAIDefaultApi.cpp contains duplicate code blocks:

void OAIDefaultApi::testEndpoint() {
    // ... request setup ...
    
    // First OAuth block (oauth_primary)
    _OauthMethod = 2;
    _implicitFlow.unlink();
    _credentialFlow.unlink();
    _passwordFlow.unlink();
    _authFlow.link();
    QStringList scope;                              // <-- First declaration
    auto token = _authFlow.getToken(scope.join(" ")); // <-- First declaration
    // ... more code ...

    // Second OAuth block (oauth_secondary)  
    _OauthMethod = 2;
    _implicitFlow.unlink();
    _credentialFlow.unlink();
    _passwordFlow.unlink();
    _authFlow.link();
    QStringList scope;                              // <-- ERROR: redefinition
    auto token = _authFlow.getToken(scope.join(" ")); // <-- ERROR: redefinition
    // ... more code ...

Compilation errors:

error C2086: 'QStringList scope': redefinition
error C2371: 'token': redefinition; different basic types
Expected Output

Only one OAuth code block should be generated. Since the security schemes are alternatives (OR logic), the client only needs to support one OAuth flow at runtime.

Related issues/PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions