Openapi-generator: [BUG] httpClient.interceptors().add(retryingOAuth) on a immutabe list

Created on 2 Jun 2019  路  3Comments  路  Source: OpenAPITools/openapi-generator

Bug Report Checklist

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

Generating a Java SDK using openapi-generator with an oauth2 securityScheme causes an add() to be called on an UnmodifiableCollection

__produces:__

Exception in thread "main" java.lang.UnsupportedOperationException
    at java.util.Collections$UnmodifiableCollection.add(Collections.java:1055)
    at org.openapitools.client.ApiClient.<init>(ApiClient.java:117)
    at org.openapitools.client.Main.main(Main.java:12)

openapi-generator version

4.0.1

OpenAPI declaration file content or url
openapi: 3.0.0
info:
  version: 1.0.0
  title: Test
paths:
  /:
    get:
      summary: Get Test
      operationId: GetTest
      responses:
        200:
          description: Ok

components:
  securitySchemes:
    myauth:
      type: oauth2
      flows:
        authorizationCode:
          authorizationUrl: https://auth.com/authorize
          tokenUrl: https://auth.com/oauth/token
          scopes:
            read: Grants read access
security:
  - myauth: [read]
Command line used for generation
java -jar openapi-generator-cli.jar generate \
-i docs/public/openapi/test.yaml \
   -g java \
   -o test-sdk
Steps to reproduce
package org.openapitools.client.api;

import org.openapitools.client.ApiClient;
import org.openapitools.client.ApiException;

import java.util.HashMap;

public class Main {
    public static void main(String[] args) throws ApiException {
        final ApiClient apiClient = new ApiClient("someclientId", "someClientSecret", new HashMap<String, String>());
        final DefaultApi defaultApi = new DefaultApi(apiClient);
        defaultApi.getTest();
    }
}
Related issues/PRs
Suggest a fix
Java Bug

Most helpful comment

Looks like it broke when switching to OkHttp 3, the list returned by OkHttpClient .interceptors() wasn't immutable in previous versions. There are other places within ApiClient.mustache, which are already adding interceptors by way of OkHttpClient.Builder:

if (debugging) {
    loggingInterceptor = new HttpLoggingInterceptor();
    loggingInterceptor.setLevel(Level.BODY);
    httpClient = httpClient.newBuilder().addInterceptor(loggingInterceptor).build();
} else {
    httpClient.interceptors().remove(loggingInterceptor);
    loggingInterceptor = null;
}

But note, that while adding an interceptor this way works, the removal in the else-block would fail as well. It should instead be something like

httpClient = httpClient.newBuilder().interceptors().remove(loggingInterceptor).build();

(the list returned by OkHttpClient.Builder.interceptors() is mutable).

Basically every case of httpClient.interceptors().add/remove() in ApiClient.mustache needs to be replaced by the builder-approach.

All 3 comments

馃憤 Thanks for opening this issue!
馃彿 I have applied any labels matching special text in your issue.

The team will review the labels and make any necessary changes.

Looks like it broke when switching to OkHttp 3, the list returned by OkHttpClient .interceptors() wasn't immutable in previous versions. There are other places within ApiClient.mustache, which are already adding interceptors by way of OkHttpClient.Builder:

if (debugging) {
    loggingInterceptor = new HttpLoggingInterceptor();
    loggingInterceptor.setLevel(Level.BODY);
    httpClient = httpClient.newBuilder().addInterceptor(loggingInterceptor).build();
} else {
    httpClient.interceptors().remove(loggingInterceptor);
    loggingInterceptor = null;
}

But note, that while adding an interceptor this way works, the removal in the else-block would fail as well. It should instead be something like

httpClient = httpClient.newBuilder().interceptors().remove(loggingInterceptor).build();

(the list returned by OkHttpClient.Builder.interceptors() is mutable).

Basically every case of httpClient.interceptors().add/remove() in ApiClient.mustache needs to be replaced by the builder-approach.

Hi guys, facing the same issue trying to authorize via client credentials flow. Any updates on this?

Was this page helpful?
0 / 5 - 0 ratings