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

Fix line number reporting when parsing csdl with multiline reference element #3118

Merged
merged 2 commits into from
Nov 13, 2024
Merged
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
36 changes: 11 additions & 25 deletions src/Microsoft.OData.Edm/Csdl/CsdlReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -617,36 +617,22 @@ private void ParseReferenceElement()
{
Debug.Assert(this.reader.LocalName == CsdlConstants.Element_Reference);

XmlReaderSettings settings = new XmlReaderSettings();
IXmlLineInfo lineInfo = this.reader as IXmlLineInfo;
if (lineInfo != null && lineInfo.HasLineInfo())
{
settings.LineNumberOffset = lineInfo.LineNumber - 1;
settings.LinePositionOffset = lineInfo.LinePosition - 2;
}
string artifactPath = this.source ?? this.reader.BaseURI;
CsdlReferenceParser referenceParser = new CsdlReferenceParser(artifactPath, this.reader);
referenceParser.ParseDocumentElement();

using (StringReader sr = new StringReader(this.reader.ReadOuterXml()))
if (referenceParser.HasErrors)
{
using (XmlReader xr = XmlReader.Create(sr, settings))
foreach (EdmError error in referenceParser.Errors)
{
string artifactPath = this.source ?? this.reader.BaseURI;
CsdlReferenceParser referenceParser = new CsdlReferenceParser(artifactPath, xr);
referenceParser.ParseDocumentElement();

if (referenceParser.HasErrors)
{
foreach (var error in referenceParser.Errors)
{
this.errors.Add(error);
}
}

if (referenceParser.Result != null)
{
this.references.Add(referenceParser.Result.Value);
}
this.errors.Add(error);
}
}

if (referenceParser.Result != null)
{
this.references.Add(referenceParser.Result.Value);
}
}

private void ParseSchemaElement()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2252,6 +2252,61 @@ public void ShouldReportCorrectLineNumbersWithMultiLineElements()
}


[Fact]
public void ShouldReportCorrectLineNumbersWithMultiLineReferenceElement()
{
string csdl =
"""
<?xml version="1.0" encoding="utf-8"?>
<edmx:Edmx Version="4.0" xmlns:edmx="http://docs.oasis-open.org/odata/ns/edmx">
<edmx:Reference
Uri="https://docs.oasis-open.org/odata/odata-data-aggregation-ext/v4.0/cs03/vocabularies/Org.OData.Aggregation.V1.xml"
>
<edmx:Include
Namespace="Org.OData.Aggregation.V1" Alias="Aggregation"/>
</edmx:Reference>
<edmx:DataServices>
<Schema Namespace="name.space" Alias="self" xmlns="http://docs.oasis-open.org/odata/ns/edm" xmlns:ags="http://aggregator.microsoft.com/internal">
<EnumType

Name="someEnum">
<Member Name="notApplicable" Value="0" />

<Member Name="enabled" Value="1" />
<Member Name="disabled" Value="2" />
<Member Name="unknownFutureValue" Value="3" />
</EnumType>

<EnumType Name="otherEnum">
<Member Name="success" Value="0" />
<Member Name="failure" Value="1" />
<Member Name="unknownFutureValue" Value="2" />
</EnumType>
</Schema>
</edmx:DataServices>
</edmx:Edmx>
""";



using var reader = XmlReader.Create(new StringReader(csdl));
var model = CsdlReader.Parse(reader);

var reference = model.GetEdmReferences().FirstOrDefault();
var edmInclude = reference.Includes.FirstOrDefault(i => i.Alias == "Aggregation");
AssertLineLocation(edmInclude, 6, 6);

var someEnum = model.FindDeclaredType("name.space.someEnum") as IEdmEnumType;
AssertLineLocation(someEnum, 11, 8);

AssertLineLocation(
someEnum.Members.FirstOrDefault(m => m.Name == "disabled"), 17, 10);

var otherEnum = model.FindDeclaredType("name.space.otherEnum") as IEdmElement;
AssertLineLocation(otherEnum, 21, 8);
}


[Fact]
public void ShouldReportCorrectLineNumbersWithMultiLineElementsAndMultipleSchemas()
{
Expand Down