Skip to content
This repository has been archived by the owner on Oct 23, 2024. It is now read-only.

fix AfterFilter NullPointer exception in #3443 #3475

Merged
merged 2 commits into from
Oct 3, 2020
Merged

fix AfterFilter NullPointer exception in #3443 #3475

merged 2 commits into from
Oct 3, 2020

Conversation

echohlne
Copy link
Contributor

@echohlne echohlne commented Sep 29, 2020

I have a problem similar to #3443 and try to fix this.

fastjson version: 1.2.73(by the way, 1.7.20 works fine.)
reason:
When I create a JSONSerializer like JSONSerializer jsonSerializer = new JSONSerializer(serializeWriter, new SerializeConfig()); in my service, the class member:references will be null. Then if we custom a new AfterFilter class and try to create the json string. The AfterFilter class will throw a Nullpointer Exception like below.
exception stacktrace:

Exception in thread "main" com.alibaba.fastjson.JSONException: write javaBean error, fastjson version 1.2.73, class CustomAfterFilter$Parameter, write javaBean error, fastjson version 1.2.73, class CustomAfterFilter$ParameterDesc, fieldName : parameterDesc
	at com.alibaba.fastjson.serializer.JavaBeanSerializer.write(JavaBeanSerializer.java:539)
	at com.alibaba.fastjson.serializer.JavaBeanSerializer.write(JavaBeanSerializer.java:149)
	at com.alibaba.fastjson.serializer.JSONSerializer.write(JSONSerializer.java:312)
	at CustomAfterFilter.main(CustomAfterFilter.java:15)
Caused by: com.alibaba.fastjson.JSONException: write javaBean error, fastjson version 1.2.73, class CustomAfterFilter$ParameterDesc, fieldName : parameterDesc
	at com.alibaba.fastjson.serializer.JavaBeanSerializer.write(JavaBeanSerializer.java:539)
	at com.alibaba.fastjson.serializer.JavaBeanSerializer.write(JavaBeanSerializer.java:149)
	at com.alibaba.fastjson.serializer.FieldSerializer.writeValue(FieldSerializer.java:320)
	at com.alibaba.fastjson.serializer.JavaBeanSerializer.write(JavaBeanSerializer.java:470)
	... 3 more
Caused by: java.lang.NullPointerException
	at com.alibaba.fastjson.serializer.AfterFilter.writeKeyValue(AfterFilter.java:26)
	at CustomAfterFilter$CustomFilter.writeAfter(CustomAfterFilter.java:53)
	at com.alibaba.fastjson.serializer.AfterFilter.writeAfter(AfterFilter.java:17)
	at com.alibaba.fastjson.serializer.JavaBeanSerializer.writeAfter(JavaBeanSerializer.java:834)
	at com.alibaba.fastjson.serializer.JavaBeanSerializer.write(JavaBeanSerializer.java:502)
	... 6 more

Simplified reproduce code:

import com.alibaba.fastjson.serializer.*;

public class CustomAfterFilter {
    public static void main(String args[]) {
        SerializeWriter serializeWriter = new SerializeWriter();
        try {
            JSONSerializer jsonSerializer = new JSONSerializer(serializeWriter, new SerializeConfig());

            Parameter parameter = new Parameter();
            parameter.setParameterDesc(new ParameterDesc("vipExpireDate", "VIP expire date."));

            jsonSerializer.config(SerializerFeature.DisableCircularReferenceDetect, true);
            jsonSerializer.getAfterFilters().add(new CustomFilter());
            jsonSerializer.write(parameter);

            System.out.println(jsonSerializer.toString());
        } finally {
            serializeWriter.close();
        }
    }

    static class Parameter {
        private ParameterDesc parameterDesc;

        public ParameterDesc getParameterDesc() {
            return parameterDesc;
        }
        public void setParameterDesc(ParameterDesc parameterType) {
            this.parameterDesc = parameterType;
        }
    }

    static class ParameterDesc {
        private String parameterName;
        private String parameterUsage;
        // do some work...

        public ParameterDesc(String parameterName, String parameterUsage) {
            this.parameterName = parameterName;
            this.parameterUsage = parameterUsage;
        }
    }

    static class CustomFilter extends AfterFilter {
        @Override
        public void writeAfter(Object object) {
            if (object instanceof ParameterDesc) {
                writeKeyValue("ParameterDesc", "VIP expire date.");
            }
        }
    }
}

@CLAassistant
Copy link

CLAassistant commented Sep 29, 2020

CLA assistant check
All committers have signed the CLA.

@codecov-commenter
Copy link

codecov-commenter commented Sep 29, 2020

Codecov Report

Merging #3475 into master will decrease coverage by 0.01%.
The diff coverage is 50.00%.

Impacted file tree graph

@@             Coverage Diff              @@
##             master    #3475      +/-   ##
============================================
- Coverage     80.38%   80.37%   -0.02%     
+ Complexity     7472     7469       -3     
============================================
  Files           157      157              
  Lines         27912    27912              
  Branches       6731     6731              
============================================
- Hits          22438    22433       -5     
- Misses         3457     3460       +3     
- Partials       2017     2019       +2     
Impacted Files Coverage Δ Complexity Δ
...a/com/alibaba/fastjson/serializer/AfterFilter.java 94.73% <50.00%> (ø) 6.00 <0.00> (+1.00)
...com/alibaba/fastjson/serializer/SerialContext.java 82.69% <0.00%> (-3.85%) 21.00% <0.00%> (-1.00%)
...main/java/com/alibaba/fastjson/util/FieldInfo.java 85.95% <0.00%> (-1.01%) 96.00% <0.00%> (-3.00%)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 3ea25de...20e16be. Read the comment docs.

@echohlne echohlne changed the title fix AlterFilter NullPointer exception in #3443 fix AfterFilter NullPointer exception in #3443 Sep 29, 2020
@wenshao wenshao merged commit 4a9ca85 into alibaba:master Oct 3, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants