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

修改请求参数转换逻辑 #1347

Closed

Conversation

miaomiaoLmm
Copy link

@miaomiaoLmm miaomiaoLmm commented Jul 17, 2023

修改请求参数转换逻辑:
问题:rpc请求序列化异常,rpc请求进行request参数序列化,由于子类没有重写apply方法,导致从父类获取apply方法中的request和response类型,由于类型定义为范型,request参数序列化过程会自动转换为linkedHashMap类型并请求apply方法,由于apply方法中调用的process方法确定了实际的request类型,导致linkedHashMap转换Request类型异常

interface A<T,R>{
  R apply(T t);
}

public abstract class B<T,R> implements A<T,R>{
   @Override
    public R apply(T request) {
        return this.process(request);
    }

protected abstract R process(T request);
}      

class C extends B<Request,Response>{
    @Override
    protected Response process(Request request) {
        logger.info("Hello FaaS !");
        Response resp = new Response();
        resp.setUrl(request.getUrl());
        return resp;
    }

}

}

解决方案:判断重写apply方法的class类是否为目标类,如果不是目标类,通过获取目标类上确定的Request和Response类型进行请求参数序列化。

@sofastack-cla
Copy link

sofastack-cla bot commented Jul 17, 2023

Hi @miaomiaoLmm, welcome to SOFAStack community, Please sign Contributor License Agreement!

After you signed CLA, we will automatically sync the status of this pull request in 3 minutes.

@sofastack-cla sofastack-cla bot added cla:no Need sign CLA First-time contributor First-time contributor size/S labels Jul 17, 2023
@miaomiaoLmm miaomiaoLmm reopened this Jul 17, 2023
@sofastack-cla
Copy link

sofastack-cla bot commented Jul 17, 2023

Hi @miaomiaoLmm, welcome to SOFAStack community, Please sign Contributor License Agreement!

After you signed CLA, we will automatically sync the status of this pull request in 3 minutes.

@miaomiaoLmm miaomiaoLmm marked this pull request as draft July 17, 2023 08:03
@sofastack-cla sofastack-cla bot added cla:yes CLA is ok and removed cla:no Need sign CLA labels Jul 17, 2023
@codecov
Copy link

codecov bot commented Jul 20, 2023

Codecov Report

Merging #1347 (d4489eb) into master (8ca4f28) will decrease coverage by 0.01%.
Report is 4 commits behind head on master.
The diff coverage is 88.88%.

❗ Current head d4489eb differs from pull request most recent head c37390e. Consider uploading reports for the commit c37390e to get more accurate results

@@             Coverage Diff              @@
##             master    #1347      +/-   ##
============================================
- Coverage     72.05%   72.05%   -0.01%     
- Complexity      783      784       +1     
============================================
  Files           416      416              
  Lines         17661    17668       +7     
  Branches       2752     2753       +1     
============================================
+ Hits          12726    12730       +4     
  Misses         3533     3533              
- Partials       1402     1405       +3     
Files Changed Coverage Δ
...m/alipay/sofa/rpc/codec/jackson/JacksonHelper.java 84.78% <88.88%> (+0.16%) ⬆️

... and 2 files with indirect coverage changes

@EvenLjj EvenLjj marked this pull request as ready for review July 20, 2023 16:13
if (jsonMethod == null) {
throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_METHOD_NOT_FOUND, clazz.getName(),
methodName));
}

// parse request types
Type[] parameterTypes = jsonMethod.getGenericParameterTypes();
Type[] parameterTypes;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@miaomiaoLmm Please add a single test to ensure problem resolution.

@sofastack-cla sofastack-cla bot added size/L and removed size/S labels Jul 27, 2023
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.*;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't use * in import statements. You can set it in IDE configuration.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New pull request I removed util.*

@EvenLjj EvenLjj added this to the 5.11.0 milestone Jul 28, 2023
@Lo1nt
Copy link
Collaborator

Lo1nt commented Aug 1, 2023

pls do a 'mvn clean package -Dmaven.test.skip=true' before u commit and push

if (jsonMethod.getDeclaringClass() != clazz && !clazz.isInterface()) {
parameterTypes = new Type[1];
Type type = clazz.getGenericSuperclass();
ParameterizedType parameterizedType = (ParameterizedType) type;
Copy link
Collaborator

@Lo1nt Lo1nt Aug 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if clazz.getGenericSuperclass() returns type which is not ParameterizedType?

Say if Class c extends B implement A, B may not always be a ParameterizedType.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new pull request fixes the parent object non-ParameterizedType conversion problem

淼淼 added 3 commits August 4, 2023 15:59
… father object/super object definition apply method, and definition requestType and responseType, type change fixed success

2. The TestRequest2 method is provided to test the problem of parent class non-ParameterizedType conversion
… father object/super object definition apply method, and definition requestType and responseType, type change fixed success

2. The TestRequest2 method is provided to test the problem of parent class non-ParameterizedType conversion
… father object/super object definition apply method, and definition requestType and responseType, type change fixed success

2. The TestRequest2 method is provided to test the problem of parent class non-ParameterizedType conversion
try {
ParameterizedType parameterizedType = (ParameterizedType) type;
// parse request types
parameterTypes[0] = parameterizedType.getActualTypeArguments()[0];
Copy link
Collaborator

@Lo1nt Lo1nt Aug 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里感觉还是有问题的,应该无法保证所有走到这里的case都是 Class A<X, Y> 的形式? Class A<X, Y, Z> 或者 Class A<X> 应该都会触发异常

if (jsonMethod.getDeclaringClass() != clazz && !clazz.isInterface()) {
parameterTypes = new Type[1];
Type type = clazz.getGenericSuperclass();
try {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里可以用if else来处理,尽量不要使用 try catch 当 ifelse 用

@stale
Copy link

stale bot commented Oct 9, 2023

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix This will not be worked on label Oct 9, 2023
@stale stale bot closed this Oct 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla:yes CLA is ok First-time contributor First-time contributor size/L wontfix This will not be worked on
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants