Skip to content
Draft
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 @@ -6,6 +6,7 @@
using System.Buffers.Text;
using System.ClientModel;
using System.ClientModel.Primitives;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
Expand Down Expand Up @@ -752,12 +753,12 @@ private MethodProvider BuildWriteObjectValueMethodGeneric()
},
writer.WriteEndObject(), Break
}),
// IEnumerable<object> case
BuildWriteObjectValueSwitchCase(typeof(IEnumerable<object>), "objectEnumerable",
// IEnumerable case
Comment thread
jorgerangel-msft marked this conversation as resolved.
BuildWriteObjectValueSwitchCase(typeof(IEnumerable), "objectEnumerable",
objectEnumerable => new MethodBodyStatement[]
{
writer.WriteStartArray(),
new ForEachStatement("item", objectEnumerable.As<IEnumerable<object>>(), out var item)
new ForEachStatement("item", objectEnumerable.As<IEnumerable>(), out var item)
{
writer.WriteObjectValue(item.As<object>(), options)
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -788,8 +788,12 @@ private MethodBodyStatement GetFrameworkTypeConversionFromReader(CSharpType elem
// Map framework types to their Utf8JsonReader method names
var readerMethodName = frameworkType switch
{
Type t when t == typeof(short) => nameof(Utf8JsonReader.GetInt16),
Type t when t == typeof(ushort) => nameof(Utf8JsonReader.GetUInt16),
Comment thread
jorgerangel-msft marked this conversation as resolved.
Type t when t == typeof(int) => nameof(Utf8JsonReader.GetInt32),
Type t when t == typeof(uint) => nameof(Utf8JsonReader.GetUInt32),
Type t when t == typeof(long) => nameof(Utf8JsonReader.GetInt64),
Type t when t == typeof(ulong) => nameof(Utf8JsonReader.GetUInt64),
Type t when t == typeof(bool) => nameof(Utf8JsonReader.GetBoolean),
Type t when t == typeof(double) => nameof(Utf8JsonReader.GetDouble),
Type t when t == typeof(float) => nameof(Utf8JsonReader.GetSingle),
Expand Down Expand Up @@ -1056,10 +1060,18 @@ private static bool ShouldBuildStackVarForFrameworkType(CSharpType type)
}

return type.Equals(typeof(string)) ||
type.Equals(typeof(short)) ||
type.Equals(typeof(short?)) ||
type.Equals(typeof(ushort)) ||
type.Equals(typeof(ushort?)) ||
Comment thread
jorgerangel-msft marked this conversation as resolved.
type.Equals(typeof(int)) ||
type.Equals(typeof(int?)) ||
type.Equals(typeof(uint)) ||
type.Equals(typeof(uint?)) ||
type.Equals(typeof(long)) ||
type.Equals(typeof(long?)) ||
type.Equals(typeof(ulong)) ||
type.Equals(typeof(ulong?)) ||
type.Equals(typeof(double)) ||
type.Equals(typeof(double?)) ||
type.Equals(typeof(float)) ||
Expand Down Expand Up @@ -1098,10 +1110,18 @@ private static bool IsConvertibleFromBinaryData(CSharpType type)
}

return type.Equals(typeof(string)) ||
type.Equals(typeof(short)) ||
type.Equals(typeof(short?)) ||
type.Equals(typeof(ushort)) ||
type.Equals(typeof(ushort?)) ||
type.Equals(typeof(int)) ||
type.Equals(typeof(int?)) ||
type.Equals(typeof(uint)) ||
type.Equals(typeof(uint?)) ||
type.Equals(typeof(long)) ||
type.Equals(typeof(long?)) ||
type.Equals(typeof(ulong)) ||
type.Equals(typeof(ulong?)) ||
type.Equals(typeof(double)) ||
type.Equals(typeof(double?)) ||
type.Equals(typeof(float)) ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System;
using System.ClientModel;
using System.ClientModel.Primitives;
using System.Collections;
using System.Collections.Generic;
using System.Text.Json;

Expand Down Expand Up @@ -89,7 +90,7 @@ public static void WriteObjectValue<T>(this global::System.Text.Json.Utf8JsonWri
}
writer.WriteEndObject();
break;
case global::System.Collections.Generic.IEnumerable<object> objectEnumerable:
case global::System.Collections.IEnumerable objectEnumerable:
writer.WriteStartArray();
foreach (var item in objectEnumerable)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System;
using System.ClientModel;
using System.ClientModel.Primitives;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text.Json;
Expand Down Expand Up @@ -91,7 +92,7 @@ public static void WriteObjectValue<T>(this global::System.Text.Json.Utf8JsonWri
}
writer.WriteEndObject();
break;
case global::System.Collections.Generic.IEnumerable<object> objectEnumerable:
case global::System.Collections.IEnumerable objectEnumerable:
Comment thread
jorgerangel-msft marked this conversation as resolved.
writer.WriteStartArray();
foreach (var item in objectEnumerable)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2528,6 +2528,69 @@ public void ListMethodIsRenamedToGet()
}
}

[TestCase(InputPrimitiveTypeKind.Int16)]
[TestCase(InputPrimitiveTypeKind.UInt16)]
[TestCase(InputPrimitiveTypeKind.UInt32)]
[TestCase(InputPrimitiveTypeKind.UInt64)]
public void NestedIntegerCollectionRequestsUseBinaryContentHelpers(InputPrimitiveTypeKind kind)
{
MockHelpers.LoadMockGenerator();
using var writer = new CodeWriter();
foreach (var isDictionary in new[] { false, true })
{
var elementType = new InputPrimitiveType(kind, kind.ToString(), $"TypeSpec.{kind.ToString().ToLowerInvariant()}");
var arrayType = InputFactory.Array(elementType);
var bodyType = isDictionary ? (InputType)InputFactory.Dictionary(arrayType) : InputFactory.Array(arrayType);
var name = isDictionary ? "SendRecord" : "SendNested";
var operation = InputFactory.Operation(name, parameters: [InputFactory.BodyParameter("body", bodyType, isRequired: true)]);
var serviceMethod = InputFactory.BasicServiceMethod(name, operation,
parameters: [InputFactory.MethodParameter("body", bodyType, location: InputRequestLocation.Body, isRequired: true)]);
var inputClient = InputFactory.Client("TestClient", methods: [serviceMethod]);
var client = ScmCodeModelGenerator.Instance.TypeFactory.CreateClient(inputClient)!;
var methods = new ScmMethodProviderCollection(serviceMethod, client);
foreach (var method in methods.Where(m => m.Kind == ScmMethodKind.Convenience))
{
writer.WriteMethod(method);
}
}

Assert.AreEqual(Helpers.GetExpectedFromFile(kind.ToString()), writer.ToString(false));
}

[TestCase(InputPrimitiveTypeKind.Int16)]
[TestCase(InputPrimitiveTypeKind.UInt16)]
[TestCase(InputPrimitiveTypeKind.UInt32)]
[TestCase(InputPrimitiveTypeKind.UInt64)]
public void IntegerCollectionResponsesUseUtf8JsonReader(InputPrimitiveTypeKind kind)
{
MockHelpers.LoadMockGenerator();
using var writer = new CodeWriter();
foreach (var isDictionary in new[] { false, true })
{
foreach (var isNullable in new[] { false, true })
{
InputType elementType = new InputPrimitiveType(kind, kind.ToString(), $"TypeSpec.{kind.ToString().ToLowerInvariant()}");
if (isNullable)
{
elementType = new InputNullableType(elementType);
}
var responseType = isDictionary ? (InputType)InputFactory.Dictionary(elementType) : InputFactory.Array(elementType);
var name = $"Get{(isNullable ? "Nullable" : "")}{(isDictionary ? "Dictionary" : "List")}";
var operation = InputFactory.Operation(name, responses: [InputFactory.OperationResponse([200], responseType)]);
var serviceMethod = InputFactory.BasicServiceMethod(name, operation);
var inputClient = InputFactory.Client("TestClient", methods: [serviceMethod]);
var client = ScmCodeModelGenerator.Instance.TypeFactory.CreateClient(inputClient)!;
var methods = new ScmMethodProviderCollection(serviceMethod, client);
foreach (var method in methods.Where(m => m.Kind == ScmMethodKind.Convenience))
{
writer.WriteMethod(method);
}
}
}

Assert.AreEqual(Helpers.GetExpectedFromFile(kind.ToString()), writer.ToString(false));
}

[TestCase(typeof(int))]
[TestCase(typeof(int), true)]
[TestCase(typeof(int?))]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
public virtual global::System.ClientModel.ClientResult<global::System.Collections.Generic.IReadOnlyList<short>> GetList(global::System.Threading.CancellationToken cancellationToken = default)
{
global::System.ClientModel.ClientResult result = this.GetList(cancellationToken.ToRequestOptions());
global::System.Collections.Generic.List<short> value = new global::System.Collections.Generic.List<short>();
global::System.BinaryData data = result.GetRawResponse().Content;
global::System.Text.Json.Utf8JsonReader jsonReader = new global::System.Text.Json.Utf8JsonReader(data.ToMemory().Span);
jsonReader.Read();
while (jsonReader.Read())
{
if ((jsonReader.TokenType == global::System.Text.Json.JsonTokenType.EndArray))
{
break;
}
value.Add(jsonReader.GetInt16());
}
return global::System.ClientModel.ClientResult.FromValue(((global::System.Collections.Generic.IReadOnlyList<short>)value), result.GetRawResponse());
}
public virtual async global::System.Threading.Tasks.Task<global::System.ClientModel.ClientResult<global::System.Collections.Generic.IReadOnlyList<short>>> GetListAsync(global::System.Threading.CancellationToken cancellationToken = default)
{
global::System.ClientModel.ClientResult result = await this.GetListAsync(cancellationToken.ToRequestOptions()).ConfigureAwait(false);
global::System.Collections.Generic.List<short> value = new global::System.Collections.Generic.List<short>();
global::System.BinaryData data = result.GetRawResponse().Content;
global::System.Text.Json.Utf8JsonReader jsonReader = new global::System.Text.Json.Utf8JsonReader(data.ToMemory().Span);
jsonReader.Read();
while (jsonReader.Read())
{
if ((jsonReader.TokenType == global::System.Text.Json.JsonTokenType.EndArray))
{
break;
}
value.Add(jsonReader.GetInt16());
}
return global::System.ClientModel.ClientResult.FromValue(((global::System.Collections.Generic.IReadOnlyList<short>)value), result.GetRawResponse());
}
public virtual global::System.ClientModel.ClientResult<global::System.Collections.Generic.IReadOnlyList<short?>> GetNullableList(global::System.Threading.CancellationToken cancellationToken = default)
{
global::System.ClientModel.ClientResult result = this.GetNullableList(cancellationToken.ToRequestOptions());
global::System.Collections.Generic.List<short?> value = new global::System.Collections.Generic.List<short?>();
global::System.BinaryData data = result.GetRawResponse().Content;
global::System.Text.Json.Utf8JsonReader jsonReader = new global::System.Text.Json.Utf8JsonReader(data.ToMemory().Span);
jsonReader.Read();
while (jsonReader.Read())
{
if ((jsonReader.TokenType == global::System.Text.Json.JsonTokenType.EndArray))
{
break;
}
value.Add((jsonReader.TokenType == global::System.Text.Json.JsonTokenType.Null) ? null : jsonReader.GetInt16());
}
return global::System.ClientModel.ClientResult.FromValue(((global::System.Collections.Generic.IReadOnlyList<short?>)value), result.GetRawResponse());
}
public virtual async global::System.Threading.Tasks.Task<global::System.ClientModel.ClientResult<global::System.Collections.Generic.IReadOnlyList<short?>>> GetNullableListAsync(global::System.Threading.CancellationToken cancellationToken = default)
{
global::System.ClientModel.ClientResult result = await this.GetNullableListAsync(cancellationToken.ToRequestOptions()).ConfigureAwait(false);
global::System.Collections.Generic.List<short?> value = new global::System.Collections.Generic.List<short?>();
global::System.BinaryData data = result.GetRawResponse().Content;
global::System.Text.Json.Utf8JsonReader jsonReader = new global::System.Text.Json.Utf8JsonReader(data.ToMemory().Span);
jsonReader.Read();
while (jsonReader.Read())
{
if ((jsonReader.TokenType == global::System.Text.Json.JsonTokenType.EndArray))
{
break;
}
value.Add((jsonReader.TokenType == global::System.Text.Json.JsonTokenType.Null) ? null : jsonReader.GetInt16());
}
return global::System.ClientModel.ClientResult.FromValue(((global::System.Collections.Generic.IReadOnlyList<short?>)value), result.GetRawResponse());
}
public virtual global::System.ClientModel.ClientResult<global::System.Collections.Generic.IReadOnlyDictionary<string, short>> GetDictionary(global::System.Threading.CancellationToken cancellationToken = default)
{
global::System.ClientModel.ClientResult result = this.GetDictionary(cancellationToken.ToRequestOptions());
global::System.Collections.Generic.IDictionary<string, short> value = new global::System.Collections.Generic.Dictionary<string, short>();
global::System.BinaryData data = result.GetRawResponse().Content;
global::System.Text.Json.Utf8JsonReader jsonReader = new global::System.Text.Json.Utf8JsonReader(data.ToMemory().Span);
jsonReader.Read();
while (jsonReader.Read())
{
if ((jsonReader.TokenType == global::System.Text.Json.JsonTokenType.EndObject))
{
break;
}
string propertyName = jsonReader.GetString();
jsonReader.Read();
value.Add(propertyName, jsonReader.GetInt16());
}
return global::System.ClientModel.ClientResult.FromValue(((global::System.Collections.Generic.IReadOnlyDictionary<string, short>)value), result.GetRawResponse());
}
public virtual async global::System.Threading.Tasks.Task<global::System.ClientModel.ClientResult<global::System.Collections.Generic.IReadOnlyDictionary<string, short>>> GetDictionaryAsync(global::System.Threading.CancellationToken cancellationToken = default)
{
global::System.ClientModel.ClientResult result = await this.GetDictionaryAsync(cancellationToken.ToRequestOptions()).ConfigureAwait(false);
global::System.Collections.Generic.IDictionary<string, short> value = new global::System.Collections.Generic.Dictionary<string, short>();
global::System.BinaryData data = result.GetRawResponse().Content;
global::System.Text.Json.Utf8JsonReader jsonReader = new global::System.Text.Json.Utf8JsonReader(data.ToMemory().Span);
jsonReader.Read();
while (jsonReader.Read())
{
if ((jsonReader.TokenType == global::System.Text.Json.JsonTokenType.EndObject))
{
break;
}
string propertyName = jsonReader.GetString();
jsonReader.Read();
value.Add(propertyName, jsonReader.GetInt16());
}
return global::System.ClientModel.ClientResult.FromValue(((global::System.Collections.Generic.IReadOnlyDictionary<string, short>)value), result.GetRawResponse());
}
public virtual global::System.ClientModel.ClientResult<global::System.Collections.Generic.IReadOnlyDictionary<string, short?>> GetNullableDictionary(global::System.Threading.CancellationToken cancellationToken = default)
{
global::System.ClientModel.ClientResult result = this.GetNullableDictionary(cancellationToken.ToRequestOptions());
global::System.Collections.Generic.IDictionary<string, short?> value = new global::System.Collections.Generic.Dictionary<string, short?>();
global::System.BinaryData data = result.GetRawResponse().Content;
global::System.Text.Json.Utf8JsonReader jsonReader = new global::System.Text.Json.Utf8JsonReader(data.ToMemory().Span);
jsonReader.Read();
while (jsonReader.Read())
{
if ((jsonReader.TokenType == global::System.Text.Json.JsonTokenType.EndObject))
{
break;
}
string propertyName = jsonReader.GetString();
jsonReader.Read();
value.Add(propertyName, (jsonReader.TokenType == global::System.Text.Json.JsonTokenType.Null) ? null : jsonReader.GetInt16());
}
return global::System.ClientModel.ClientResult.FromValue(((global::System.Collections.Generic.IReadOnlyDictionary<string, short?>)value), result.GetRawResponse());
}
public virtual async global::System.Threading.Tasks.Task<global::System.ClientModel.ClientResult<global::System.Collections.Generic.IReadOnlyDictionary<string, short?>>> GetNullableDictionaryAsync(global::System.Threading.CancellationToken cancellationToken = default)
{
global::System.ClientModel.ClientResult result = await this.GetNullableDictionaryAsync(cancellationToken.ToRequestOptions()).ConfigureAwait(false);
global::System.Collections.Generic.IDictionary<string, short?> value = new global::System.Collections.Generic.Dictionary<string, short?>();
global::System.BinaryData data = result.GetRawResponse().Content;
global::System.Text.Json.Utf8JsonReader jsonReader = new global::System.Text.Json.Utf8JsonReader(data.ToMemory().Span);
jsonReader.Read();
while (jsonReader.Read())
{
if ((jsonReader.TokenType == global::System.Text.Json.JsonTokenType.EndObject))
{
break;
}
string propertyName = jsonReader.GetString();
jsonReader.Read();
value.Add(propertyName, (jsonReader.TokenType == global::System.Text.Json.JsonTokenType.Null) ? null : jsonReader.GetInt16());
}
return global::System.ClientModel.ClientResult.FromValue(((global::System.Collections.Generic.IReadOnlyDictionary<string, short?>)value), result.GetRawResponse());
}
Loading
Loading