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)
4.0.1
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]
java -jar openapi-generator-cli.jar generate \
-i docs/public/openapi/test.yaml \
-g java \
-o test-sdk
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();
}
}
馃憤 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?
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 withinApiClient.mustache, which are already adding interceptors by way ofOkHttpClient.Builder: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
(the list returned by
OkHttpClient.Builder.interceptors()is mutable).Basically every case of
httpClient.interceptors().add/remove()inApiClient.mustacheneeds to be replaced by the builder-approach.