Skip to content

Commit

Permalink
Introduce property to disable default mapper per REST Client
Browse files Browse the repository at this point in the history
The default mapper has a very confusing behavior where
it throws an exception HTTP code > 400 even if the
method return type is Response.
This PR means to provide a way so users can disable this mapper
per client instead of globally.

Closes: quarkusio#44813
  • Loading branch information
geoand committed Nov 28, 2024
1 parent be0030f commit 4767332
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,13 @@ default Optional<String> uriReload() {
* This stacktrace will be used if the invocation throws an exception
*/
Optional<Boolean> captureStacktrace();

/**
* TODO:
* This property is not applicable to the RESTEasy Client.
*/
@WithDefault("${microprofile.rest.client.disable.default.mapper:false}")
Boolean disableDefaultMapper();
}

class RestClientKeysProvider implements Supplier<Iterable<String>> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,11 @@ static QuarkusRestClientBuilder newBuilder() {
*/
QuarkusRestClientBuilder userAgent(String userAgent);

/**
* TODO:
*/
QuarkusRestClientBuilder disableDefaultMapper(Boolean disable);

/**
* Based on the configured QuarkusRestClientBuilder, creates a new instance of the given REST interface to invoke API calls
* against.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,12 @@ public QuarkusRestClientBuilder userAgent(String userAgent) {
return this;
}

@Override
public QuarkusRestClientBuilder disableDefaultMapper(Boolean disable) {
proxy.disableDefaultMapper(disable);
return null;
}

@Override
public <T> T build(Class<T> clazz) throws IllegalStateException, RestClientDefinitionException {
return proxy.build(clazz);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ public class RestClientBuilderImpl implements RestClientBuilder {

private Boolean trustAll;
private String userAgent;
private Boolean disableDefaultMapper;

@Override
public RestClientBuilderImpl baseUrl(URL url) {
Expand Down Expand Up @@ -260,6 +261,11 @@ public RestClientBuilderImpl userAgent(String userAgent) {
return this;
}

public RestClientBuilderImpl disableDefaultMapper(Boolean disable) {
this.disableDefaultMapper = disableDefaultMapper;
return this;
}

@Override
public RestClientBuilderImpl executorService(ExecutorService executor) {
throw new IllegalArgumentException("Specifying executor service is not supported. " +
Expand Down Expand Up @@ -429,13 +435,6 @@ public <T> T build(Class<T> aClass) throws IllegalStateException, RestClientDefi
register(mapper.getKey(), mapper.getValue());
}

Object defaultMapperDisabled = getConfiguration().getProperty(DEFAULT_MAPPER_DISABLED);
Boolean globallyDisabledMapper = ConfigProvider.getConfig()
.getOptionalValue(DEFAULT_MAPPER_DISABLED, Boolean.class).orElse(false);
if (!globallyDisabledMapper && !(defaultMapperDisabled instanceof Boolean && (Boolean) defaultMapperDisabled)) {
exceptionMappers.add(new DefaultMicroprofileRestClientExceptionMapper());
}

exceptionMappers.sort(Comparator.comparingInt(ResponseExceptionMapper::getPriority));
redirectHandlers.sort(Comparator.comparingInt(RedirectHandler::getPriority));
clientBuilder.register(new MicroProfileRestClientResponseFilter(exceptionMappers));
Expand Down Expand Up @@ -483,6 +482,16 @@ public <T> T build(Class<T> aClass) throws IllegalStateException, RestClientDefi
clientBuilder.setUserAgent(restClients.userAgent().get());
}

Boolean effectiveDisableDefaultMapper = disableDefaultMapper;
if (effectiveDisableDefaultMapper == null) {
effectiveDisableDefaultMapper = ConfigProvider.getConfig().getOptionalValue(DEFAULT_MAPPER_DISABLED, Boolean.class)
.orElse(false);
}

if (!effectiveDisableDefaultMapper) {
exceptionMappers.add(new DefaultMicroprofileRestClientExceptionMapper());
}

Integer maxChunkSize = (Integer) getConfiguration().getProperty(QuarkusRestClientProperties.MAX_CHUNK_SIZE);
if (maxChunkSize != null) {
clientBuilder.maxChunkSize(maxChunkSize);
Expand Down Expand Up @@ -573,4 +582,5 @@ private MultiQueryParamMode toMultiQueryParamMode(QueryParamStyle queryParamStyl
}
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ private void configureCustomProperties(QuarkusRestClientBuilder builder) {

Boolean captureStacktrace = oneOf(restClientConfig.captureStacktrace()).orElse(configRoot.captureStacktrace());
builder.property(QuarkusRestClientProperties.CAPTURE_STACKTRACE, captureStacktrace);

builder.disableDefaultMapper(restClientConfig.disableDefaultMapper());
}

private static Function<MemorySize, Integer> intChunkSize() {
Expand Down

0 comments on commit 4767332

Please sign in to comment.