Uploaded image for project: 'Spring Web Services'
  1. Spring Web Services
  2. SWS-640

evaluateAsBoolean returns true for node with text content "false"

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 1.5.8
    • Fix Version/s: 2.0 RC1
    • Component/s: XML
    • Labels:
      None

      Description

      The method Jaxp13XPathTemplate.evaluateAsBoolean returns true if the element selected by the expression exists, regardless of its contents. The text contents can be empty, "false", or "WTF", and evaluateAsBoolean will return true.

      This does not appear to match its Javadoc, which says: "Evaluates the given expression as a boolean. Returns the boolean evaluation of the expression, or false if it is invalid." The very similar method evaluateAsDouble has nearly identical Javadoc: "Evaluates the given expression as a double. Returns the evaluation of the expression, or Double.NaN if it is invalid." - but evaluateAsDouble returns the actual contents of the text node. If evaluateAsBoolean is really meant to mean, "doesExpressionSelectAnything", it should be renamed.

      Below is a unit test that demonstrates the problem. The second assert fails.

      The problem also occurs when using a WSDL which defines aFlag as type "xs:boolean". And, it also occurs with JaxenXPathTemplate. However, I have not seen it using sources other than DOMSource.

      package com.yodle.misc;
       
      import static org.junit.Assert.*;
       
      import javax.xml.transform.dom.DOMSource;
       
      import org.junit.Test;
      import org.springframework.ws.server.endpoint.AbstractDomPayloadEndpoint;
      import org.springframework.xml.transform.StringSource;
      import org.springframework.xml.xpath.Jaxp13XPathTemplate;
      import org.w3c.dom.Document;
      import org.w3c.dom.Element;
       
      public class BugDemo {
       
      	final String request =
      		"<someRequest> " +		
      		"	<aDouble>123</aDouble> " +
      		"	<aFlag>false</aFlag> " +
      		"</someRequest> ";
      	
      	private class Endpoint extends AbstractDomPayloadEndpoint {
       
      		@Override
      		protected Element invokeInternal(Element requestElement, Document responseDocument) throws Exception {
      			Jaxp13XPathTemplate xPathTemplate = new Jaxp13XPathTemplate();
      			DOMSource src = new DOMSource(requestElement);
      			
      			double aDouble = xPathTemplate.evaluateAsDouble("aDouble", src);
      			assertEquals(123d, aDouble, 0.01);
      			
      			boolean aFlag = xPathTemplate.evaluateAsBoolean("aFlag", src);
      			assertFalse(aFlag);
      			
      			return null;
      		}
      		
      	};
      	
      	@Test
      	public void demonstrateEvaluateAsBooleanStrangeBehavior() throws Exception {
      		Endpoint endPoint = new Endpoint();
      		endPoint.invoke(new StringSource(request));
      	}
      	
      }

        Activity

        djudd David Judd created issue -
        arjen.poutsma Arjen Poutsma made changes -
        Field Original Value New Value
        Fix Version/s 1.5.10 [ 11497 ]
        Fix Version/s 2.0 M4 [ 11626 ]
        arjen.poutsma Arjen Poutsma made changes -
        Description The method Jaxp13XPathTemplate.evaluateAsBoolean returns true if the element selected by the expression exists, regardless of its contents. The text contents can be empty, "false", or "WTF", and evaluateAsBoolean will return true.

        This does not appear to match its Javadoc, which says: "Evaluates the given expression as a boolean. Returns the boolean evaluation of the expression, or false if it is invalid." The very similar method evaluateAsDouble has nearly identical Javadoc: "Evaluates the given expression as a double. Returns the evaluation of the expression, or Double.NaN if it is invalid." - but evaluateAsDouble returns the actual contents of the text node. If evaluateAsBoolean is really meant to mean, "doesExpressionSelectAnything", it should be renamed.

        Below is a unit test that demonstrates the problem. The second assert fails.

        The problem also occurs when using a WSDL which defines aFlag as type "xs:boolean". And, it also occurs with JaxenXPathTemplate. However, I have not seen it using sources other than DOMSource.

        package com.yodle.misc;

        import static org.junit.Assert.*;

        import javax.xml.transform.dom.DOMSource;

        import org.junit.Test;
        import org.springframework.ws.server.endpoint.AbstractDomPayloadEndpoint;
        import org.springframework.xml.transform.StringSource;
        import org.springframework.xml.xpath.Jaxp13XPathTemplate;
        import org.w3c.dom.Document;
        import org.w3c.dom.Element;

        public class BugDemo {

        final String request =
        "<someRequest> " +
        " <aDouble>123</aDouble> " +
        " <aFlag>false</aFlag> " +
        "</someRequest> ";

        private class Endpoint extends AbstractDomPayloadEndpoint {

        @Override
        protected Element invokeInternal(Element requestElement, Document responseDocument) throws Exception {
        Jaxp13XPathTemplate xPathTemplate = new Jaxp13XPathTemplate();
        DOMSource src = new DOMSource(requestElement);

        double aDouble = xPathTemplate.evaluateAsDouble("aDouble", src);
        assertEquals(123d, aDouble, 0.01);

        boolean aFlag = xPathTemplate.evaluateAsBoolean("aFlag", src);
        assertFalse(aFlag);

        return null;
        }

        };

        @Test
        public void demonstrateEvaluateAsBooleanStrangeBehavior() throws Exception {
        Endpoint endPoint = new Endpoint();
        endPoint.invoke(new StringSource(request));
        }

        }
        The method Jaxp13XPathTemplate.evaluateAsBoolean returns true if the element selected by the expression exists, regardless of its contents. The text contents can be empty, "false", or "WTF", and evaluateAsBoolean will return true.

        This does not appear to match its Javadoc, which says: "Evaluates the given expression as a boolean. Returns the boolean evaluation of the expression, or false if it is invalid." The very similar method evaluateAsDouble has nearly identical Javadoc: "Evaluates the given expression as a double. Returns the evaluation of the expression, or Double.NaN if it is invalid." - but evaluateAsDouble returns the actual contents of the text node. If evaluateAsBoolean is really meant to mean, "doesExpressionSelectAnything", it should be renamed.

        Below is a unit test that demonstrates the problem. The second assert fails.

        The problem also occurs when using a WSDL which defines aFlag as type "xs:boolean". And, it also occurs with JaxenXPathTemplate. However, I have not seen it using sources other than DOMSource.

        {code}
        package com.yodle.misc;

        import static org.junit.Assert.*;

        import javax.xml.transform.dom.DOMSource;

        import org.junit.Test;
        import org.springframework.ws.server.endpoint.AbstractDomPayloadEndpoint;
        import org.springframework.xml.transform.StringSource;
        import org.springframework.xml.xpath.Jaxp13XPathTemplate;
        import org.w3c.dom.Document;
        import org.w3c.dom.Element;

        public class BugDemo {

        final String request =
        "<someRequest> " +
        " <aDouble>123</aDouble> " +
        " <aFlag>false</aFlag> " +
        "</someRequest> ";

        private class Endpoint extends AbstractDomPayloadEndpoint {

        @Override
        protected Element invokeInternal(Element requestElement, Document responseDocument) throws Exception {
        Jaxp13XPathTemplate xPathTemplate = new Jaxp13XPathTemplate();
        DOMSource src = new DOMSource(requestElement);

        double aDouble = xPathTemplate.evaluateAsDouble("aDouble", src);
        assertEquals(123d, aDouble, 0.01);

        boolean aFlag = xPathTemplate.evaluateAsBoolean("aFlag", src);
        assertFalse(aFlag);

        return null;
        }

        };

        @Test
        public void demonstrateEvaluateAsBooleanStrangeBehavior() throws Exception {
        Endpoint endPoint = new Endpoint();
        endPoint.invoke(new StringSource(request));
        }

        }
        {code}
        arjen.poutsma Arjen Poutsma made changes -
        Status Open [ 1 ] In Progress [ 3 ]
        arjen.poutsma Arjen Poutsma made changes -
        Status In Progress [ 3 ] Resolved [ 5 ]
        Resolution Fixed [ 1 ]
        arjen.poutsma Arjen Poutsma made changes -
        Time Spent 2d 4h 29m [ 73740 ]
        Remaining Estimate 0d [ 0 ]
        arjen.poutsma Arjen Poutsma made changes -
        Fix Version/s 2.0 M4 [ 11626 ]
        Fix Version/s 2.0 RC1 [ 11392 ]
        arjen.poutsma Arjen Poutsma made changes -
        Fix Version/s 1.5.10 [ 11497 ]
        arjen.poutsma Arjen Poutsma made changes -
        Status Resolved [ 5 ] Closed [ 6 ]

          People

          • Assignee:
            arjen.poutsma Arjen Poutsma
            Reporter:
            djudd David Judd
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Time Tracking

              Estimated:
              Original Estimate - Not Specified
              Not Specified
              Remaining:
              Remaining Estimate - 0d
              0d
              Logged:
              Time Spent - 2d 4h 29m
              2d 4h 29m