Skip to content

Commit

Permalink
fix the @RequestBean and @QueryBean sometime do not work.
Browse files Browse the repository at this point in the history
Fixes #144
Fixes #141
  • Loading branch information
LCDZhao-Z authored Apr 28, 2022
2 parents 9bbfe7b + e706a8e commit 4f2cb40
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
*/
public class RequestBeanParamResolver implements ParamResolverFactory {

private static final Map<Class<?>, ParamResolver> META_CACHE = new ConcurrentHashMap<>(16);
protected final Map<Class<?>, ParamResolver> metaCache = new ConcurrentHashMap<>(16);
private final DeployContext ctx;

public RequestBeanParamResolver(DeployContext ctx) {
Expand All @@ -70,10 +70,10 @@ public ParamResolver createResolver(Param param,
Class<?> type = param.type();
// instantiate target object by unsafe

ParamResolver resolver = META_CACHE.get(type);
ParamResolver resolver = metaCache.get(type);
if (resolver == null) {
// no need to check the previous value
META_CACHE.putIfAbsent(type, resolver = new Resolver(newTypeMeta(type,
metaCache.putIfAbsent(type, resolver = new Resolver(newTypeMeta(type,
converters,
ctx.resolverFactory().orElse(null))));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,17 @@ public Optional<StringConverter> createConverter(Key key) {
} else {
converter = ConverterUtils.str2ObjectConverter(key.type());
}
if (converter == null) {
throw new IllegalArgumentException("There is no suitable StringConverter for class(" + key.type() + "),"
+ "It should have a constructor that accepts a single String argument or "
+ "have a static method named valueOf() or fromString() that accepts a single String argument.");
}

return Optional.of(new StringConverter() {
@Override
public Object fromString(String value) {
if (converter == null) {
throw new IllegalArgumentException("There is no suitable StringConverter for class(" +
key.type() + "),It should have a constructor that accepts a single String argument" +
" or have a static method named valueOf() or fromString() that accepts a " +
"single String argument.");
}

return converter.apply(value);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright 2022 OPPO ESA Stack Project
* 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
* http://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 io.esastack.restlight.integration.springmvc.cases.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface CustomFieldParam {
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@
import io.esastack.restlight.core.resolver.rspentity.AbstractResponseEntityResolver;
import io.esastack.restlight.core.serialize.HttpRequestSerializer;
import io.esastack.restlight.core.serialize.HttpResponseSerializer;
import io.esastack.restlight.integration.springmvc.cases.annotation.CustomFieldParam;
import io.esastack.restlight.integration.springmvc.cases.annotation.CustomRequestBean;
import io.esastack.restlight.server.context.RequestContext;
import io.esastack.restlight.integration.springmvc.cases.annotation.CustomRequestBody;
import io.esastack.restlight.integration.springmvc.cases.annotation.CustomResponseBody;
import io.esastack.restlight.integration.springmvc.entity.UserData;
import io.esastack.restlight.server.context.RequestContext;
import io.netty.handler.codec.http.HttpHeaderNames;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Expand Down Expand Up @@ -126,4 +127,20 @@ public boolean supports(HandlerMethod method) {
}
};
}

@Bean
public ParamResolverFactory customParamResolverFactory() {
return new ParamResolverFactory() {
@Override
public ParamResolver createResolver(Param param, StringConverterProvider converters,
List<? extends HttpRequestSerializer> serializers) {
return context -> context.request().getParam("name");
}

@Override
public boolean supports(Param param) {
return param.hasAnnotation(CustomFieldParam.class);
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,9 @@ public UserData customBody(@CustomRequestBody UserData user) {
public String customResponseBody(@RequestParam String name) {
return name;
}

@GetMapping("get/param/wrong")
public UserData paramWrong(@RequestParam UserData user) {
return user;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@

package io.esastack.restlight.integration.springmvc.entity;

import io.esastack.restlight.integration.springmvc.cases.annotation.CustomFieldParam;

import java.math.BigDecimal;
import java.util.Date;

public class UserData {

@CustomFieldParam
private String name;

private Integer age;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,11 @@ public void testCustomResponseBody() throws Exception {
UserData user = response.bodyToEntity(UserData.class);
Assert.assertEquals("test", user.getName());
}

@Test
public void testParamWrong() throws Exception {
RestResponseBase response = restClient.get(domain + "/annotation/get/param/wrong")
.addParam("user", "").execute().toCompletableFuture().get();
Assert.assertEquals(HttpStatus.INTERNAL_SERVER_ERROR.code(), response.status());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
package io.esastack.restlight.integration.springmvc.test;

import io.esastack.restclient.RestResponseBase;
import io.netty.channel.nio.NioEventLoopGroup;
import org.junit.Assert;
import org.junit.Test;

Expand All @@ -33,7 +32,7 @@ public void testBizAware() throws Exception {
public void testIoAware() throws Exception {
RestResponseBase response = restClient.get(domain + "/aware/get/io").execute()
.toCompletableFuture().get();
Assert.assertEquals(NioEventLoopGroup.class.getName(), response.bodyToEntity(String.class));
Assert.assertTrue(response.bodyToEntity(String.class).contains("EventLoopGroup"));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class MessageBodyReaderAdapterTest {
void testBasic() {
assertThrows(NullPointerException.class, () -> new MessageBodyWriterAdapter<>(null));
MessageBodyReaderAdapter<?> adapter = new MessageBodyReaderAdapter<>(mock(Providers.class));
assertEquals(Ordered.LOWEST_PRECEDENCE, adapter.getOrder());
assertEquals(90, adapter.getOrder());
assertTrue(adapter.supports(mock(Param.class)));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ void testWriteTo() throws Throwable {

final Providers providers = mock(Providers.class);
final MessageBodyWriterAdapter<?> adapter = new MessageBodyWriterAdapter<>(providers);
assertEquals(Ordered.LOWEST_PRECEDENCE, adapter.getOrder());
assertEquals(90, adapter.getOrder());

final HttpResponse response = mock(HttpResponse.class);
final ResponseEntity entity = mock(ResponseEntity.class);
Expand Down

0 comments on commit 4f2cb40

Please sign in to comment.