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

[Dubbo-267] Fix protobuf type lose field when deepCopy inJvmInvoker … #268

Merged
merged 1 commit into from
Dec 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ limitations under the License.
<version>${dubbo.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>${dubbo.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* 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.common.serialize.protobuf.support;

import static org.apache.dubbo.common.constants.LoggerCodeConstants.PROTOCOL_ERROR_DESERIALIZE;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.logger.ErrorTypeAwareLogger;
import org.apache.dubbo.common.logger.LoggerFactory;
import org.apache.dubbo.rpc.protocol.injvm.DefaultParamDeepCopyUtil;
import org.apache.dubbo.rpc.protocol.injvm.ParamDeepCopyUtil;

public class ProtobufParamDeepCopyUtil implements ParamDeepCopyUtil {
private static final ErrorTypeAwareLogger logger = LoggerFactory.getErrorTypeAwareLogger(DefaultParamDeepCopyUtil.class);

private ParamDeepCopyUtil delegate;

public ProtobufParamDeepCopyUtil(ParamDeepCopyUtil delegate) {
this.delegate = delegate;
}

@Override
public <T> T copy(URL url, Object src, Class<T> targetClass) {
boolean isProtobufTypeSupported = ProtobufUtils.isSupported(targetClass);
if(isProtobufTypeSupported){
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
ProtobufUtils.serialize(src, outputStream);

try (ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray())) {
T deserialize = ProtobufUtils.deserialize(inputStream, targetClass);
return deserialize;
} catch (IOException e) {
logger.error(PROTOCOL_ERROR_DESERIALIZE, "", "", "Unable to deep copy parameter to target class.", e);
}

} catch (Throwable e) {
logger.error(PROTOCOL_ERROR_DESERIALIZE, "", "", "Unable to deep copy parameter to target class.", e);
}

if (src.getClass().equals(targetClass)) {
return (T) src;
} else {
return null;
}
}
return delegate.copy(url, src, targetClass);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
protobufDeepUtil=org.apache.dubbo.common.serialize.protobuf.support.ProtobufParamDeepCopyUtil
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* 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.common.serialize.protobuf.support;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.common.serialize.protobuf.support.model.GooglePB;
import org.apache.dubbo.rpc.protocol.injvm.DefaultParamDeepCopyUtil;
import org.apache.dubbo.rpc.protocol.injvm.ParamDeepCopyUtil;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import com.google.protobuf.InvalidProtocolBufferException;

public class ProtobufParamDeepCopyUtilTest {
private ParamDeepCopyUtil paramDeepCopyUtil;

@BeforeEach
public void setUp() {
URL url = mockURL();
this.paramDeepCopyUtil = url.getOrDefaultFrameworkModel().getExtensionLoader(ParamDeepCopyUtil.class)
.getExtension(url.getParameter(CommonConstants.INJVM_COPY_UTIL_KEY, DefaultParamDeepCopyUtil.NAME));
}

@Test
public void testProtobufDeepCopy() throws InvalidProtocolBufferException {
ProtobufUtils.marshaller(GooglePB.PBRequestType.getDefaultInstance());
GooglePB.PhoneNumber phoneNumber = GooglePB.PhoneNumber.newBuilder()
.setNumber("134123781291").build();
List<GooglePB.PhoneNumber> phoneNumberList = new ArrayList<>();
phoneNumberList.add(phoneNumber);
Map<String, GooglePB.PhoneNumber> phoneNumberMap = new HashMap<>();
phoneNumberMap.put("someUser", phoneNumber);
GooglePB.PBRequestType request = GooglePB.PBRequestType.newBuilder()
.setAge(15).setCash(10).setMoney(16.0).setNum(100L)
.addAllPhone(phoneNumberList).putAllDoubleMap(phoneNumberMap).build();
GooglePB.PBRequestType copyResult = paramDeepCopyUtil.copy(mockURL(), request, GooglePB.PBRequestType.class);
String jsonString = ProtobufUtils.serializeJson(request);
String jsonString2 = ProtobufUtils.serializeJson(copyResult);
assertEquals(jsonString, jsonString2);
assertNotEquals(System.identityHashCode(request), System.identityHashCode(copyResult));
List<GooglePB.PhoneNumber> copyPhoneList = copyResult.getPhoneList();
Map<String, GooglePB.PhoneNumber> copyDoubleMap = copyResult.getDoubleMap();
assertNotEquals(System.identityHashCode(phoneNumberList.get(0)), System.identityHashCode(copyPhoneList.get(0)));
assertNotEquals(System.identityHashCode(phoneNumberMap.get("someUser")), System.identityHashCode(copyDoubleMap.get("someUser")));
}

URL mockURL() {
URL url = new URL("dubbo", "localhost", 20880);
return url;
}
}
Loading