You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We are using okhttp-gson library with async methods of API Client to integrate with foreign API.
We have described response as array in specification for our path in API.
But we faced problem that foreign API may produce array or object in some cases in response:
array - as success response.
object - as negative response.
HTTP status is 200 for both responses.
It leads to JsonParseException in Gson.
Problem
Unfortunately, looks like generated ApiClient does not handle RuntimeExcaption (e.g. JsonParseException) and do not notified callback about error when parsing the response.
In this case our code does not receive the callback and keep waiting the response till timeout.
openapi-generator version
4.2.3 (additionaly checked on latest master 5.0.0-beta)
OpenAPI declaration file content or url
Command line used for generation
Steps to reproduce
Specify paths to receive array of objects as response.
Generate java project using okhttp-gson library.
Try to send request using async methods of ApiClient and receive an object in response.
ER: Callback were notified through callback.onFailure.
AR: JsonParseException were swallowed and callback were not notified at all.
Exception in thread "OkHttp Dispatcher" com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $
at com.google.gson.Gson.fromJson(Gson.java:944)
at com.google.gson.Gson.fromJson(Gson.java:897)
at com.google.gson.Gson.fromJson(Gson.java:846)
at net.testproject.api.invoker.JSON.deserialize(JSON.java:138)
at net.testproject.api.invoker.ApiClient.deserialize(ApiClient.java:764)
at net.testproject.api.invoker.ApiClient.handleResponse(ApiClient.java:967)
at net.testproject.api.invoker.ApiClient$1.onResponse(ApiClient.java:933)
at okhttp3.internal.connection.RealCall$AsyncCall.run(RealCall.kt:504)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:834)Caused by: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $
at com.google.gson.stream.JsonReader.beginArray(JsonReader.java:351)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:80)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:61)
at com.google.gson.Gson.fromJson(Gson.java:932)
... 10 more
Bug Report Checklist
Description
We are using okhttp-gson library with async methods of API Client to integrate with foreign API.
We have described response as array in specification for our path in API.
But we faced problem that foreign API may produce array or object in some cases in response:
HTTP status is 200 for both responses.
It leads to JsonParseException in Gson.
Problem
Unfortunately, looks like generated ApiClient does not handle RuntimeExcaption (e.g. JsonParseException) and do not notified callback about error when parsing the response.
In this case our code does not receive the callback and keep waiting the response till timeout.
openapi-generator version
4.2.3 (additionaly checked on latest master 5.0.0-beta)
OpenAPI declaration file content or url
Command line used for generation
Steps to reproduce
ER: Callback were notified through
callback.onFailure
.AR:
JsonParseException
were swallowed and callback were not notified at all.Exception in thread "OkHttp Dispatcher" com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $
at com.google.gson.Gson.fromJson(Gson.java:944)
at com.google.gson.Gson.fromJson(Gson.java:897)
at com.google.gson.Gson.fromJson(Gson.java:846)
at net.testproject.api.invoker.JSON.deserialize(JSON.java:138)
at net.testproject.api.invoker.ApiClient.deserialize(ApiClient.java:764)
at net.testproject.api.invoker.ApiClient.handleResponse(ApiClient.java:967)
at net.testproject.api.invoker.ApiClient$1.onResponse(ApiClient.java:933)
at okhttp3.internal.connection.RealCall$AsyncCall.run(RealCall.kt:504)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:834)Caused by: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $
at com.google.gson.stream.JsonReader.beginArray(JsonReader.java:351)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:80)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:61)
at com.google.gson.Gson.fromJson(Gson.java:932)
... 10 more
Related issues/PRs
#7089
Suggest a fix
Catch any Exception in async context, wrap it in ApiException and notify callback about incident (see PR).
I'm not sure this is the best solution but it solves our issue. Feel free to suggest another solution.
The text was updated successfully, but these errors were encountered: