Add virtual DeserializeXmlValue and SerializeXmlValue APIs to ScmTypeFactory#10186
Add virtual DeserializeXmlValue and SerializeXmlValue APIs to ScmTypeFactory#10186
Conversation
…cmTypeFactory Agent-Logs-Url: https://github.com/microsoft/typespec/sessions/d5c279ee-c360-42a7-970b-30add42ea465 Co-authored-by: jorgerangel-msft <102122018+jorgerangel-msft@users.noreply.github.com>
Agent-Logs-Url: https://github.com/microsoft/typespec/sessions/d5c279ee-c360-42a7-970b-30add42ea465 Co-authored-by: jorgerangel-msft <102122018+jorgerangel-msft@users.noreply.github.com>
.chronus/changes/add-xml-serialize-deserialize-value-apis-2026-3-27.md
Outdated
Show resolved
Hide resolved
Agent-Logs-Url: https://github.com/microsoft/typespec/sessions/4d2deae7-13ce-4e8a-bf7c-0084380adacb Co-authored-by: jorgerangel-msft <102122018+jorgerangel-msft@users.noreply.github.com>
jorgerangel-msft
left a comment
There was a problem hiding this comment.
@copilot ensure all tests are passing. Also rerun the Generate.ps1 script
| }; | ||
| } | ||
|
|
||
| internal static ValueExpression DeserializeXmlValueCore( |
There was a problem hiding this comment.
@copilot can we just replace the CreateXmlDeserializeValueExpression method with this method? The contents should be unchanged from CreateXmlDeserializeValueExpression.
There was a problem hiding this comment.
Done in 4772098. CreateXmlDeserializeValueExpression has been replaced with DeserializeXmlValueCore (internal static), and callers now route through ScmCodeModelGenerator.Instance.TypeFactory.DeserializeXmlValue(...) matching the JSON pattern.
| return CreateXmlDeserializePrimitiveExpression(element, valueType, format); | ||
| } | ||
|
|
||
| internal static MethodBodyStatement SerializeXmlValueCore( |
There was a problem hiding this comment.
@copilot can we just replace the CreateXmlSerializeValueExpression method with this? The body should be the same as CreateXmlSerializeValueExpression.
There was a problem hiding this comment.
Done in 4772098. CreateXmlSerializeValueExpression has been replaced with SerializeXmlValueCore (internal static, returning ValueExpression with the same body), and callers now route through ScmCodeModelGenerator.Instance.TypeFactory.SerializeXmlValue(...).
| }; | ||
| } | ||
|
|
||
| internal static ValueExpression SerializeXmlValueExpressionCore(ValueExpression value, CSharpType valueType, SerializationFormat serializationFormat) |
There was a problem hiding this comment.
Removed in 4772098. It was a duplicate of CreateXmlSerializeValueExpression. Now SerializeXmlValueCore returns ValueExpression directly (same body as the old method), so SerializeXmlValueExpressionCore is no longer needed.
| ScopedApi<XElement> element, | ||
| ScopedApi<ModelReaderWriterOptions> mrwOptionsParameter, | ||
| SerializationFormat format) | ||
| => MrwSerializationTypeDefinition.DeserializeXmlValueCore(valueType, element, mrwOptionsParameter, format); |
| ScopedApi<XmlWriter> xmlWriter, | ||
| ScopedApi<ModelReaderWriterOptions> mrwOptionsParameter, | ||
| SerializationFormat serializationFormat) | ||
| => MrwSerializationTypeDefinition.SerializeXmlValueCore(valueType, value, xmlWriter, mrwOptionsParameter, serializationFormat); |
| [TestCase(typeof(byte), SerializationFormat.Default, ExpectedResult = "((byte)((int)foo))")] | ||
| [TestCase(typeof(sbyte), SerializationFormat.Default, ExpectedResult = "((sbyte)((int)foo))")] | ||
| [TestCase(typeof(short), SerializationFormat.Default, ExpectedResult = "((short)((int)foo))")] | ||
| public string TestDeserializeXmlValueCore_PrimitiveTypes(Type type, SerializationFormat format) |
| } | ||
|
|
||
| [Test] | ||
| public void TestSerializeXmlValueCore_String() |
There was a problem hiding this comment.
@copilot use TestData to validate the serialization tests.
There was a problem hiding this comment.
Done in 4772098. Added TestData-based validation for two override tests: SerializeXmlValueOverride_CustomTypeSerialization and DeserializeXmlValueOverride_CustomTypeDeserialization, which compare full generated output against expected .cs files.
|
|
||
| [TestCase(SerializationFormat.Duration_ISO8601)] | ||
| [TestCase(SerializationFormat.Duration_Constant)] | ||
| public void TestSerializeXmlValueCore_TimeSpan(SerializationFormat format) |
There was a problem hiding this comment.
@copilot we should also add a test case where we override the new APIs and add custom serialization for a new type, or existing type.
There was a problem hiding this comment.
Done in 4772098. Added two override tests using Moq:
SerializeXmlValueOverride_CustomTypeSerialization— overridesSerializeXmlValueforstringtype to returnvalue.ToString()and validates output with TestDataDeserializeXmlValueOverride_CustomTypeDeserialization— overridesDeserializeXmlValueforstringtype to returnelement.ToString()and validates output with TestData
…ds, move tests to XML test files, add override tests Agent-Logs-Url: https://github.com/microsoft/typespec/sessions/fb4989f3-4f35-4758-9fbc-8e1a87f8c2c4 Co-authored-by: jorgerangel-msft <102122018+jorgerangel-msft@users.noreply.github.com>
ScmTypeFactoryhas virtualDeserializeJsonValue/SerializeJsonValuefor JSON but lacks XML equivalents, preventing extending generators from overriding XML serialization for types likeEtag.Changes
ScmTypeFactory.cs— AddedvirtualmethodsDeserializeXmlValue(returnsValueExpression) andSerializeXmlValue(returnsValueExpression), enabling extending generators to override XML serializationMrwSerializationTypeDefinition.Xml.cs— Replaced privateCreateXmlDeserializeValueExpressionandCreateXmlSerializeValueExpressionwithinternal staticcore methods (DeserializeXmlValueCore,SerializeXmlValueCore). Updated callers to route throughScmCodeModelGenerator.Instance.TypeFactoryfor overrideability, matching the JSON pattern.XmlDeserializationTests.cs— Added unit tests forDeserializeXmlValueCorecovering primitive types,DateTimeOffset, andTimeSpanXmlSerializationTests.cs— Added unit tests forSerializeXmlValueCorecovering primitive types,DateTimeOffset, andTimeSpan. Added TestData-validated override tests demonstrating custom type factory overrides for both serialize and deserialize directions.⌨️ Start Copilot coding agent tasks without leaving your editor — available in VS Code, Visual Studio, JetBrains IDEs and Eclipse.