From 1e094dcfcabf81d9f668cbc6666945618b1e9023 Mon Sep 17 00:00:00 2001 From: Sylvain Prieur Date: Fri, 1 Nov 2019 13:40:17 -0700 Subject: [PATCH] Fixes for Csdl.GetNamespaceAlias extension method. (#1586) * Fixes for Csdl.GetNamespaceAlias extension method. - GetNamespace's documentation advertises returning null if the namespace doesn't have an alias. However, it throws KeyNotFoundException instead, as it's calling VersioningDictionary.Get. - GetNamespace crashes with NullReferenceException if the CSDL doesn't define any namespace alias. A null check for GetAnnotationValue's return value is missing. * Adjusted for older .NET versions. * Unit tests * Positive test case for GetNamespaceAlias --- .../Csdl/SerializationExtensionMethods.cs | 9 ++++++++- .../ExtensionMethods/ExtensionMethodTests.cs | 20 +++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.OData.Edm/Csdl/SerializationExtensionMethods.cs b/src/Microsoft.OData.Edm/Csdl/SerializationExtensionMethods.cs index 0c5ba2c453..ab7411361a 100644 --- a/src/Microsoft.OData.Edm/Csdl/SerializationExtensionMethods.cs +++ b/src/Microsoft.OData.Edm/Csdl/SerializationExtensionMethods.cs @@ -270,8 +270,15 @@ public static void SetNamespaceAlias(this IEdmModel model, string namespaceName, /// The alias of the given namespace, or null if one does not exist. public static string GetNamespaceAlias(this IEdmModel model, string namespaceName) { + EdmUtil.CheckArgumentNull(model, "model"); VersioningDictionary mappings = model.GetAnnotationValue>(model, EdmConstants.InternalUri, CsdlConstants.NamespaceAliasAnnotation); - return mappings.Get(namespaceName); + string namespaceAlias; + if (mappings != null && mappings.TryGetValue(namespaceName, out namespaceAlias)) + { + return namespaceAlias; + } + + return null; } // This internal method exists so we can get a consistent view of the mappings through the entire serialization process. diff --git a/test/FunctionalTests/Microsoft.OData.Edm.Tests/ExtensionMethods/ExtensionMethodTests.cs b/test/FunctionalTests/Microsoft.OData.Edm.Tests/ExtensionMethods/ExtensionMethodTests.cs index 2d2cd602fc..dd32c6c742 100644 --- a/test/FunctionalTests/Microsoft.OData.Edm.Tests/ExtensionMethods/ExtensionMethodTests.cs +++ b/test/FunctionalTests/Microsoft.OData.Edm.Tests/ExtensionMethods/ExtensionMethodTests.cs @@ -937,6 +937,26 @@ public void FindTypeForUndefinedTypeDoesnotGetIntoInfiniteSearchLoop() Assert.Null(unknownType); } + [Fact] + public void GetNamespaceAliasReturnsNullForNamespaceWithoutAlias() + { + Assert.Null(TestModel.Instance.Model.GetNamespaceAlias("SomeNamespace.NotIn.Model")); + } + + [Fact] + public void GetNamespaceAliasReturnsNullForModelsWithoutAliases() + { + EdmModel model = new EdmModel(false); + Assert.Null(model.GetNamespaceAlias("SomeNamespace")); + } + + [Fact] + public void GetNamespaceAliasForNamespaceWithAlias() + { + Assert.Equal(TestModel.TestModelAlias, TestModel.Instance.Model.GetNamespaceAlias(TestModel.TestModelNameSpace)); + Assert.Equal(TestModel.TestModelAlias2, TestModel.Instance.Model.GetNamespaceAlias(TestModel.TestModelNameSpace2)); + } + internal class TestModel { public static TestModel Instance = new TestModel();