Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

property as annotation target #2238

Draft
wants to merge 4 commits into
base: release-7.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@
// </copyright>
//---------------------------------------------------------------------

using Microsoft.OData.Edm.Vocabularies;
using System.Collections.Generic;

namespace Microsoft.OData.Edm.Csdl.Parsing.Ast
{

/// <summary>
/// Represents a CSDL navigation property Path expression.
/// </summary>
internal class CsdlNavigationPropertyPathExpression : CsdlPathExpression
internal class CsdlNavigationPropertyPathExpression : CsdlPathExpression, IEdmNavigationPropertyPath
{
public CsdlNavigationPropertyPathExpression(string path, CsdlLocation location)
: base(path, location)
Expand All @@ -20,5 +24,7 @@ public override EdmExpressionKind ExpressionKind
{
get { return EdmExpressionKind.NavigationPropertyPath; }
}

IEnumerable<string> IEdmPathExpression.PathSegments => throw new System.NotImplementedException();
}
}
5 changes: 5 additions & 0 deletions src/Microsoft.OData.Edm/EdmUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,7 @@ internal static string FullyQualifiedName(IEdmVocabularyAnnotatable element)
{
IEdmOperationReturn operationReturn;
IEdmEnumMember enumMember;
IEdmNavigationPropertyPath edmNavigationPropertyPath;
IEdmOperationParameter parameter = element as IEdmOperationParameter;
if (parameter != null)
{
Expand All @@ -485,6 +486,10 @@ internal static string FullyQualifiedName(IEdmVocabularyAnnotatable element)
return operationName + "/" + CsdlConstants.OperationReturnExternalTarget;
}
}
else if((edmNavigationPropertyPath = element as IEdmNavigationPropertyPath) != null)
{
return edmNavigationPropertyPath.Path;
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// </copyright>
//---------------------------------------------------------------------

using Microsoft.OData.Edm.Vocabularies;
using System.Collections.Generic;

namespace Microsoft.OData.Edm
Expand All @@ -23,4 +24,11 @@ public interface IEdmPathExpression : IEdmExpression
/// </summary>
string Path { get; }
}



[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1040:Avoid empty interfaces", Justification = "marker interface")]
public interface IEdmNavigationPropertyPath : IEdmPathExpression, IEdmVocabularyAnnotatable
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Microsoft.OData.Edm.Vocabularies
/// <summary>
/// Represents an EDM navigation property path expression.
/// </summary>
public class EdmNavigationPropertyPathExpression : EdmPathExpression
public class EdmNavigationPropertyPathExpression : EdmPathExpression, IEdmNavigationPropertyPath
{
/// <summary>
/// Initializes a new instance of the <see cref="EdmNavigationPropertyPathExpression"/> class.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2685,6 +2685,47 @@ public void ValidateEdmxVersions(string odataVersion)
WriteAndVerifyJson(edmModel, "{\"$Version\":\"" + odataVersion + "\"}", false);
}


[Fact]
public void CanWriteNavigationPropertyPathAnnotation()
{
EdmModel model = new EdmModel();
const string schemaNamespace = "example.org";
var entity01 = new EdmEntityType(schemaNamespace, "entity01");
entity01.AddKeys(entity01.AddStructuralProperty("key", EdmPrimitiveTypeKind.String));
model.AddElement(entity01);

var entity02 = new EdmEntityType(schemaNamespace, "entity02");
entity02.AddKeys(entity02.AddStructuralProperty("key", EdmPrimitiveTypeKind.String));
entity02.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo { Name = "other", Target = entity01, TargetMultiplicity = EdmMultiplicity.Many });
model.AddElement(entity02);

var annotation = new EdmVocabularyAnnotation(new EdmNavigationPropertyPathExpression("foo/bar"), CoreVocabularyModel.DescriptionTerm, new EdmStringConstant("test"));
model.AddVocabularyAnnotation(annotation);

// Act & Assert for XML
WriteAndVerifyXml(model, @"<?xml version=""1.0"" encoding=""utf-16""?>" +
@"<edmx:Edmx Version=""4.0"" xmlns:edmx=""http://docs.oasis-open.org/odata/ns/edmx"">" +
@"<edmx:DataServices>" +
@"<Schema Namespace=""example.org"" xmlns=""http://docs.oasis-open.org/odata/ns/edm"">" +
@"<EntityType Name=""entity01"">" +
@"<Key>" +
@"<PropertyRef Name=""key"" />" +
@"</Key>" +
@"<Property Name=""key"" Type=""Edm.String"" />" +
@"</EntityType>" +
@"<EntityType Name=""entity02"">" +
@"<Key>" +
@"<PropertyRef Name=""key"" />" +
@"</Key>" +
@"<Property Name=""key"" Type=""Edm.String"" />" +
@"<NavigationProperty Name=""other"" Type=""Collection(example.org.entity01)"" />" +
@"</EntityType>" +
@"</Schema>" +
@"</edmx:DataServices>" +
@"</edmx:Edmx>");
}

internal static void WriteAndVerifyXml(IEdmModel model, string expected, CsdlTarget target = CsdlTarget.OData)
{
using (StringWriter sw = new StringWriter())
Expand Down
11 changes: 11 additions & 0 deletions test/FunctionalTests/Microsoft.OData.Edm.Tests/EdmUtilTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@ public void EntitySetShouldProduceCorrectFullyQualifiedName()
Assert.Equal("d.s.container/entitySet", EdmUtil.FullyQualifiedName(entitySet));
}

[Fact]
public void EdmNavigationPropertyHasAnnotationTargetName()
{
// arrange
var path = new Edm.Vocabularies.EdmNavigationPropertyPathExpression("foo/bar/baz");
// act
var actual = EdmUtil.FullyQualifiedName(path);
// assert
Assert.Equal("foo/bar/baz", actual);
}

[Theory]
[InlineData("", false)]
[InlineData(" ", false)]
Expand Down