fix(rest): Fix rest convert error (#13783)

This commit is contained in:
Sean Yang 2024-02-26 10:05:51 +08:00 committed by GitHub
parent 97d5563990
commit 044a775373
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 17 additions and 43 deletions

View File

@ -23,6 +23,9 @@ import org.apache.dubbo.rpc.protocol.tri.rest.RestConstants;
import org.apache.dubbo.rpc.protocol.tri.rest.mapping.meta.ParameterMeta;
import org.apache.dubbo.rpc.protocol.tri.rest.util.DefaultRestToolKit;
import javax.ws.rs.core.MultivaluedHashMap;
import javax.ws.rs.core.MultivaluedMap;
final class JaxrsRestToolKit extends DefaultRestToolKit {
private final BeanArgumentBinder binder;
@ -32,6 +35,17 @@ final class JaxrsRestToolKit extends DefaultRestToolKit {
binder = new BeanArgumentBinder(frameworkModel);
}
@Override
public Object convert(Object value, ParameterMeta parameter) {
if (MultivaluedMap.class.isAssignableFrom(parameter.getType())) {
if (value instanceof MultivaluedMap) {
return value;
}
return typeConverter.convert(value, MultivaluedHashMap.class);
}
return super.convert(value, parameter);
}
@Override
public int getDialect() {
return RestConstants.DIALECT_JAXRS;

View File

@ -1,31 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.dubbo.rpc.protocol.tri.rest.support.jaxrs;
import org.apache.dubbo.common.convert.Converter;
import javax.ws.rs.core.MultivaluedHashMap;
import javax.ws.rs.core.MultivaluedMap;
@SuppressWarnings("rawtypes")
public class MultivaluedMapCreationConverter implements Converter<Integer, MultivaluedMap> {
@Override
public MultivaluedMap<?, ?> convert(Integer source) {
return new MultivaluedHashMap<>(source);
}
}

View File

@ -1 +0,0 @@
integer-to-multi-valued-map=org.apache.dubbo.rpc.protocol.tri.rest.support.jaxrs.MultivaluedMapCreationConverter

View File

@ -730,11 +730,11 @@ public class GeneralTypeConverter implements TypeConverter {
}
protected Collection customCreateCollection(Class targetClass, int size) {
return null;
return converterUtil == null ? null : (Collection) converterUtil.convertIfPossible(size, targetClass);
}
private Map customCreateMap(Class targetClass, int size) {
return null;
protected Map customCreateMap(Class targetClass, int size) {
return converterUtil == null ? null : (Map) converterUtil.convertIfPossible(size, targetClass);
}
private Collection createCollection(Class targetClass, int size) {
@ -792,10 +792,6 @@ public class GeneralTypeConverter implements TypeConverter {
if (collection != null) {
return collection;
}
collection = (Collection) converterUtil.convertIfPossible(size, targetClass);
if (collection != null) {
return collection;
}
if (targetClass.isAssignableFrom(ArrayList.class)) {
return new ArrayList<>(size);
}
@ -934,10 +930,6 @@ public class GeneralTypeConverter implements TypeConverter {
if (map != null) {
return map;
}
map = (Map) converterUtil.convertIfPossible(size, targetClass);
if (map != null) {
return map;
}
if (targetClass.isAssignableFrom(LinkedHashMap.class)) {
return CollectionUtils.newLinkedHashMap(size);
}