Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG][JAVA] Fix error handling in okhttp-gson async api client #7089 #7090

Closed
4 of 6 tasks
dmavrichev opened this issue Jul 31, 2020 · 0 comments
Closed
4 of 6 tasks

Comments

@dmavrichev
Copy link
Contributor

Bug Report Checklist

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

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
  1. Specify paths to receive array of objects as response.
  2. Generate java project using okhttp-gson library.
  3. 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

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants