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
When processing a JSON response body containing a field with base64-encoded data (type=string, format=byte), the Gson deserializer throws an the following exception:
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 13 path $.content] with root cause
java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 13 path $.content
at com.google.gson.stream.JsonReader.beginArray(JsonReader.java:351)
at com.google.gson.internal.bind.ArrayTypeAdapter.read(ArrayTypeAdapter.java:70)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:131)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:222)
at com.google.gson.Gson.fromJson(Gson.java:932)
at com.google.gson.Gson.fromJson(Gson.java:897)
at com.google.gson.Gson.fromJson(Gson.java:846)
at nl.pompkracht.portal.port.adapter.out.rest.ats.swagger.JSON.deserialize(JSON.java:134)
at nl.pompkracht.portal.port.adapter.out.rest.ats.swagger.ApiClient.deserialize(ApiClient.java:710)
at nl.pompkracht.portal.port.adapter.out.rest.ats.swagger.ApiClient.handleResponse(ApiClient.java:913)
at nl.pompkracht.portal.port.adapter.out.rest.ats.swagger.ApiClient.execute(ApiClient.java:840)
...
Swagger-codegen version
3.0.23
Swagger declaration file content or url
The following schema (part of the declaration file) corresponds to the object that cannot be deserialized. More, specifically, content is the offending field here:
Generate Java client from OpenAPI spec with an operation that contains base-64 encoded data in the response body
Call the relevant method in the generated code
Verify that the Gson exception occurs
Related issues/PRs
Found an issue with serializing byte[] array types from generated server code, but this applies to consuming base64 data from generated client code, so not really related I think
Suggest a fix/enhancement
Was able to work around the issue by adding a JsonDeserializer type adapter for byte arrays to gson, like this:
ApiClient apiClient = new ApiClient();
var serializer = apiClient.getJSON();
serializer.setGson(serializer.getGson().newBuilder()
.registerTypeAdapter(byte[].class, new ByteArrayDeserializer())
.create()
);
....
private static class ByteArrayDeserializer implements JsonDeserializer<byte[]> {
@Override
public byte[] deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
return Base64.getDecoder().decode(json.getAsString());
}
}
The text was updated successfully, but these errors were encountered:
@jesperronn#4824 seems related indeed. However I do not think this is the same error, since that issue is fixed in v2.4.0 and this error was encountered in v3.0.23
I would like to add the label as you requested but I cannot find how to do that
Description
When processing a JSON response body containing a field with base64-encoded data (type=string, format=byte), the Gson deserializer throws an the following exception:
Swagger-codegen version
3.0.23
Swagger declaration file content or url
The following schema (part of the declaration file) corresponds to the object that cannot be deserialized. More, specifically,
content
is the offending field here:Command line used for generation
Steps to reproduce
Related issues/PRs
Found an issue with serializing byte[] array types from generated server code, but this applies to consuming base64 data from generated client code, so not really related I think
Suggest a fix/enhancement
Was able to work around the issue by adding a JsonDeserializer type adapter for byte arrays to gson, like this:
The text was updated successfully, but these errors were encountered: