From fe8e972999fe4cd13df9706709d994e4c67ddc6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Habinshuti?= Date: Tue, 12 Nov 2024 19:53:59 +0300 Subject: [PATCH 1/2] Fix line number reporting when parsing csdl with multiline reference elements --- src/Microsoft.OData.Edm/Csdl/CsdlReader.cs | 36 ++++-------- .../Csdl/CsdlReaderTests.cs | 55 +++++++++++++++++++ 2 files changed, 66 insertions(+), 25 deletions(-) diff --git a/src/Microsoft.OData.Edm/Csdl/CsdlReader.cs b/src/Microsoft.OData.Edm/Csdl/CsdlReader.cs index 579845432e..47537cbc91 100644 --- a/src/Microsoft.OData.Edm/Csdl/CsdlReader.cs +++ b/src/Microsoft.OData.Edm/Csdl/CsdlReader.cs @@ -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 (var 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() diff --git a/test/FunctionalTests/Microsoft.OData.Edm.Tests/Csdl/CsdlReaderTests.cs b/test/FunctionalTests/Microsoft.OData.Edm.Tests/Csdl/CsdlReaderTests.cs index 8bf539e6cd..b8de65402a 100644 --- a/test/FunctionalTests/Microsoft.OData.Edm.Tests/Csdl/CsdlReaderTests.cs +++ b/test/FunctionalTests/Microsoft.OData.Edm.Tests/Csdl/CsdlReaderTests.cs @@ -2252,6 +2252,61 @@ public void ShouldReportCorrectLineNumbersWithMultiLineElements() } + [Fact] + public void ShouldReportCorrectLineNumbersWithMultiLineReferenceElement() + { + string csdl = + """ + + + + + + + + + + + + + + + + + + + + + + + + """; + + + + 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() { From 40bfdf153f378fd70951ac27e5247c787ec65b7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Habinshuti?= Date: Wed, 13 Nov 2024 06:45:55 +0300 Subject: [PATCH 2/2] Minor var type change --- src/Microsoft.OData.Edm/Csdl/CsdlReader.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.OData.Edm/Csdl/CsdlReader.cs b/src/Microsoft.OData.Edm/Csdl/CsdlReader.cs index 47537cbc91..8a871b819a 100644 --- a/src/Microsoft.OData.Edm/Csdl/CsdlReader.cs +++ b/src/Microsoft.OData.Edm/Csdl/CsdlReader.cs @@ -623,7 +623,7 @@ private void ParseReferenceElement() if (referenceParser.HasErrors) { - foreach (var error in referenceParser.Errors) + foreach (EdmError error in referenceParser.Errors) { this.errors.Add(error); }