users@jaxb.java.net

Bad enum value in the input XML does not throw exception, assigns null

From: Shashi Velur <vshashi_at_gmail.com>
Date: Fri, 2 Oct 2009 09:15:04 -0700 (PDT)

Hello:
 
I am looking for some help on enforcing the type-safety of the enumeration
values passed-in from the XML.
 
The current behavior I am seeing with JAXB RI ver. 2.1.9 is that the invalid
enumerations in XML are mapped to null values in the generated Java classes.
I expected to see an exception thrown whenever
Unmarshaller.unmarshal(xmlStreamReader, clazz) method is invoked with an XML
that has an invalid enum value.
 
I looked through the JAXB RI source while debugging and found that the
following method in
com.sun.xml.bind.v2.model.impl.RuntimeEnumLeafInfoImpl.java is returning a
null whenever the map (with all valid enum Strings) is looked-up using the
given enum value (in my case an invalid enum value):

   public T parse(CharSequence lexical) throws AccessorException,
SAXException {
       // TODO: error handling
 
       B b = baseXducer.parse(lexical);
       if(b==null) {
 
           return null;
       }
 
       return parseMap.get(b);
   }

 
This seems to be called from
com.sun.xml.bind.v2.runtime.reflect.TransducedAccessor.CompositeTransducedAccessorImpl
class. The following code snippet simply sets null to the Java Object (of
the generated JAXB Java class) whenever the previous method call is made
with an invalid enumeration.

       public void parse(BeanT bean, CharSequence lexical) throws
AccessorException, SAXException {
           acc.set(bean,xducer.parse(lexical));
       }

 
Is this the expected behavior?
I expect the above method to check for the null return value of
xducer.parse(lexical) and throw an exception as the passed-in value does not
match any of the given enum values.
 
I have used the following XML annotation for type-safety:


   
       
           
       
   
   
       
           
               
                   
               
           
       
       
           
               
                   
               
           
       
       
           
               
                   
               
           
       
       
           
               
                   
               
           
       
   


 
Any help is much appreciated. Thank you.
 
Cheers,
Shashi
 
 
Twitter: @shashivelur
Blog: http://www.shashivelur.com.blog
LinkedIn: http://www.linkedin.com/in/shashivelur
-- 
View this message in context: http://www.nabble.com/Bad-enum-value-in-the-input-XML-does-not-throw-exception%2C-assigns-null-tp25716726p25716726.html
Sent from the java.net - jaxb users mailing list archive at Nabble.com.