Merge remote-tracking branch 'origin/3.1.x'

This commit is contained in:
Olga Maciaszek-Sharma
2022-10-19 12:42:12 +02:00
3 changed files with 78 additions and 6 deletions

View File

@@ -37,8 +37,8 @@ import org.springframework.web.context.request.RequestContextHolder;
import static feign.Util.checkNotNull;
/**
* @author Grzejszczak
* @author Sharma
* @author Marcin Grzejszczak
* @author Olga Maciaszek-Sharma
* @author Niang
* @author Bohutskyi
* @author kim
@@ -163,7 +163,7 @@ class FeignCircuitBreakerInvocationHandler implements InvocationHandler {
* @return cached methods map for fallback invoking
*/
static Map<Method, Method> toFallbackMethod(Map<Method, InvocationHandlerFactory.MethodHandler> dispatch) {
Map<Method, Method> result = new LinkedHashMap<Method, Method>();
Map<Method, Method> result = new LinkedHashMap<>();
for (Method method : dispatch.keySet()) {
method.setAccessible(true);
result.put(method, method);

View File

@@ -17,6 +17,7 @@
package org.springframework.cloud.openfeign.support;
import java.io.IOException;
import java.lang.reflect.Array;
import java.util.Iterator;
import java.util.function.Predicate;
@@ -32,6 +33,7 @@ import static feign.form.ContentProcessor.CRLF;
/**
* @author Darren Foong
* @author Wu Daifu
*/
public abstract class AbstractFormWriter extends AbstractWriter {
@@ -61,10 +63,16 @@ public abstract class AbstractFormWriter extends AbstractWriter {
protected abstract String writeAsString(Object object) throws IOException;
private boolean isTypeOrCollection(Object object, Predicate<Object> isType) {
if (object == null) {
return false;
}
if (object.getClass().isArray()) {
Object[] array = (Object[]) object;
return array.length > 1 && isType.test(array[0]);
int len = Array.getLength(object);
if (len > 0) {
Object one = Array.get(object, 0);
return len > 1 && one != null && isType.test(one);
}
return false;
}
else if (object instanceof Iterable) {
Iterable<?> iterable = (Iterable<?>) object;

View File

@@ -0,0 +1,64 @@
/*
* Copyright 2013-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.openfeign.support;
import java.io.IOException;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.http.MediaType;
/**
* @author Wu Daifu
*/
class AbstractFormWriterTests {
@Test
void shouldCorrectlyResolveIfApplicableForCollection() {
MockFormWriter formWriter = new MockFormWriter();
Object object = new Object();
Assertions.assertFalse(formWriter.isApplicable(object));
object = new Object[] { new Object(), new Object() };
Assertions.assertFalse(formWriter.isApplicable(object));
object = new UserPojo();
Assertions.assertTrue(formWriter.isApplicable(object));
object = new UserPojo[] { new UserPojo(), new UserPojo() };
Assertions.assertTrue(formWriter.isApplicable(object));
object = new byte[] { '1', '2' };
Assertions.assertFalse(formWriter.isApplicable(object));
}
class MockFormWriter extends AbstractFormWriter {
@Override
protected MediaType getContentType() {
return null;
}
@Override
protected String writeAsString(Object object) throws IOException {
return null;
}
}
class UserPojo {
}
}