Skip to content

Commit

Permalink
[ISSUE #144] remove the reuse of metaCache & fix the ci issue & fix t…
Browse files Browse the repository at this point in the history
…he to pojo start error
  • Loading branch information
brotherlu-xcq committed Apr 28, 2022
1 parent 9bbfe7b commit 19c6f39
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 9 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,16 @@ 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());
}
}

0 comments on commit 19c6f39

Please sign in to comment.