marcusaram / snakeyaml

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

IllegalArgumentException thrown when attempting to set BigDecimal property #40

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Create a bean instance that has a BigDecimal property.
2. Set the BigDecimal property to a non-decimal value, i.e. "5".
3. Create a default Yaml and dump the bean to a String.
4. Attempt to load a new bean instance from the String.

What is the expected output? What do you see instead?

Yaml should return a new instance of a Bean that has an identical
BigDecimal value on the set property.

Instead this exception is thrown:

Can't construct a java object for
tag:yaml.org,2002:<*removed*>.DogFoodBean; exception=Cannot create
property=decimal for JavaBean=<*removed*>.DogFoodBean@c3c749; argument type
mismatch
 in "<reader>", line 0, column 0

    at
org.yaml.snakeyaml.constructor.Constructor$ConstructYamlObject.construct(Constru
ctor.java:295)
    at
org.yaml.snakeyaml.constructor.BaseConstructor.callConstructor(BaseConstructor.j
ava:187)
    at
org.yaml.snakeyaml.constructor.BaseConstructor.constructObject(BaseConstructor.j
ava:170)
    at
org.yaml.snakeyaml.constructor.BaseConstructor.constructDocument(BaseConstructor
.java:129)
    at
org.yaml.snakeyaml.constructor.BaseConstructor.getSingleData(BaseConstructor.jav
a:115)
    at org.yaml.snakeyaml.Loader.load(Loader.java:51)
    at org.yaml.snakeyaml.Yaml.load(Yaml.java:152)
    at <*removed*>.DogFoodTest.testOwnBigDecimal(DogFoodTest.java:20)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.jav
a:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:59)
    at org.junit.internal.runners.MethodRoadie.runTestMethod(MethodRoadie.java:98)
    at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:79)
    at
org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadi
e.java:87)
    at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:77)
    at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:42)
    at
org.junit.internal.runners.JUnit4ClassRunner.invokeTestMethod(JUnit4ClassRunner.
java:88)
    at
org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:5
1)
    at
org.junit.internal.runners.JUnit4ClassRunner$1.run(JUnit4ClassRunner.java:44)
    at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:27)
    at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:37)
    at org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:42)
    at
org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReferen
ce.java:45)
    at
org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner
.java:460)
    at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner
.java:673)
    at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java
:386)
    at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.jav
a:196)
Caused by: org.yaml.snakeyaml.error.YAMLException: Cannot create
property=decimal for JavaBean=<*removed*>.DogFoodBean@c3c749; argument type
mismatch
    at
org.yaml.snakeyaml.constructor.Constructor$ConstructMapping.constructJavaBean2nd
Step(Constructor.java:239)
    at
org.yaml.snakeyaml.constructor.Constructor$ConstructMapping.construct(Constructo
r.java:150)
    at
org.yaml.snakeyaml.constructor.Constructor$ConstructYamlObject.construct(Constru
ctor.java:293)
    ... 29 more
Caused by: java.lang.IllegalArgumentException: argument type mismatch
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.jav
a:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.yaml.snakeyaml.introspector.MethodProperty.set(MethodProperty.java:32)
    at
org.yaml.snakeyaml.constructor.Constructor$ConstructMapping.constructJavaBean2nd
Step(Constructor.java:237)
    ... 31 more

What version of the product are you using? On what operating system?

Version 1.4, Windows XP Professional, Java 1.6

Please provide any additional information below.

Test Code:

import java.math.BigDecimal;

//SnakeYaml should be able to eat its own dog-food (load its own output).
public class DogFoodBean{

    BigDecimal decimal;

    public DogFoodBean(){
        decimal = BigDecimal.ZERO;
    }

    public BigDecimal getDecimal(){
        return decimal;
    }

    public void setDecimal(BigDecimal decimal){
        this.decimal = decimal;
    }   

}

import static org.junit.Assert.assertEquals;

import java.math.BigDecimal;

import org.junit.Test;
import org.yaml.snakeyaml.Yaml;

public class DogFoodTest {

    @Test
    public void testOwnBigDecimal(){
        DogFoodBean input = new DogFoodBean();
        input.setDecimal(new BigDecimal("5"));      
        Yaml yaml = new Yaml();
        String text = yaml.dump(input);
        DogFoodBean output = (DogFoodBean) yaml.load(text);
        assertEquals(output.getDecimal(), input.getDecimal());
    }

}

Original issue reported on code.google.com by sitri...@gmail.com on 31 Dec 2009 at 7:08

GoogleCodeExporter commented 9 years ago
Thank you for the very clear report.

Original comment by aso...@gmail.com on 31 Dec 2009 at 9:45

GoogleCodeExporter commented 9 years ago
If you need urgently the fix please take the latest source.
This is the test:
http://code.google.com/p/snakeyaml/source/browse/src/test/java/org/yaml/snakeyam
l/issues/issue40/DogFoodBeanTest.java?spec=svnbd994466ca570d4a1461c39407ac098c1a
08d260&r=bd994466ca570d4a1461c39407ac098c1a08d260

Since I am not happy with the implementation I would like to refactor the 
source to
fix the issue properly. It may take more time.

Original comment by aso...@gmail.com on 2 Jan 2010 at 2:23

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
Fixed.
(Ignore tags when they are compatible with the JavaBean runtime class)

Now the tag !!float is ignored in favor of the BigDecimal runtime class:

!!org.yaml.snakeyaml.issues.issue40.DogFoodBean {decimal: !!float '5'}

Original comment by aso...@gmail.com on 4 Jan 2010 at 12:13