Skip to content

Commit

Permalink
Fixes for Csdl.GetNamespaceAlias extension method. (#1586)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
syprieur authored and mikepizzo committed Nov 1, 2019
1 parent e08a361 commit 1e094dc
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -270,8 +270,15 @@ public static void SetNamespaceAlias(this IEdmModel model, string namespaceName,
/// <returns>The alias of the given namespace, or null if one does not exist.</returns>
public static string GetNamespaceAlias(this IEdmModel model, string namespaceName)
{
EdmUtil.CheckArgumentNull(model, "model");
VersioningDictionary<string, string> mappings = model.GetAnnotationValue<VersioningDictionary<string, string>>(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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 1e094dc

Please sign in to comment.