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

The builder clones itself before enrichment #2117

Merged
32 changes: 17 additions & 15 deletions core/src/main/java/feign/AsyncFeign.java
Original file line number Diff line number Diff line change
Expand Up @@ -186,30 +186,32 @@ public AsyncBuilder<C> invocationHandlerFactory(InvocationHandlerFactory invocat
}

public AsyncFeign<C> build() {
super.enrich();
AsyncBuilder<C> enrichedBuilder = super.enrich();
Copy link
Member

@velo velo Jul 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My major concern is that in the future, someone will access a field directly, forget the enrichBuilder prefix

What if, we move the builder body to a new method, and the we invoke the "internalBuild()" in the enriched instance?


AsyncResponseHandler responseHandler =
(AsyncResponseHandler) Capability.enrich(
new AsyncResponseHandler(
logLevel,
logger,
decoder,
errorDecoder,
dismiss404,
closeAfterDecode, responseInterceptor),
enrichedBuilder.logLevel,
enrichedBuilder.logger,
enrichedBuilder.decoder,
enrichedBuilder.errorDecoder,
enrichedBuilder.dismiss404,
enrichedBuilder.closeAfterDecode, enrichedBuilder.responseInterceptor),
AsyncResponseHandler.class,
capabilities);
enrichedBuilder.capabilities);

final MethodHandler.Factory<C> methodHandlerFactory =
new AsynchronousMethodHandler.Factory<>(
client, retryer, requestInterceptors,
responseHandler, logger, logLevel,
propagationPolicy, methodInfoResolver,
new RequestTemplateFactoryResolver(encoder, queryMapEncoder),
options, decoder, errorDecoder);
enrichedBuilder.client, enrichedBuilder.retryer, enrichedBuilder.requestInterceptors,
responseHandler, enrichedBuilder.logger, enrichedBuilder.logLevel,
enrichedBuilder.propagationPolicy, enrichedBuilder.methodInfoResolver,
new RequestTemplateFactoryResolver(enrichedBuilder.encoder,
enrichedBuilder.queryMapEncoder),
enrichedBuilder.options, enrichedBuilder.decoder, enrichedBuilder.errorDecoder);
final ReflectiveFeign<C> feign =
new ReflectiveFeign<>(contract, methodHandlerFactory, invocationHandlerFactory,
defaultContextSupplier);
new ReflectiveFeign<>(enrichedBuilder.contract, methodHandlerFactory,
enrichedBuilder.invocationHandlerFactory,
enrichedBuilder.defaultContextSupplier);
return new AsyncFeign<>(feign);
}
}
Expand Down
50 changes: 29 additions & 21 deletions core/src/main/java/feign/BaseBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import java.util.Objects;
import java.util.stream.Collectors;

public abstract class BaseBuilder<B extends BaseBuilder<B>> {
public abstract class BaseBuilder<B extends BaseBuilder<B>> implements Cloneable {

private final B thisB;

Expand Down Expand Up @@ -224,33 +224,41 @@ public B addCapability(Capability capability) {
return thisB;
}

@SuppressWarnings("unchecked")
protected B enrich() {
if (capabilities.isEmpty()) {
return thisB;
}

getFieldsToEnrich().forEach(field -> {
field.setAccessible(true);
try {
final Object originalValue = field.get(thisB);
final Object enriched;
if (originalValue instanceof List) {
Type ownerType = ((ParameterizedType) field.getGenericType()).getActualTypeArguments()[0];
enriched = ((List) originalValue).stream()
.map(value -> Capability.enrich(value, (Class<?>) ownerType, capabilities))
.collect(Collectors.toList());
} else {
enriched = Capability.enrich(originalValue, field.getType(), capabilities);
try {
B clone = (B) thisB.clone();

getFieldsToEnrich().forEach(field -> {
field.setAccessible(true);
try {
final Object originalValue = field.get(clone);
final Object enriched;
if (originalValue instanceof List) {
Type ownerType =
((ParameterizedType) field.getGenericType()).getActualTypeArguments()[0];
enriched = ((List) originalValue).stream()
.map(value -> Capability.enrich(value, (Class<?>) ownerType, capabilities))
.collect(Collectors.toList());
} else {
enriched = Capability.enrich(originalValue, field.getType(), capabilities);
}
field.set(clone, enriched);
} catch (IllegalArgumentException | IllegalAccessException e) {
throw new RuntimeException("Unable to enrich field " + field, e);
} finally {
field.setAccessible(false);
}
field.set(thisB, enriched);
} catch (IllegalArgumentException | IllegalAccessException e) {
throw new RuntimeException("Unable to enrich field " + field, e);
} finally {
field.setAccessible(false);
}
});
});

return thisB;
return clone;
} catch (CloneNotSupportedException e) {
throw new AssertionError(e);
}
}

List<Field> getFieldsToEnrich() {
Expand Down
22 changes: 14 additions & 8 deletions core/src/main/java/feign/Feign.java
Original file line number Diff line number Diff line change
Expand Up @@ -197,17 +197,23 @@ public <T> T target(Target<T> target) {
}

public Feign build() {
super.enrich();
Builder enrichedBuilder = super.enrich();

final ResponseHandler responseHandler =
new ResponseHandler(logLevel, logger, decoder, errorDecoder,
dismiss404, closeAfterDecode, responseInterceptor);
new ResponseHandler(enrichedBuilder.logLevel, enrichedBuilder.logger,
enrichedBuilder.decoder, enrichedBuilder.errorDecoder,
enrichedBuilder.dismiss404, enrichedBuilder.closeAfterDecode,
enrichedBuilder.responseInterceptor);
MethodHandler.Factory<Object> methodHandlerFactory =
new SynchronousMethodHandler.Factory(client, retryer, requestInterceptors,
responseHandler, logger, logLevel, propagationPolicy,
new RequestTemplateFactoryResolver(encoder, queryMapEncoder),
options);
return new ReflectiveFeign<>(contract, methodHandlerFactory, invocationHandlerFactory,
new SynchronousMethodHandler.Factory(enrichedBuilder.client, enrichedBuilder.retryer,
enrichedBuilder.requestInterceptors,
responseHandler, enrichedBuilder.logger, enrichedBuilder.logLevel,
enrichedBuilder.propagationPolicy,
new RequestTemplateFactoryResolver(enrichedBuilder.encoder,
enrichedBuilder.queryMapEncoder),
enrichedBuilder.options);
return new ReflectiveFeign<>(enrichedBuilder.contract, methodHandlerFactory,
enrichedBuilder.invocationHandlerFactory,
() -> null);
}
}
Expand Down
2 changes: 2 additions & 0 deletions core/src/test/java/feign/BaseBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package feign;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.RETURNS_MOCKS;
import java.lang.reflect.Field;
Expand Down Expand Up @@ -46,6 +47,7 @@ private void test(BaseBuilder<?> builder, int expectedFieldsCount)
}
assertTrue("Field was not enriched " + field, Mockito.mockingDetails(mockedValue)
.isMock());
assertNotSame(builder, enriched);
}

}
Expand Down
33 changes: 17 additions & 16 deletions kotlin/src/main/java/feign/kotlin/CoroutineFeign.java
Original file line number Diff line number Diff line change
Expand Up @@ -158,24 +158,25 @@ public <T> T target(Target<T> target, C context) {

@SuppressWarnings("unchecked")
public CoroutineFeign<C> build() {
super.enrich();
CoroutineBuilder<C> enrichedBuilder = super.enrich();

AsyncFeign<C> asyncFeign = (AsyncFeign<C>) AsyncFeign.builder()
.logLevel(logLevel)
.client((AsyncClient<Object>) client)
.decoder(decoder)
.errorDecoder(errorDecoder)
.contract(contract)
.retryer(retryer)
.logger(logger)
.encoder(encoder)
.queryMapEncoder(queryMapEncoder)
.options(options)
.requestInterceptors(requestInterceptors)
.responseInterceptor(responseInterceptor)
.invocationHandlerFactory(invocationHandlerFactory)
.defaultContextSupplier((AsyncContextSupplier<Object>) defaultContextSupplier)
.methodInfoResolver(methodInfoResolver)
.logLevel(enrichedBuilder.logLevel)
.client((AsyncClient<Object>) enrichedBuilder.client)
.decoder(enrichedBuilder.decoder)
.errorDecoder(enrichedBuilder.errorDecoder)
.contract(enrichedBuilder.contract)
.retryer(enrichedBuilder.retryer)
.logger(enrichedBuilder.logger)
.encoder(enrichedBuilder.encoder)
.queryMapEncoder(enrichedBuilder.queryMapEncoder)
.options(enrichedBuilder.options)
.requestInterceptors(enrichedBuilder.requestInterceptors)
.responseInterceptor(enrichedBuilder.responseInterceptor)
.invocationHandlerFactory(enrichedBuilder.invocationHandlerFactory)
.defaultContextSupplier(
(AsyncContextSupplier<Object>) enrichedBuilder.defaultContextSupplier)
.methodInfoResolver(enrichedBuilder.methodInfoResolver)
.build();
return new CoroutineFeign<>(asyncFeign);
}
Expand Down