Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -1023,7 +1023,7 @@ private static ValueExpression CreateXmlDeserializePrimitiveExpression(
{
return valueType.FrameworkType switch
{
Type t when t == typeof(Uri) => New.Instance(valueType.FrameworkType, element.Value()),
Type t when t == typeof(Uri) => New.Instance<Uri>(element.Value(), FrameworkEnumValue(UriKind.RelativeOrAbsolute)),
Type t when t == typeof(IPAddress) => Static<IPAddress>().Invoke(nameof(IPAddress.Parse), element.Value()),
Type t when t == typeof(Stream) => BinaryDataSnippets.FromString(element.Value()).ToStream(),
Type t when t == typeof(byte[]) => element.GetBytesFromBase64(serializationFormat.ToFormatSpecifier()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2226,7 +2226,7 @@ internal static ValueExpression DeserializeJsonValueCore(
ValueExpression? exp = frameworkType switch
{
Type t when t == typeof(Uri) =>
New.Instance(frameworkType, element.GetString()),
New.Instance<Uri>(element.GetString(), FrameworkEnumValue(UriKind.RelativeOrAbsolute)),
Type t when t == typeof(IPAddress) =>
Static<IPAddress>().Invoke(nameof(IPAddress.Parse), element.GetString()),
Type t when t == typeof(BinaryData) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,25 @@ public static IEnumerable<TestCaseData> TestDeserializationStatementTestCases
false);
}
}

[Test]
public void TestDeserializationUsesUriKindRelativeOrAbsolute()
{
var inputModel = InputFactory.Model("TestModel", properties:
[InputFactory.Property("prop1", InputPrimitiveType.Url)]);

var mrwProvider = new ModelProvider(inputModel).SerializationProviders.FirstOrDefault();
Assert.IsNotNull(mrwProvider);

var deserializationMethod = mrwProvider!.Methods.Where(m => m.Signature.Name.StartsWith("Deserialize")).FirstOrDefault();
Assert.IsNotNull(deserializationMethod);

var methodBody = deserializationMethod!.BodyStatements!.ToDisplayString();

Assert.IsTrue(methodBody.Contains("new global::System.Uri("),
$"Uri property should use new Uri() constructor. Actual:\n{methodBody}");
Assert.IsTrue(methodBody.Contains("global::System.UriKind.RelativeOrAbsolute"),
$"Uri property should specify UriKind.RelativeOrAbsolute. Actual:\n{methodBody}");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ protected virtual void JsonModelWriteCore(global::System.Text.Json.Utf8JsonWrite
{
continue;
}
prop1 = string.IsNullOrEmpty(prop.Value.GetString()) ? null : new global::System.Uri(prop.Value.GetString());
prop1 = string.IsNullOrEmpty(prop.Value.GetString()) ? null : new global::System.Uri(prop.Value.GetString(), global::System.UriKind.RelativeOrAbsolute);
continue;
}
if ((options.Format != "W"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,32 @@ public void XmlDeserializationHandlesBytesProperty()
$"Bytes property should use child.GetBytesFromBase64(\"D\") with Base64 format. Actual:\n{methodBody}");
}

[Test]
public void XmlDeserializationHandlesUriProperty()
{
var inputModel = InputFactory.Model(
"TestXmlModel",
usage: InputModelTypeUsage.Input | InputModelTypeUsage.Xml,
properties: [InputFactory.Property(
"endpoint",
InputPrimitiveType.Url,
serializationOptions: InputFactory.Serialization.Options(xml: InputFactory.Serialization.Xml("endpoint")))]);

var modelProvider = new ModelProvider(inputModel);
var mrwProvider = modelProvider.SerializationProviders.FirstOrDefault() as MrwSerializationTypeDefinition;
Assert.IsNotNull(mrwProvider);
var xmlDeserializationMethod = mrwProvider!.Methods.FirstOrDefault(m => m.Signature.Name == "DeserializeTestXmlModel" &&
m.Signature.Parameters.Any(p => p.Type.Equals(typeof(XElement))));

Assert.IsNotNull(xmlDeserializationMethod);
var methodBody = xmlDeserializationMethod!.BodyStatements!.ToDisplayString();

Assert.IsTrue(methodBody.Contains("new global::System.Uri("),
$"Uri property should use new Uri() constructor for deserialization. Actual:\n{methodBody}");
Assert.IsTrue(methodBody.Contains("global::System.UriKind.RelativeOrAbsolute"),
$"Uri property should specify UriKind.RelativeOrAbsolute. Actual:\n{methodBody}");
}

[Test]
public void XmlDeserializationHandlesBinaryDataProperty()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
global::System.BinaryData data = result.GetRawResponse().Content;
using global::System.Text.Json.JsonDocument document = global::System.Text.Json.JsonDocument.Parse(data);
global::System.Text.Json.JsonElement element = document.RootElement;
return global::System.ClientModel.ClientResult.FromValue(new global::System.Uri(element.GetString()), result.GetRawResponse());
return global::System.ClientModel.ClientResult.FromValue(new global::System.Uri(element.GetString(), global::System.UriKind.RelativeOrAbsolute), result.GetRawResponse());
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ internal static ListWithNextLinkResponse DeserializeListWithNextLinkResponse(Jso
{
continue;
}
next = string.IsNullOrEmpty(prop.Value.GetString()) ? null : new Uri(prop.Value.GetString());
next = string.IsNullOrEmpty(prop.Value.GetString()) ? null : new Uri(prop.Value.GetString(), UriKind.RelativeOrAbsolute);
continue;
}
if (options.Format != "W")
Expand Down