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

gson cannot deserialise to Void #18

Open
robinfriedli opened this issue Jul 25, 2023 · 1 comment · May be fixed by #19
Open

gson cannot deserialise to Void #18

robinfriedli opened this issue Jul 25, 2023 · 1 comment · May be fixed by #19

Comments

@robinfriedli
Copy link

setStats creates a ResponseTransformer that attempts to deserialise the response to java.lang.Void:

    private CompletionStage<Void> setStats(JSONObject jsonBody) {
        HttpUrl url = baseUrl.newBuilder()
                .addPathSegment("bots")
                .addPathSegment(botId)
                .addPathSegment("stats")
                .build();

        return post(url, jsonBody, Void.class);
    }

    private <E> CompletionStage<E> post(HttpUrl url, JSONObject jsonBody, Class<E> aClass) {
        return post(url, jsonBody, new DefaultResponseTransformer<>(aClass, gson));
    }

public class DefaultResponseTransformer<E> implements ResponseTransformer<E> {

    private final Class<E> aClass;
    private final Gson gson;

    public DefaultResponseTransformer(Class<E> aClass, Gson gson) {
        this.aClass = aClass;
        this.gson = gson;
    }

    @Override
    public E transform(Response response) throws IOException {
        String body = response.body().string();
        return gson.fromJson(body, aClass);
    }

}

However, this does not work on newer versions of java because java.base is a sealed module:

com.google.gson.JsonIOException: Failed making constructor 'java.lang.Void#Void()' accessible; either change its visibility or write a custom InstanceCreator or TypeAdapter for its declaring type: Unable to make private java.lang.Void() accessible: module java.base does not "opens java.lang" to unnamed module @45d84a20
	at com.google.gson.internal.ConstructorConstructor$3.construct(ConstructorConstructor.java:131)
	at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:211)
	at com.google.gson.Gson.fromJson(Gson.java:991)
	at com.google.gson.Gson.fromJson(Gson.java:956)
	at com.google.gson.Gson.fromJson(Gson.java:905)
	at com.google.gson.Gson.fromJson(Gson.java:876)
	at org.discordbots.api.client.io.DefaultResponseTransformer.transform(DefaultResponseTransformer.java:21)
	at org.discordbots.api.client.impl.DiscordBotListAPIImpl$1.onResponse(DiscordBotListAPIImpl.java:234)
	at okhttp3.internal.connection.RealCall$AsyncCall.run(RealCall.kt:519)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
	at java.base/java.lang.Thread.run(Thread.java:833)

I don't think it makes sense anyway. If you are not interested in the response, why deserialise it? Probably better to not use a ResponseTransformer and complete the future with null when done instead of deserialising to Void.

@robinfriedli
Copy link
Author

#19 added a PR that adds a ResponseTransformer implementation that just returns null rather than trying to deserialise for null

robinfriedli added a commit to robinfriedli/aiode that referenced this issue Jul 25, 2023
- use own fork to fix pr Top-gg-Community/java-sdk#18
- update stats for all shards on startup
- use callback to log errors and success
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant