[WIP] Fix XML serialization for model collections#9826
Conversation
- Add nameHint parameter to WriteObjectValue for XmlWriter - Add XML FromEnumerable overload in BinaryContentHelper with rootNameHint/childNameHint - Update ScmMethodProviderCollection for XML list serialization/deserialization - Add helper methods to detect XML collection operations Co-authored-by: jorgerangel-msft <[email protected]>
Co-authored-by: jorgerangel-msft <[email protected]>
...ator/Microsoft.TypeSpec.Generator.ClientModel/src/Providers/BinaryContentHelperDefinition.cs
Outdated
Show resolved
Hide resolved
...ator/Microsoft.TypeSpec.Generator.ClientModel/src/Providers/BinaryContentHelperDefinition.cs
Outdated
Show resolved
Hide resolved
...erator/Microsoft.TypeSpec.Generator.ClientModel/src/Providers/ScmMethodProviderCollection.cs
Outdated
Show resolved
Hide resolved
...erator/Microsoft.TypeSpec.Generator.ClientModel/src/Providers/ScmMethodProviderCollection.cs
Show resolved
Hide resolved
...erator/Microsoft.TypeSpec.Generator.ClientModel/src/Providers/ScmMethodProviderCollection.cs
Outdated
Show resolved
Hide resolved
...erator/Microsoft.TypeSpec.Generator.ClientModel/src/Providers/ScmMethodProviderCollection.cs
Outdated
Show resolved
Hide resolved
...or/TestProjects/Local/Sample-TypeSpec/src/Generated/Internal/ModelSerializationExtensions.cs
Outdated
Show resolved
Hide resolved
|
@copilot try fixing the feedback again |
Co-authored-by: jorgerangel-msft <[email protected]>
All review feedback addressed in c149c5f:
|
...t.TypeSpec.Generator.ClientModel/src/Providers/ModelSerializationExtensionsDefinition.Xml.cs
Outdated
Show resolved
Hide resolved
...erator/Microsoft.TypeSpec.Generator.ClientModel/src/Providers/ScmMethodProviderCollection.cs
Outdated
Show resolved
Hide resolved
...erator/Microsoft.TypeSpec.Generator.ClientModel/src/Providers/ScmMethodProviderCollection.cs
Outdated
Show resolved
Hide resolved
.../Microsoft.TypeSpec.Generator.ClientModel/test/Providers/ScmMethodProviderCollectionTests.cs
Outdated
Show resolved
Hide resolved
|
@copilot lets also make |
Co-authored-by: jorgerangel-msft <[email protected]>
Co-authored-by: jorgerangel-msft <[email protected]>
...hodProviderCollectionTests/ConvenienceMethod_XmlListResponse_UsesXDocumentDeserialization.cs
Show resolved
Hide resolved
...t.TypeSpec.Generator.ClientModel/src/Providers/ModelSerializationExtensionsDefinition.Xml.cs
Outdated
Show resolved
Hide resolved
…tionExtensionsDefinition.Xml Co-authored-by: jorgerangel-msft <[email protected]>
jorgerangel-msft
left a comment
There was a problem hiding this comment.
@copilot we need to also add a test for validating the FromEnumerable method. It is not being generated correctly:
public static BinaryContent FromEnumerable<T>(IEnumerable<T> enumerable, string rootNameHint, string childNameHint)
where T : notnull
{
using (MemoryStream stream = new MemoryStream(256))
{
using (XmlWriter writer = XmlWriter.Create(stream0, ModelSerializationExtensions.XmlWriterSettings))
{
writer0.WriteStartElement(rootNameHint);
foreach (var item in enumerable)
{
writer0.WriteObjectValue<T>(item, ModelSerializationExtensions.WireOptions, childNameHint);
}
writer0.WriteEndElement();
}
if (stream1.Position > int.MaxValue)
{
return BinaryContent.Create(BinaryData.FromStream(stream1));
}
else
{
return BinaryContent.Create(new BinaryData(stream1.GetBuffer().AsMemory(0, (int)stream1.Position)));
}
}
}|
No changes needing a change description found. |
Co-authored-by: jorgerangel-msft <[email protected]>
Fixed in 33c80de. The issue was that standalone |
| && m.Signature.Parameters[1].Name == "rootNameHint"); | ||
| Assert.IsNotNull(fromEnumerableXmlMethod); | ||
| Assert.IsNotNull(fromEnumerableXmlMethod.BodyStatements); | ||
| Assert.AreEqual(Helpers.GetExpectedFromFile(), fromEnumerableXmlMethod.BodyStatements!.ToDisplayString()); |
There was a problem hiding this comment.
@copilot lets validate the entire method including the signature
| }, | ||
| xmlWriter.WriteEndElement(), | ||
| ]); | ||
| outerUsing.Add(innerUsing); |
nameHintparameter toWriteObjectValueforXmlWriterinModelSerializationExtensionsDefinition.Xml.csModelSerializationExtensionsSnippets.Xml.csto supportnameHintparameterFromEnumerableoverload inBinaryContentHelperDefinition.cswithrootNameHintandchildNameHintBinaryContentHelperSnippets.cswith newFromEnumerableoverload for XMLScmMethodProviderCollection.csserialization path to use XMLFromEnumerablewhen content type is XML and body is listScmMethodProviderCollection.csdeserialization path to use XDocument-based XML deserialization when response content type is XMLBinaryContentHelperDefinitionclass publicAddRangeinModelSerializationExtensionsDefinition.Xml.csFromEnumerableXML method (stream0/writer0/stream1 → stream/writer) and add testeng/scripts/Generate.ps1Original prompt
This section details on the original issue you should resolve
<issue_title>Fix XML Serialization For Model Collections</issue_title>
<issue_description>Consider this operation:
Since the request and response payload bodies are model collections, we cannot use ModelReaderWriter directly to serialize. We need to update our BinaryContentHelper and ModelSerializationExtensions to correctly handle this case.
For BinaryContentHelper, we need to generate this new helper method:
In the generated conv method, we then need to call this helper and pass in the both the serialized name of the collection and the serialized name of it's model elements:
The ModelSerializationExtension's
WriteObjectValuemethod for XML writer will need to be updated to this:For deserialization, we need to update the generated conv method to handle deserializing the payload directly: