OData / odata.net

ODataLib: Open Data Protocol - .NET Libraries and Frameworks
https://docs.microsoft.com/odata
Other
686 stars 349 forks source link

Error messages for DataTimeOffset validation are not accurate #534

Open brjohnstmsft opened 8 years ago

brjohnstmsft commented 8 years ago

Since this related issue was fixed, the error messages for DateTimeOffset errors have regressed and are no longer accurate or helpful.

Assemblies affected

ODataLib 6.15.

Reproduce steps

These test cases show the problem:

using System;
using System.Collections.Generic;
using Microsoft.OData.Core;
using Microsoft.OData.Core.UriParser;
using Microsoft.OData.Edm;
using Microsoft.OData.Edm.Library;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace ODataFilterRepro
{
    [TestClass]
    public class FilterTests
    {
        [TestMethod]
        public void ParseFilterWithMissingTimeZoneExpectODataException()
        {
            ODataQueryOptionParser parser = CreateParser("createdOn gt 2015-12-29T00:00:00");
            ODataException e = AssertThrows<ODataException>(() => parser.ParseFilter());

            const string ExpectedMessage =
                "The time zone information is missing on the DateTimeOffset value '2013-01-01T12:00:00'. A DateTimeOffset value must contain the time zone information.";
            Assert.AreEqual(ExpectedMessage, e.Message);
        }

        [TestMethod]
        public void ParseFilterWithTimeZoneOutOfRangeExpectODataException()
        {
            ODataQueryOptionParser parser = CreateParser("createdOn gt 2015-12-29T00:00:00-25:00");
            ODataException e = AssertThrows<ODataException>(() => parser.ParseFilter());

            const string ExpectedMessage = "Offset must be within plus or minus 14 hours.";
            Assert.AreEqual(ExpectedMessage, e.Message);
        }

        public static T AssertThrows<T>(Action callback) where T : Exception
        {
            try
            {
                callback();
                Assert.Fail("Failed to throw expected exception of type {0}", typeof(T).Name);
            }
            catch (T e)
            {
                return e;
            }

            return null;
        }

        private static ODataQueryOptionParser CreateParser(string filter)
        {
            var targetType = new EdmEntityType("test", "document");
            targetType.AddStructuralProperty("createdOn", EdmPrimitiveTypeKind.DateTimeOffset);
            var keyProperty = targetType.AddStructuralProperty("key", EdmPrimitiveTypeKind.String);
            targetType.AddKeys(keyProperty);

            var model = new EdmModel();
            var container = new EdmEntityContainer("test", "default");
            model.AddElement(targetType);
            model.AddElement(container);

            var entitySet = new EdmEntitySet(model.EntityContainer, "docs", targetType);

            var options = new Dictionary<string, string>() { { "$filter", filter } };
            return new ODataQueryOptionParser(model, targetType, entitySet, options);
        }
    }
}

Expected result

The tests above would pass.

Actual result

The error message for both cases is "The DateTimeOffset text '2013-01-01T12:00:00' should be in format 'yyyy-mm-ddThh:mm:ss('.'s+)?(zzzzzz)?' and each field value is within valid range." The pattern given in the error message is incorrect, and the message is not as specific as the old error messages, making it less helpful.

LianwMS commented 8 years ago

@brjohnstmsft Thanks for report the issue. We will fix it ASAP and release it in 6.16. Thanks!

chetanmore0510 commented 2 years ago

@LianwMS : looks like this issue is still open even on the latest version of odata. can we have fix for this?

habbes commented 2 years ago

Added to backlog: 1848272