EDMX returned from $metadata endpoint has incorrect "Unicode" attributes
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.
@brjohnstmsft I made a fix. Would you please try it out. Thanks.
Thanks! I'll try it out with the next nightly build.
I've confirmed that this works with the latest nightly build. Thanks again!
You are welcome give us more feedback. Thanks.
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.