youxinren / snakeyaml

Automatically exported from code.google.com/p/snakeyaml
Apache License 2.0
0 stars 0 forks source link

JavaBeanDumper-Constructor is missing field initialization #65

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Using the constructor public JavaBeanDumper(Representer representer,
DumperOptions options) will not initialize the field classTags. There is
also no setter to initialize it.

Calling dump(Object data, Writer output) will result in a
NullPointerException at line 86:
for (Class<? extends Object> clazz : classTags) {

Looks like this constructor is missing something like: "this(false)"

Original issue reported on code.google.com by lerch.jo...@gmail.com on 26 Apr 2010 at 2:27

GoogleCodeExporter commented 9 years ago
classTags instance is created in line 49

Can you please provide a test with a failure ?

I did not quite catch your last statement.

Original comment by aso...@gmail.com on 26 Apr 2010 at 10:26

GoogleCodeExporter commented 9 years ago
Yes it is, but line 49 is not beeing executed if you instantiate by the 
constructor
public JavaBeanDumper(Representer representer, DumperOptions options) { in line 
59

To reproduce just instantiate JavaBeanDumper(null, new DumperOptions()) and 
call dump(..)

Original comment by lerch.jo...@gmail.com on 27 Apr 2010 at 6:32

GoogleCodeExporter commented 9 years ago
I suggest to change:

public JavaBeanDumper(Representer representer, DumperOptions options) {
        this.options = options;
        this.representer = representer;
    }

to:

public JavaBeanDumper(Representer representer, DumperOptions options) {
        this(false);
        this.options = options;
        this.representer = representer;
    }

Original comment by lerch.jo...@gmail.com on 27 Apr 2010 at 6:34

GoogleCodeExporter commented 9 years ago
I have added testDumpObject2 to
http://code.google.com/p/snakeyaml/source/browse/src/test/java/org/yaml/snakeyam
l/JavaB
eanDumperTest.java

It works properly. There is no failure.
Can you please provide a test ?

Original comment by py4fun@gmail.com on 27 Apr 2010 at 9:46

GoogleCodeExporter commented 9 years ago
Your test is not doing what i was writing.

Instantiate with null, not with new Representer().

In method dump(Object, Writer) is this if statement:

if (this.representer == null) {
            repr = new Representer();
            for (Class<? extends Object> clazz : classTags) {
                repr.addClassTag(clazz, Tag.MAP);
            }
        } else {
            repr = this.representer;
        }

This will only break, if this.representer is null and the foreach will iterate 
over
the not initialized value "classTags"

Original comment by lerch.jo...@gmail.com on 27 Apr 2010 at 9:59

GoogleCodeExporter commented 9 years ago
I got the point. The checks are added to display a proper message for a 'null' 
argument:
http://code.google.com/p/snakeyaml/source/detail?
r=191df57ec54b599e91071b356a670731cc8e84f5

Does it fix the issue ?

Original comment by py4fun@gmail.com on 27 Apr 2010 at 10:22

GoogleCodeExporter commented 9 years ago
Okay, this is also a way to fix it. Thanks.

Original comment by lerch.jo...@gmail.com on 27 Apr 2010 at 11:02

GoogleCodeExporter commented 9 years ago
It will be delivered in version 1.7

Original comment by aso...@gmail.com on 28 Apr 2010 at 7:32