WebApi icon indicating copy to clipboard operation
WebApi copied to clipboard

EDMX returned from $metadata endpoint has incorrect "Unicode" attributes

Open bitsforthought opened this issue 10 years ago • 5 comments

When building a model using ODataModelBuilder, action/function parameters of type Edm.String have their type references constructed improperly, which results in spurious and incorrect "Unicode" attributes in the serialized XML for the EDM model. According to the CSDL spec, the default value of the Unicode attribute should be true, but it is set to false in the serialized EDM model.

Assemblies affected

Microsoft.AspNet.OData 5.9.0

Reproduce steps

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.OData.Builder;
using System.Xml;
using Microsoft.OData.Edm;
using Microsoft.OData.Edm.Csdl;
using Microsoft.OData.Edm.Validation;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace ODataUnicodeRepro
{
    [TestClass]
    public class ODataUnicodeTest
    {
        [TestMethod]
        public void ModelShouldNotContainUnicodeAttribute()
        {
            var builder = new ODataModelBuilder();
            builder.Action("doSomething").Parameter<string>("param1");

            IEdmModel model = builder.GetEdmModel();
            string serializedModel = SerializeEdmModelToXml(model);

            Assert.IsFalse(
                serializedModel.Contains("Unicode"),
                "Expected no instance of 'Unicode' in the serialized EDM model. Actual:\n{0}",
                serializedModel);
        }

        private static string SerializeEdmModelToXml(IEdmModel model)
        {
            var stringBuilder = new StringBuilder();
            using (XmlWriter writer = XmlWriter.Create(stringBuilder))
            {
                IEnumerable<EdmError> validationErrors;
                if (!EdmxWriter.TryWriteEdmx(model, writer, EdmxTarget.OData, out validationErrors))
                {
                    Assert.Inconclusive(
                        "Test bug: Failed to serialize EDM model. Errors: {0}",
                        validationErrors.Select(e => e.ErrorMessage));
                }
            }

            return stringBuilder.ToString();
        }
    }
}

Expected result

The Parameter element in the EDMX either would not contain the Unicode attribute, or it would be set to true. Preferably the Unicode attribute would not even be present.

Actual result

There is a Unicode attribute, and it's set to false.

Additional details

The root cause is that EdmModelHelperMethods.GetEdmTypeReference is creating a EdmPrimitiveTypeReference instead of a EdmStringTypeReference. When calling into EDMLib to serialize the model, EdmModelVisitor.VisitPrimitiveTypeReference calls AsString on the type reference. Since it is not an EdmStringTypeReference, this results in the creation of a BadStringTypeReference, which sets the Unicode property to false.

bitsforthought avatar May 19 '16 01:05 bitsforthought

@brjohnstmsft I made a fix. Would you please try it out. Thanks.

xuzhg avatar May 19 '16 05:05 xuzhg

Thanks! I'll try it out with the next nightly build.

bitsforthought avatar May 19 '16 18:05 bitsforthought

I've confirmed that this works with the latest nightly build. Thanks again!

bitsforthought avatar May 20 '16 22:05 bitsforthought

You are welcome give us more feedback. Thanks.

xuzhg avatar May 23 '16 03:05 xuzhg

FYI @xuzhg -- This fix was never applied in 6.0. We're seeing the regression now that we're upgrading Azure Search to the latest version. I've submitted a PR to port the fix over to 7.x.

bitsforthought avatar Aug 25 '18 00:08 bitsforthought