tommysiu / beanio

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

BeanReaderContext does not take into account bean nesting #37

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?  If applicable, please provide a
mapping configuration and sample record input to recreate the problem.
1. Create a mapping with a nested bean
2. Try to read a file with a field error on one of the nested bean fields
3. The field errors returned do not take into account the bean nesting. 

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

Given a record with a bean inside such as:
<record name="bigBean" class="example.BigBean">
  <bean name="myBean" class="example.MyBean">
    <field name="num" type="integer" />
  </bean>
  <bean name="yourBean" class="example.MyBean">
    <field name="num" type="integer" />
  </bean>
</record>

If there is a field error on 'num' I would expect field errors to be created 
under the key "myBean.num" or "yourBean.num", giving the full property path. 
Instead, we are seeing field errors created under the key "num". 

What version of BeanIO are you using?
BeanIO 1.2.3

Please provide any additional information below.

This is a problem because our error handler needs to know the full path to the 
invalid field in order to generate the correct error message. Also, the 
fieldErrorMap within the BeanReaderContext maintains a map of field names to 
values. In the example above, this map might look like "num" -> "5" So when 
interpolating field values into error messages you might get the wrong value 
(ie, if one field was "A" and one field was "B" the two field errors generated 
would interpolate the same field value because the fieldErrorMap only has one 
entry for "num" rather than having two entries, such as "myBean.num" -> "A", 
"yourBean.num" -> "B"). I don't know if this is resolved in BeanIO 2.0.

Original issue reported on code.google.com by HiJo...@gmail.com on 7 Jun 2012 at 4:30

GoogleCodeExporter commented 8 years ago
Confirmed the behavior is the same in 2.0.

Original comment by HiJo...@gmail.com on 7 Jun 2012 at 5:19

GoogleCodeExporter commented 8 years ago
I agree there is definitely some room for improvement here, and will think 
about a better long term solution.

For now, if you have any duplicate field names, you can change them to 
something unique, and then use getter and setter attributes.  For example:

<record name="bigBean" class="example.BigBean">
  <bean name="myBean" class="example.MyBean">
    <field name="num" type="integer" />
  </bean>
  <bean name="yourBean" class="example.MyBean">
    <field name="num2" getter="getNum" setter="setNum" type="integer" />
  </bean>
</record>

Hope that helps.

Thanks,
Kevin

Original comment by kevin.s...@gmail.com on 7 Jun 2012 at 7:33

GoogleCodeExporter commented 8 years ago
We're doing something similar to that for now, but unfortunately you can't use 
templates with this approach so our BeanIO mappings have a lot more duplication 
now.

Original comment by HiJo...@gmail.com on 7 Jun 2012 at 7:59

GoogleCodeExporter commented 8 years ago
Good point.  I'm working on a couple other things right now, but will make 
improved error reporting my next priority for a 2.x release.  Sorry this won't 
help you in the near term.

Original comment by kevin.s...@gmail.com on 8 Jun 2012 at 1:57