Skip to content
This repository has been archived by the owner on Dec 31, 2021. It is now read-only.

Commit

Permalink
Improve HttpMethodResponse#json(TypeReference<R> responseType)
Browse files Browse the repository at this point in the history
  • Loading branch information
flytreeleft committed Jan 16, 2021
1 parent 3167f3a commit a2950cc
Showing 1 changed file with 11 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package org.github.flytreeleft.nexus3.keycloak.plugin.internal.http;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Type;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.introspect.VisibilityChecker;
import org.keycloak.util.JsonSerialization;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Type;

public class HttpMethodResponse<R> {
private final HttpMethod<R> method;
Expand All @@ -35,10 +35,13 @@ public HttpMethodResponse<R> json(final TypeReference<R> responseType) {
@Override
public R execute() {
return method.execute((InputStream inputStream) -> {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
mapper.setVisibility(VisibilityChecker.Std.defaultInstance()
.withFieldVisibility(JsonAutoDetect.Visibility.ANY));

try {
JsonSerialization.mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
JsonSerialization.mapper.setVisibility(VisibilityChecker.Std.defaultInstance().withFieldVisibility(JsonAutoDetect.Visibility.ANY));
return JsonSerialization.readValue(inputStream, responseType);
return mapper.readValue(inputStream, responseType);
} catch (IOException e) {
throw new RuntimeException("Error parsing JSON response.", e);
}
Expand Down

0 comments on commit a2950cc

Please sign in to comment.