Skip to content

Commit

Permalink
Fix issue where CsdlWriter TryWriteCsdl does not trigger flushing of …
Browse files Browse the repository at this point in the history
…XmlWriter buffer (#2523)
  • Loading branch information
gathogojr authored Oct 18, 2022
1 parent 9c58b8e commit d155952
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/Microsoft.OData.Edm/Csdl/CsdlXmlWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ private void WriteODataCsdl()
this.WriteSchemas();
this.EndElement(); // </DataServices>
this.EndElement(); // </Edmx>
this.writer.Flush();
}

private void WriteEFCsdl()
Expand All @@ -77,6 +78,7 @@ private void WriteEFCsdl()
this.EndElement(); // </ConceptualModels>
this.EndElement(); // </Runtime>
this.EndElement(); // </Edmx>
this.writer.Flush();
}

private void WriteEdmxElement()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
#if NETCOREAPP3_1
using System.Text.Encodings.Web;
using System.Text.Json;
Expand Down Expand Up @@ -2812,5 +2813,46 @@ public void ShouldSubstituteFullyQualifiedNamespaceWithAliasIfAliasIsSet()
}
}");
}

[Theory]
[InlineData(CsdlTarget.OData, "<edmx:DataServices>", "</edmx:DataServices>")]
[InlineData(CsdlTarget.EntityFramework, "<edmx:Runtime><edmx:ConceptualModels>", "</edmx:ConceptualModels></edmx:Runtime>")]
public void TryWriteCsdlShouldFlush(CsdlTarget csdlTarget, string schemaParentOpeningPartial, string schemaParentClosingPartial)
{
EdmModel model = new EdmModel();

var customerEntityType = new EdmEntityType("NS", "Customer");
var key = customerEntityType.AddStructuralProperty("Id", EdmPrimitiveTypeKind.Int32);
customerEntityType.AddKeys(key);
customerEntityType.AddStructuralProperty("Name", EdmPrimitiveTypeKind.String);
model.AddElement(customerEntityType);

var builder = new StringBuilder();
using (var writer = XmlWriter.Create(builder, new XmlWriterSettings { Encoding = Encoding.UTF8 }))
{
if (!CsdlWriter.TryWriteCsdl(model, writer, csdlTarget, out var errors))
{
Assert.True(false, "Serialization was unsuccessful");
}

// Xml writer should have flushed whatever is in the buffer before TryWriteCsdl is exited
Assert.Equal(
builder.ToString(),
"<?xml version=\"1.0\" encoding=\"utf-16\"?>" +
"<edmx:Edmx Version=\"4.0\" xmlns:edmx=\"http://docs.oasis-open.org/odata/ns/edmx\">" +
schemaParentOpeningPartial +
"<Schema Namespace=\"NS\" xmlns=\"http://docs.oasis-open.org/odata/ns/edm\">" +
"<EntityType Name=\"Customer\">" +
"<Key>" +
"<PropertyRef Name=\"Id\" />" +
"</Key>" +
"<Property Name=\"Id\" Type=\"Edm.Int32\" />" +
"<Property Name=\"Name\" Type=\"Edm.String\" />" +
"</EntityType>" +
"</Schema>" +
schemaParentClosingPartial +
"</edmx:Edmx>");
}
}
}
}
}

0 comments on commit d155952

Please sign in to comment.