diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/src/Providers/ScmMethodProviderCollection.cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/src/Providers/ScmMethodProviderCollection.cs index fb0bb6e6025..e5fd808282a 100644 --- a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/src/Providers/ScmMethodProviderCollection.cs +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/src/Providers/ScmMethodProviderCollection.cs @@ -7,6 +7,7 @@ using System.Collections; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; +using System.Globalization; using System.IO; using System.Linq; using System.Net.ServerSentEvents; @@ -295,7 +296,8 @@ .. GetStackVariablesForProtocolParamConversion(convenienceBodyParameters, out va Declare("result", This.Invoke(protocolMethod.Signature, [.. GetProtocolMethodArguments(paramDeclarations)], isAsync).ToApi(), out ClientResponseApi result), .. GetStackVariablesForReturnValueConversion(result, responseBodyType, isAsync, out var resultDeclarations), IsConvertibleFromBinaryData(responseBodyType) - ? Return(result.FromValue(GetResultConversion(result, result.GetRawResponse(), responseBodyType, resultDeclarations), result.GetRawResponse())) + || GetPlainTextParseType(responseBodyType, out _) is not null + ? GetResultConversionStatements(result, result.GetRawResponse(), responseBodyType, resultDeclarations) : new[] { @@ -634,6 +636,21 @@ private IEnumerable GetStackVariablesForReturnValueConversi out declarations); } + if ((IsConvertibleFromBinaryData(responseBodyType) || GetPlainTextParseType(responseBodyType, out _) is not null) + && (responseBodyType.IsFrameworkType || responseBodyType.IsEnum) + && !responseBodyType.Equals(typeof(BinaryData)) + && !HasOnlyPlainTextContentType()) + { + var data = result.GetRawResponse().Content(); + var statements = new MethodBodyStatement[] + { + UsingDeclare("document", data.Parse(), out var document) + }; + declarations["data"] = data; + declarations["document"] = document; + return statements; + } + return []; } @@ -837,6 +854,47 @@ private MethodBodyStatement AddElement(ValueExpression? dictKey, ValueExpression return scopedApi.Add(element); } + private MethodBodyStatement[] GetResultConversionStatements(ClientResponseApi result, HttpResponseApi response, CSharpType responseBodyType, Dictionary declarations) + { + var plainTextParseType = GetPlainTextParseType(responseBodyType, out var enumType); + if (!responseBodyType.Equals(typeof(string)) && plainTextParseType is not null && HasOnlyPlainTextContentType()) + { + return + [ + Declare("value", responseBodyType, GetPlainTextValueConversion(responseBodyType, plainTextParseType, enumType, response.Content().InvokeToString()), out var value), + Return(result.FromValue(value, response)) + ]; + } + + var isSpecialCaseType = responseBodyType.Equals(typeof(BinaryData)) + || responseBodyType.IsCollection + || (responseBodyType.Equals(typeof(string)) && HasOnlyPlainTextContentType()); + + if (!isSpecialCaseType && (responseBodyType.IsFrameworkType || responseBodyType.IsEnum)) + { + var element = declarations["document"].As().RootElement(); + var deserializedValue = ScmCodeModelGenerator.Instance.TypeFactory.DeserializeJsonValue( + responseBodyType.WithNullable(false), + element, + declarations["data"].As(), + ScmCodeModelGenerator.Instance.ModelSerializationExtensionsDefinition.WireOptionsField.As(), + responseBodyType.Equals(typeof(TimeSpan)) || responseBodyType.Equals(typeof(TimeSpan?)) + ? SerializationFormat.Duration_Constant + : SerializationFormat.Default); + var valueExpression = responseBodyType.IsNullable + ? new TernaryConditionalExpression(element.ValueKindEqualsNull(), Null.CastTo(responseBodyType), deserializedValue) + : deserializedValue; + + return + [ + Declare("value", responseBodyType, valueExpression, out var value), + Return(result.FromValue(value, response)) + ]; + } + + return [Return(result.FromValue(GetResultConversion(result, response, responseBodyType, declarations), response))]; + } + private ValueExpression GetResultConversion(ClientResponseApi result, HttpResponseApi response, CSharpType responseBodyType, Dictionary declarations) { if (responseBodyType.Equals(typeof(BinaryData))) @@ -855,19 +913,157 @@ private ValueExpression GetResultConversion(ClientResponseApi result, HttpRespon { return declarations["value"].CastTo(new CSharpType(responseBodyType.OutputType.FrameworkType, responseBodyType.Arguments[0], responseBodyType.Arguments[1])); } - if (responseBodyType.Equals(typeof(string)) && ServiceMethod.Operation.Responses.Any(r => r.IsErrorResponse is false && r.ContentTypes.Contains("text/plain"))) + if (responseBodyType.Equals(typeof(string)) && HasOnlyPlainTextContentType()) { return response.Content().InvokeToString(); } - if (responseBodyType.IsFrameworkType) + return result.CastTo(responseBodyType); + } + + private ValueExpression GetPlainTextValueConversion(CSharpType responseBodyType, Type parseType, CSharpType? enumType, ValueExpression content) + { + var invariantCulture = new MemberExpression(typeof(CultureInfo), nameof(CultureInfo.InvariantCulture)); + var deserializedValue = parseType switch + { + Type t when t == typeof(string) => content, + Type t when t == typeof(bool) => Static().Invoke(nameof(bool.Parse), content).As(), + Type t when t == typeof(Guid) => Static().Invoke(nameof(Guid.Parse), content).As(), + Type t when t == typeof(Uri) => New.Instance(content, FrameworkEnumValue(UriKind.RelativeOrAbsolute)), + Type t when t == typeof(TimeSpan) => GetPlainTextTimeSpanConversion(content, invariantCulture), + Type t when t == typeof(DateTimeOffset) => content.As().ParseDateTimeOffset(Literal(GetResponseSerializationFormat().ToFormatSpecifier())), + // The remaining supported types are numeric and all expose a static Parse(string, IFormatProvider) method. + _ => Static(parseType).Invoke(nameof(int.Parse), [content, invariantCulture]).As(parseType) + }; + + if (enumType is not null) { - return response.Content().ToObjectFromJson(responseBodyType); + deserializedValue = enumType.ToEnum(deserializedValue); } - if (responseBodyType.IsEnum) + + return responseBodyType.IsNullable + ? new TernaryConditionalExpression(content.As().Trim().Equal(Literal("null")), Null.CastTo(responseBodyType), deserializedValue) + : deserializedValue; + } + + /// + /// Builds the raw-text conversion for a response, honoring the response body's wire + /// encoding. Numeric duration encodings (seconds/milliseconds) parse the content as a number and construct + /// the from it, matching 's JSON handling; + /// all other encodings (ISO 8601, constant, plain time) parse the content directly using the corresponding + /// format specifier. + /// + private ValueExpression GetPlainTextTimeSpanConversion(ValueExpression content, ValueExpression invariantCulture) + { + var format = GetResponseSerializationFormat(); + switch (format) + { + case SerializationFormat.Duration_Seconds: + return TimeSpanSnippets.FromSeconds(ParseNumeric(content, invariantCulture)); + case SerializationFormat.Duration_Seconds_Int64: + return TimeSpanSnippets.FromSeconds(ParseNumeric(content, invariantCulture)); + case SerializationFormat.Duration_Seconds_Float: + case SerializationFormat.Duration_Seconds_Double: + // Float and Double wire encodings are intentionally collapsed to a single double.Parse, + // matching MrwSerializationTypeDefinition's JSON path, which uses GetDouble() for both. + return TimeSpanSnippets.FromSeconds(ParseNumeric(content, invariantCulture)); + case SerializationFormat.Duration_Milliseconds: + return TimeSpanSnippets.FromMilliseconds(ParseNumeric(content, invariantCulture)); + case SerializationFormat.Duration_Milliseconds_Int64: + return TimeSpanSnippets.FromMilliseconds(ParseNumeric(content, invariantCulture)); + case SerializationFormat.Duration_Milliseconds_Float: + case SerializationFormat.Duration_Milliseconds_Double: + // See the Duration_Seconds_Float/Double comment above. + return TimeSpanSnippets.FromMilliseconds(ParseNumeric(content, invariantCulture)); + } + + var formatSpecifier = format.ToFormatSpecifier(); + if (formatSpecifier is null) { - return responseBodyType.ToEnum(response.Content().ToObjectFromJson(responseBodyType.UnderlyingEnumType)); + ScmCodeModelGenerator.Instance.Emitter.ReportDiagnostic( + DiagnosticCodes.UnsupportedSerialization, + $"Unsupported duration serialization format: {format}. Falling back to constant duration format.", + ServiceMethod.Operation.CrossLanguageDefinitionId); + formatSpecifier = SerializationFormat.Duration_Constant.ToFormatSpecifier()!; } - return result.CastTo(responseBodyType); + + // ISO 8601 ("P"), constant ("c") and plain time ("T") encodings all parse the content directly. + return content.As().ParseTimeSpan(Literal(formatSpecifier)); + } + + /// + /// Builds a T.Parse(content, invariantCulture) invocation for the given numeric . + /// + private static ScopedApi ParseNumeric(ValueExpression content, ValueExpression invariantCulture) + where T : struct + { + // Static members on a generic type parameter cannot be referenced by nameof. + return Static().Invoke("Parse", [content, invariantCulture]).As(); + } + + /// + /// Gets the framework type that a raw text response body is parsed into, or null when the response body + /// type isn't a primitive or enum that can be parsed from raw text. Types such as , + /// collections and generated models keep their existing conversion. + /// + private static Type? GetPlainTextParseType(CSharpType responseBodyType, out CSharpType? enumType) + { + enumType = null; + var typeToParse = responseBodyType.WithNullable(false); + if (typeToParse is { IsEnum: true, UnderlyingEnumType: { } underlyingEnumType }) + { + enumType = typeToParse; + typeToParse = underlyingEnumType; + } + + if (!typeToParse.IsFrameworkType) + { + return null; + } + + var frameworkType = typeToParse.FrameworkType; + return frameworkType switch + { + Type t when t == typeof(string) + || t == typeof(bool) + || t == typeof(Guid) + || t == typeof(Uri) + || t == typeof(TimeSpan) + || t == typeof(DateTimeOffset) + || t == typeof(byte) + || t == typeof(sbyte) + || t == typeof(short) + || t == typeof(ushort) + || t == typeof(int) + || t == typeof(uint) + || t == typeof(long) + || t == typeof(ulong) + || t == typeof(float) + || t == typeof(double) + || t == typeof(decimal) => frameworkType, + _ => null + }; + } + + private bool HasOnlyPlainTextContentType() + { + var contentTypes = ServiceMethod.Operation.Responses + .Where(r => r.IsErrorResponse is false) + .SelectMany(r => r.ContentTypes); + return contentTypes.Any() && contentTypes.All(IsPlainTextContentType); + } + + private static bool IsPlainTextContentType(string contentType) + { + return contentType.Split(';')[0].Trim().Equals("text/plain", StringComparison.OrdinalIgnoreCase); + } + + private SerializationFormat GetResponseSerializationFormat() + { + var responseBodyType = ServiceMethod.Operation.Responses + .FirstOrDefault(r => r.IsErrorResponse is false)?.BodyType; + return responseBodyType is null + ? SerializationFormat.Default + : ScmCodeModelGenerator.Instance.TypeFactory.GetSerializationFormat(responseBodyType); } private static bool ShouldBuildStackVarForFrameworkType(CSharpType type) diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/ClientProviders/ClientProviderTests.cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/ClientProviders/ClientProviderTests.cs index 69d952fd84f..fef94f5ff15 100644 --- a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/ClientProviders/ClientProviderTests.cs +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/ClientProviders/ClientProviderTests.cs @@ -3049,13 +3049,9 @@ public async Task BackCompatibility_ConvenienceMethodParamOrderChanged() var body = syncConvenienceMethod!.BodyStatements; Assert.IsNotNull(body); - var result = body!.ToDisplayString(); - Assert.AreEqual( - "global::Sample.Argument.AssertNotNullOrEmpty(param1, nameof(param1));\n\n" + - "using global::System.ClientModel.BinaryContent content = global::System.ClientModel.BinaryContent.Create(global::System.BinaryData.FromString(param1));\n" + - "global::System.ClientModel.ClientResult result = this.GetData(param3, param2, content, cancellationToken.ToRequestOptions());\n" + - "return global::System.ClientModel.ClientResult.FromValue(result.GetRawResponse().Content.ToObjectFromJson(), result.GetRawResponse());\n", - result); + using var syncWriter = new CodeWriter(); + syncWriter.WriteMethod(syncConvenienceMethod); + Assert.AreEqual(Helpers.GetExpectedFromFile("Sync"), syncWriter.ToString(false)); var asyncConvenienceMethod = convenienceMethods .FirstOrDefault(m => m.Signature.Modifiers.HasFlag(MethodSignatureModifiers.Async)); @@ -3064,13 +3060,9 @@ public async Task BackCompatibility_ConvenienceMethodParamOrderChanged() body = asyncConvenienceMethod!.BodyStatements; Assert.IsNotNull(body); - result = body!.ToDisplayString(); - Assert.AreEqual( - "global::Sample.Argument.AssertNotNullOrEmpty(param1, nameof(param1));\n\n" + - "using global::System.ClientModel.BinaryContent content = global::System.ClientModel.BinaryContent.Create(global::System.BinaryData.FromString(param1));\n" + - "global::System.ClientModel.ClientResult result = await this.GetDataAsync(param3, param2, content, cancellationToken.ToRequestOptions()).ConfigureAwait(false);\n" + - "return global::System.ClientModel.ClientResult.FromValue(result.GetRawResponse().Content.ToObjectFromJson(), result.GetRawResponse());\n", - result); + using var asyncWriter = new CodeWriter(); + asyncWriter.WriteMethod(asyncConvenienceMethod); + Assert.AreEqual(Helpers.GetExpectedFromFile("Async"), asyncWriter.ToString(false)); } [Test] @@ -3146,13 +3138,9 @@ public async Task BackCompatibility_BothMethodsParamOrderChanged() var body = syncConvenienceMethod!.BodyStatements; Assert.IsNotNull(body); - var result = body!.ToDisplayString(); - Assert.AreEqual( - "global::Sample.Argument.AssertNotNullOrEmpty(param1, nameof(param1));\n\n" + - "using global::System.ClientModel.BinaryContent content = global::System.ClientModel.BinaryContent.Create(global::System.BinaryData.FromString(param1));\n" + - "global::System.ClientModel.ClientResult result = this.UpdateResource(content, param2, param3, cancellationToken.ToRequestOptions());\n" + - "return global::System.ClientModel.ClientResult.FromValue(result.GetRawResponse().Content.ToObjectFromJson(), result.GetRawResponse());\n", - result); + using var syncWriter = new CodeWriter(); + syncWriter.WriteMethod(syncConvenienceMethod); + Assert.AreEqual(Helpers.GetExpectedFromFile("Sync"), syncWriter.ToString(false)); var asyncConvenienceMethod = convenienceMethods .FirstOrDefault(m => m.Signature.Modifiers.HasFlag(MethodSignatureModifiers.Async)); @@ -3161,13 +3149,9 @@ public async Task BackCompatibility_BothMethodsParamOrderChanged() body = asyncConvenienceMethod!.BodyStatements; Assert.IsNotNull(body); - result = body!.ToDisplayString(); - Assert.AreEqual( - "global::Sample.Argument.AssertNotNullOrEmpty(param1, nameof(param1));\n\n" + - "using global::System.ClientModel.BinaryContent content = global::System.ClientModel.BinaryContent.Create(global::System.BinaryData.FromString(param1));\n" + - "global::System.ClientModel.ClientResult result = await this.UpdateResourceAsync(content, param2, param3, cancellationToken.ToRequestOptions()).ConfigureAwait(false);\n" + - "return global::System.ClientModel.ClientResult.FromValue(result.GetRawResponse().Content.ToObjectFromJson(), result.GetRawResponse());\n", - result); + using var asyncWriter = new CodeWriter(); + asyncWriter.WriteMethod(asyncConvenienceMethod); + Assert.AreEqual(Helpers.GetExpectedFromFile("Async"), asyncWriter.ToString(false)); } [Test] diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/ClientProviders/TestData/ClientProviderTests/BackCompatibility_BothMethodsParamOrderChanged(Async).cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/ClientProviders/TestData/ClientProviderTests/BackCompatibility_BothMethodsParamOrderChanged(Async).cs new file mode 100644 index 00000000000..c729f869898 --- /dev/null +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/ClientProviders/TestData/ClientProviderTests/BackCompatibility_BothMethodsParamOrderChanged(Async).cs @@ -0,0 +1,10 @@ +public virtual async global::System.Threading.Tasks.Task> UpdateResourceAsync(string param1, int param2, bool param3, global::System.Threading.CancellationToken cancellationToken = default) +{ + global::Sample.Argument.AssertNotNullOrEmpty(param1, nameof(param1)); + + using global::System.ClientModel.BinaryContent content = global::System.ClientModel.BinaryContent.Create(global::System.BinaryData.FromString(param1)); + global::System.ClientModel.ClientResult result = await this.UpdateResourceAsync(content, param2, param3, cancellationToken.ToRequestOptions()).ConfigureAwait(false); + using global::System.Text.Json.JsonDocument document = global::System.Text.Json.JsonDocument.Parse(result.GetRawResponse().Content); + string value = document.RootElement.GetString(); + return global::System.ClientModel.ClientResult.FromValue(value, result.GetRawResponse()); +} diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/ClientProviders/TestData/ClientProviderTests/BackCompatibility_BothMethodsParamOrderChanged(Sync).cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/ClientProviders/TestData/ClientProviderTests/BackCompatibility_BothMethodsParamOrderChanged(Sync).cs new file mode 100644 index 00000000000..2eaf6ad9f42 --- /dev/null +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/ClientProviders/TestData/ClientProviderTests/BackCompatibility_BothMethodsParamOrderChanged(Sync).cs @@ -0,0 +1,10 @@ +public virtual global::System.ClientModel.ClientResult UpdateResource(string param1, int param2, bool param3, global::System.Threading.CancellationToken cancellationToken = default) +{ + global::Sample.Argument.AssertNotNullOrEmpty(param1, nameof(param1)); + + using global::System.ClientModel.BinaryContent content = global::System.ClientModel.BinaryContent.Create(global::System.BinaryData.FromString(param1)); + global::System.ClientModel.ClientResult result = this.UpdateResource(content, param2, param3, cancellationToken.ToRequestOptions()); + using global::System.Text.Json.JsonDocument document = global::System.Text.Json.JsonDocument.Parse(result.GetRawResponse().Content); + string value = document.RootElement.GetString(); + return global::System.ClientModel.ClientResult.FromValue(value, result.GetRawResponse()); +} diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/ClientProviders/TestData/ClientProviderTests/BackCompatibility_ConvenienceMethodParamOrderChanged(Async).cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/ClientProviders/TestData/ClientProviderTests/BackCompatibility_ConvenienceMethodParamOrderChanged(Async).cs new file mode 100644 index 00000000000..c1844f8cfd1 --- /dev/null +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/ClientProviders/TestData/ClientProviderTests/BackCompatibility_ConvenienceMethodParamOrderChanged(Async).cs @@ -0,0 +1,10 @@ +public virtual async global::System.Threading.Tasks.Task> GetDataAsync(string param1, int param2, bool param3, global::System.Threading.CancellationToken cancellationToken = default) +{ + global::Sample.Argument.AssertNotNullOrEmpty(param1, nameof(param1)); + + using global::System.ClientModel.BinaryContent content = global::System.ClientModel.BinaryContent.Create(global::System.BinaryData.FromString(param1)); + global::System.ClientModel.ClientResult result = await this.GetDataAsync(param3, param2, content, cancellationToken.ToRequestOptions()).ConfigureAwait(false); + using global::System.Text.Json.JsonDocument document = global::System.Text.Json.JsonDocument.Parse(result.GetRawResponse().Content); + string value = document.RootElement.GetString(); + return global::System.ClientModel.ClientResult.FromValue(value, result.GetRawResponse()); +} diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/ClientProviders/TestData/ClientProviderTests/BackCompatibility_ConvenienceMethodParamOrderChanged(Sync).cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/ClientProviders/TestData/ClientProviderTests/BackCompatibility_ConvenienceMethodParamOrderChanged(Sync).cs new file mode 100644 index 00000000000..4280587f377 --- /dev/null +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/ClientProviders/TestData/ClientProviderTests/BackCompatibility_ConvenienceMethodParamOrderChanged(Sync).cs @@ -0,0 +1,10 @@ +public virtual global::System.ClientModel.ClientResult GetData(string param1, int param2, bool param3, global::System.Threading.CancellationToken cancellationToken = default) +{ + global::Sample.Argument.AssertNotNullOrEmpty(param1, nameof(param1)); + + using global::System.ClientModel.BinaryContent content = global::System.ClientModel.BinaryContent.Create(global::System.BinaryData.FromString(param1)); + global::System.ClientModel.ClientResult result = this.GetData(param3, param2, content, cancellationToken.ToRequestOptions()); + using global::System.Text.Json.JsonDocument document = global::System.Text.Json.JsonDocument.Parse(result.GetRawResponse().Content); + string value = document.RootElement.GetString(); + return global::System.ClientModel.ClientResult.FromValue(value, result.GetRawResponse()); +} diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/ClientProviders/TestData/ClientProviderTests/BackCompatibility_MultipleNewOptionalNonBodyParametersAdded.cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/ClientProviders/TestData/ClientProviderTests/BackCompatibility_MultipleNewOptionalNonBodyParametersAdded.cs index ecd8a142b0c..aa2f0e6461a 100644 --- a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/ClientProviders/TestData/ClientProviderTests/BackCompatibility_MultipleNewOptionalNonBodyParametersAdded.cs +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/ClientProviders/TestData/ClientProviderTests/BackCompatibility_MultipleNewOptionalNonBodyParametersAdded.cs @@ -1,4 +1,4 @@ -// +// #nullable disable @@ -6,6 +6,7 @@ using System.ClientModel; using System.ClientModel.Primitives; using System.ComponentModel; +using System.Text.Json; using System.Threading; using System.Threading.Tasks; @@ -35,7 +36,9 @@ public partial class TestClient using global::System.ClientModel.BinaryContent content = global::System.ClientModel.BinaryContent.Create(global::System.BinaryData.FromString(param2)); global::System.ClientModel.ClientResult result = this.GetData(param1, content, param3, param4, cancellationToken.ToRequestOptions()); - return global::System.ClientModel.ClientResult.FromValue(result.GetRawResponse().Content.ToObjectFromJson(), result.GetRawResponse()); + using global::System.Text.Json.JsonDocument document = global::System.Text.Json.JsonDocument.Parse(result.GetRawResponse().Content); + string value = document.RootElement.GetString(); + return global::System.ClientModel.ClientResult.FromValue(value, result.GetRawResponse()); } public virtual async global::System.Threading.Tasks.Task> GetDataAsync(int param1, string param2, bool? param3 = default, string param4 = default, global::System.Threading.CancellationToken cancellationToken = default) @@ -44,7 +47,9 @@ public partial class TestClient using global::System.ClientModel.BinaryContent content = global::System.ClientModel.BinaryContent.Create(global::System.BinaryData.FromString(param2)); global::System.ClientModel.ClientResult result = await this.GetDataAsync(param1, content, param3, param4, cancellationToken.ToRequestOptions()).ConfigureAwait(false); - return global::System.ClientModel.ClientResult.FromValue(result.GetRawResponse().Content.ToObjectFromJson(), result.GetRawResponse()); + using global::System.Text.Json.JsonDocument document = global::System.Text.Json.JsonDocument.Parse(result.GetRawResponse().Content); + string value = document.RootElement.GetString(); + return global::System.ClientModel.ClientResult.FromValue(value, result.GetRawResponse()); } #pragma warning disable AZC0002 // Back-compat overload preserves the previous method signature where CancellationToken was the trailing parameter. Making it optional would introduce an ambiguous call with the new method. diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/ClientProviders/TestData/ClientProviderTests/BackCompatibility_NewOptionalBodyParameterDoesNotAddBackCompatOverload.cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/ClientProviders/TestData/ClientProviderTests/BackCompatibility_NewOptionalBodyParameterDoesNotAddBackCompatOverload.cs index bc72ecb5894..5e74787430c 100644 --- a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/ClientProviders/TestData/ClientProviderTests/BackCompatibility_NewOptionalBodyParameterDoesNotAddBackCompatOverload.cs +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/ClientProviders/TestData/ClientProviderTests/BackCompatibility_NewOptionalBodyParameterDoesNotAddBackCompatOverload.cs @@ -1,10 +1,11 @@ -// +// #nullable disable using System; using System.ClientModel; using System.ClientModel.Primitives; +using System.Text.Json; using System.Threading; using System.Threading.Tasks; @@ -28,14 +29,18 @@ public partial class TestClient { using global::System.ClientModel.BinaryContent content = global::System.ClientModel.BinaryContent.Create(global::System.BinaryData.FromString(param1)); global::System.ClientModel.ClientResult result = this.GetData(param2, content, cancellationToken.ToRequestOptions()); - return global::System.ClientModel.ClientResult.FromValue(result.GetRawResponse().Content.ToObjectFromJson(), result.GetRawResponse()); + using global::System.Text.Json.JsonDocument document = global::System.Text.Json.JsonDocument.Parse(result.GetRawResponse().Content); + string value = document.RootElement.GetString(); + return global::System.ClientModel.ClientResult.FromValue(value, result.GetRawResponse()); } public virtual async global::System.Threading.Tasks.Task> GetDataAsync(int param2, string param1 = default, global::System.Threading.CancellationToken cancellationToken = default) { using global::System.ClientModel.BinaryContent content = global::System.ClientModel.BinaryContent.Create(global::System.BinaryData.FromString(param1)); global::System.ClientModel.ClientResult result = await this.GetDataAsync(param2, content, cancellationToken.ToRequestOptions()).ConfigureAwait(false); - return global::System.ClientModel.ClientResult.FromValue(result.GetRawResponse().Content.ToObjectFromJson(), result.GetRawResponse()); + using global::System.Text.Json.JsonDocument document = global::System.Text.Json.JsonDocument.Parse(result.GetRawResponse().Content); + string value = document.RootElement.GetString(); + return global::System.ClientModel.ClientResult.FromValue(value, result.GetRawResponse()); } } } diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/ClientProviders/TestData/ClientProviderTests/BackCompatibility_NewOptionalNonBodyParameterAdded.cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/ClientProviders/TestData/ClientProviderTests/BackCompatibility_NewOptionalNonBodyParameterAdded.cs index dec0cb1513e..a2bdfe6ff93 100644 --- a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/ClientProviders/TestData/ClientProviderTests/BackCompatibility_NewOptionalNonBodyParameterAdded.cs +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/ClientProviders/TestData/ClientProviderTests/BackCompatibility_NewOptionalNonBodyParameterAdded.cs @@ -1,4 +1,4 @@ -// +// #nullable disable @@ -6,6 +6,7 @@ using System.ClientModel; using System.ClientModel.Primitives; using System.ComponentModel; +using System.Text.Json; using System.Threading; using System.Threading.Tasks; @@ -35,7 +36,9 @@ public partial class TestClient using global::System.ClientModel.BinaryContent content = global::System.ClientModel.BinaryContent.Create(global::System.BinaryData.FromString(param2)); global::System.ClientModel.ClientResult result = this.GetData(param1, content, param3, cancellationToken.ToRequestOptions()); - return global::System.ClientModel.ClientResult.FromValue(result.GetRawResponse().Content.ToObjectFromJson(), result.GetRawResponse()); + using global::System.Text.Json.JsonDocument document = global::System.Text.Json.JsonDocument.Parse(result.GetRawResponse().Content); + string value = document.RootElement.GetString(); + return global::System.ClientModel.ClientResult.FromValue(value, result.GetRawResponse()); } public virtual async global::System.Threading.Tasks.Task> GetDataAsync(int param1, string param2, bool? param3 = default, global::System.Threading.CancellationToken cancellationToken = default) @@ -44,7 +47,9 @@ public partial class TestClient using global::System.ClientModel.BinaryContent content = global::System.ClientModel.BinaryContent.Create(global::System.BinaryData.FromString(param2)); global::System.ClientModel.ClientResult result = await this.GetDataAsync(param1, content, param3, cancellationToken.ToRequestOptions()).ConfigureAwait(false); - return global::System.ClientModel.ClientResult.FromValue(result.GetRawResponse().Content.ToObjectFromJson(), result.GetRawResponse()); + using global::System.Text.Json.JsonDocument document = global::System.Text.Json.JsonDocument.Parse(result.GetRawResponse().Content); + string value = document.RootElement.GetString(); + return global::System.ClientModel.ClientResult.FromValue(value, result.GetRawResponse()); } #pragma warning disable AZC0002 // Back-compat overload preserves the previous method signature where CancellationToken was the trailing parameter. Making it optional would introduce an ambiguous call with the new method. diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/ClientProviders/TestData/ClientProviderTests/BackCompatibility_NewOptionalNonBodyParameterAddedWithModelBody.cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/ClientProviders/TestData/ClientProviderTests/BackCompatibility_NewOptionalNonBodyParameterAddedWithModelBody.cs index c1f11b6273c..9e9679f33bf 100644 --- a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/ClientProviders/TestData/ClientProviderTests/BackCompatibility_NewOptionalNonBodyParameterAddedWithModelBody.cs +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/ClientProviders/TestData/ClientProviderTests/BackCompatibility_NewOptionalNonBodyParameterAddedWithModelBody.cs @@ -1,10 +1,11 @@ -// +// #nullable disable using System.ClientModel; using System.ClientModel.Primitives; using System.ComponentModel; +using System.Text.Json; using System.Threading; using System.Threading.Tasks; using Sample.Models; @@ -34,7 +35,9 @@ public partial class TestClient global::Sample.Argument.AssertNotNull(body, nameof(body)); global::System.ClientModel.ClientResult result = this.GetData(param1, body, param3, cancellationToken.ToRequestOptions()); - return global::System.ClientModel.ClientResult.FromValue(result.GetRawResponse().Content.ToObjectFromJson(), result.GetRawResponse()); + using global::System.Text.Json.JsonDocument document = global::System.Text.Json.JsonDocument.Parse(result.GetRawResponse().Content); + string value = document.RootElement.GetString(); + return global::System.ClientModel.ClientResult.FromValue(value, result.GetRawResponse()); } public virtual async global::System.Threading.Tasks.Task> GetDataAsync(int param1, global::Sample.Models.SampleModel body, bool? param3 = default, global::System.Threading.CancellationToken cancellationToken = default) @@ -42,7 +45,9 @@ public partial class TestClient global::Sample.Argument.AssertNotNull(body, nameof(body)); global::System.ClientModel.ClientResult result = await this.GetDataAsync(param1, body, param3, cancellationToken.ToRequestOptions()).ConfigureAwait(false); - return global::System.ClientModel.ClientResult.FromValue(result.GetRawResponse().Content.ToObjectFromJson(), result.GetRawResponse()); + using global::System.Text.Json.JsonDocument document = global::System.Text.Json.JsonDocument.Parse(result.GetRawResponse().Content); + string value = document.RootElement.GetString(); + return global::System.ClientModel.ClientResult.FromValue(value, result.GetRawResponse()); } #pragma warning disable AZC0002 // Back-compat overload preserves the previous method signature where CancellationToken was the trailing parameter. Making it optional would introduce an ambiguous call with the new method. diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/ClientProviders/TestData/ClientProviderTests/BackCompatibility_NewOptionalNonBodyParameterAddedWithPathAndHeaderParameters.cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/ClientProviders/TestData/ClientProviderTests/BackCompatibility_NewOptionalNonBodyParameterAddedWithPathAndHeaderParameters.cs index f97dddf4f4a..f88b0b9a854 100644 --- a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/ClientProviders/TestData/ClientProviderTests/BackCompatibility_NewOptionalNonBodyParameterAddedWithPathAndHeaderParameters.cs +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/ClientProviders/TestData/ClientProviderTests/BackCompatibility_NewOptionalNonBodyParameterAddedWithPathAndHeaderParameters.cs @@ -1,10 +1,11 @@ -// +// #nullable disable using System.ClientModel; using System.ClientModel.Primitives; using System.ComponentModel; +using System.Text.Json; using System.Threading; using System.Threading.Tasks; @@ -36,7 +37,9 @@ public partial class TestClient global::Sample.Argument.AssertNotNullOrEmpty(region, nameof(region)); global::System.ClientModel.ClientResult result = this.GetData(itemId, filter, region, sort, cancellationToken.ToRequestOptions()); - return global::System.ClientModel.ClientResult.FromValue(result.GetRawResponse().Content.ToObjectFromJson(), result.GetRawResponse()); + using global::System.Text.Json.JsonDocument document = global::System.Text.Json.JsonDocument.Parse(result.GetRawResponse().Content); + string value = document.RootElement.GetString(); + return global::System.ClientModel.ClientResult.FromValue(value, result.GetRawResponse()); } public virtual async global::System.Threading.Tasks.Task> GetDataAsync(string itemId, int filter, string region, string sort = default, global::System.Threading.CancellationToken cancellationToken = default) @@ -45,7 +48,9 @@ public partial class TestClient global::Sample.Argument.AssertNotNullOrEmpty(region, nameof(region)); global::System.ClientModel.ClientResult result = await this.GetDataAsync(itemId, filter, region, sort, cancellationToken.ToRequestOptions()).ConfigureAwait(false); - return global::System.ClientModel.ClientResult.FromValue(result.GetRawResponse().Content.ToObjectFromJson(), result.GetRawResponse()); + using global::System.Text.Json.JsonDocument document = global::System.Text.Json.JsonDocument.Parse(result.GetRawResponse().Content); + string value = document.RootElement.GetString(); + return global::System.ClientModel.ClientResult.FromValue(value, result.GetRawResponse()); } #pragma warning disable AZC0002 // Back-compat overload preserves the previous method signature where CancellationToken was the trailing parameter. Making it optional would introduce an ambiguous call with the new method. diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/ClientProviders/TestData/ClientProviderTests/BackCompatibility_NewOptionalParameterWithReservedName.cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/ClientProviders/TestData/ClientProviderTests/BackCompatibility_NewOptionalParameterWithReservedName.cs index ffa25774611..7f6dc185754 100644 --- a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/ClientProviders/TestData/ClientProviderTests/BackCompatibility_NewOptionalParameterWithReservedName.cs +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/ClientProviders/TestData/ClientProviderTests/BackCompatibility_NewOptionalParameterWithReservedName.cs @@ -1,4 +1,4 @@ -// +// #nullable disable @@ -6,6 +6,7 @@ using System.ClientModel; using System.ClientModel.Primitives; using System.ComponentModel; +using System.Text.Json; using System.Threading; using System.Threading.Tasks; @@ -35,7 +36,9 @@ public partial class TestClient using global::System.ClientModel.BinaryContent content0 = global::System.ClientModel.BinaryContent.Create(global::System.BinaryData.FromString(content)); global::System.ClientModel.ClientResult result = this.GetData(param1, content0, @select, cancellationToken.ToRequestOptions()); - return global::System.ClientModel.ClientResult.FromValue(result.GetRawResponse().Content.ToObjectFromJson(), result.GetRawResponse()); + using global::System.Text.Json.JsonDocument document = global::System.Text.Json.JsonDocument.Parse(result.GetRawResponse().Content); + string value = document.RootElement.GetString(); + return global::System.ClientModel.ClientResult.FromValue(value, result.GetRawResponse()); } public virtual async global::System.Threading.Tasks.Task> GetDataAsync(int param1, string content, string @select = default, global::System.Threading.CancellationToken cancellationToken = default) @@ -44,7 +47,9 @@ public partial class TestClient using global::System.ClientModel.BinaryContent content0 = global::System.ClientModel.BinaryContent.Create(global::System.BinaryData.FromString(content)); global::System.ClientModel.ClientResult result = await this.GetDataAsync(param1, content0, @select, cancellationToken.ToRequestOptions()).ConfigureAwait(false); - return global::System.ClientModel.ClientResult.FromValue(result.GetRawResponse().Content.ToObjectFromJson(), result.GetRawResponse()); + using global::System.Text.Json.JsonDocument document = global::System.Text.Json.JsonDocument.Parse(result.GetRawResponse().Content); + string value = document.RootElement.GetString(); + return global::System.ClientModel.ClientResult.FromValue(value, result.GetRawResponse()); } [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Never)] diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/ClientProviders/TestData/ClientProviderTests/BackCompatibility_NewRequiredParameterDoesNotAddBackCompatOverload.cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/ClientProviders/TestData/ClientProviderTests/BackCompatibility_NewRequiredParameterDoesNotAddBackCompatOverload.cs index df446a2081b..5211ba5197d 100644 --- a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/ClientProviders/TestData/ClientProviderTests/BackCompatibility_NewRequiredParameterDoesNotAddBackCompatOverload.cs +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/ClientProviders/TestData/ClientProviderTests/BackCompatibility_NewRequiredParameterDoesNotAddBackCompatOverload.cs @@ -1,10 +1,11 @@ -// +// #nullable disable using System; using System.ClientModel; using System.ClientModel.Primitives; +using System.Text.Json; using System.Threading; using System.Threading.Tasks; @@ -34,7 +35,9 @@ public partial class TestClient using global::System.ClientModel.BinaryContent content = global::System.ClientModel.BinaryContent.Create(global::System.BinaryData.FromString(param1)); global::System.ClientModel.ClientResult result = this.GetData(param2, param3, content, cancellationToken.ToRequestOptions()); - return global::System.ClientModel.ClientResult.FromValue(result.GetRawResponse().Content.ToObjectFromJson(), result.GetRawResponse()); + using global::System.Text.Json.JsonDocument document = global::System.Text.Json.JsonDocument.Parse(result.GetRawResponse().Content); + string value = document.RootElement.GetString(); + return global::System.ClientModel.ClientResult.FromValue(value, result.GetRawResponse()); } public virtual async global::System.Threading.Tasks.Task> GetDataAsync(int param2, bool param3, string param1, global::System.Threading.CancellationToken cancellationToken = default) @@ -43,7 +46,9 @@ public partial class TestClient using global::System.ClientModel.BinaryContent content = global::System.ClientModel.BinaryContent.Create(global::System.BinaryData.FromString(param1)); global::System.ClientModel.ClientResult result = await this.GetDataAsync(param2, param3, content, cancellationToken.ToRequestOptions()).ConfigureAwait(false); - return global::System.ClientModel.ClientResult.FromValue(result.GetRawResponse().Content.ToObjectFromJson(), result.GetRawResponse()); + using global::System.Text.Json.JsonDocument document = global::System.Text.Json.JsonDocument.Parse(result.GetRawResponse().Content); + string value = document.RootElement.GetString(); + return global::System.ClientModel.ClientResult.FromValue(value, result.GetRawResponse()); } } } diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/ScmMethodProviderCollectionTests.cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/ScmMethodProviderCollectionTests.cs index 6aadbf6d2f5..7262aeb6c97 100644 --- a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/ScmMethodProviderCollectionTests.cs +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/ScmMethodProviderCollectionTests.cs @@ -5,12 +5,15 @@ using System.ClientModel; using System.ClientModel.Primitives; using System.Collections.Generic; +using System.IO; using System.Linq; using System.Net.ServerSentEvents; +using System.Text; using System.Threading; using System.Threading.Tasks; using Microsoft.CodeAnalysis; using Microsoft.TypeSpec.Generator.ClientModel.Providers; +using Microsoft.TypeSpec.Generator.EmitterRpc; using Microsoft.TypeSpec.Generator.Input; using Microsoft.TypeSpec.Generator.Input.Extensions; using Microsoft.TypeSpec.Generator.Primitives; @@ -909,7 +912,7 @@ public void ListMethodWithEnumParameter(bool isExtensible, InputRequestLocation convenienceMethod.BodyStatements!.ToDisplayString()); } } - #pragma warning restore SCME0005 +#pragma warning restore SCME0005 } // Enum bodies must be serialized via Utf8JsonWriter (not BinaryData.FromObjectAsJson) to stay AOT/trim safe (IL2026/IL3050). @@ -1559,21 +1562,31 @@ public void ListMethodIsRenamedToGet() } [TestCase(typeof(int))] + [TestCase(typeof(int), true)] + [TestCase(typeof(int?))] + [TestCase(typeof(int?), true)] [TestCase(typeof(long))] [TestCase(typeof(float))] [TestCase(typeof(double))] + [TestCase(typeof(decimal))] [TestCase(typeof(bool))] + [TestCase(typeof(bool?))] [TestCase(typeof(string))] [TestCase(typeof(Uri))] + [TestCase(typeof(byte))] + [TestCase(typeof(sbyte))] [TestCase(typeof(BinaryData))] [TestCase(typeof(DateTimeOffset))] [TestCase(typeof(TimeSpan))] - public void ScalarReturnTypeMethods(Type type) + [TestCase(typeof(TimeSpan?))] + public void ScalarReturnTypeMethods(Type type, bool isAsync = false) { - InputType? inputType = type switch + var underlyingType = Nullable.GetUnderlyingType(type); + InputType? inputType = (underlyingType ?? type) switch { { } t when t == typeof(float) => InputPrimitiveType.Float32, { } t when t == typeof(double) => InputPrimitiveType.Float64, + { } t when t == typeof(decimal) => new InputPrimitiveType(InputPrimitiveTypeKind.Decimal128, "decimal128", "TypeSpec.decimal128"), { } t when t == typeof(bool) => InputPrimitiveType.Boolean, { } t when t == typeof(string) => InputPrimitiveType.String, { } t when t == typeof(DateTimeOffset) => InputPrimitiveType.PlainDate, @@ -1581,10 +1594,17 @@ public void ScalarReturnTypeMethods(Type type) { } t when t == typeof(int) => InputPrimitiveType.Int32, { } t when t == typeof(long) => InputPrimitiveType.Int64, { } t when t == typeof(Uri) => InputPrimitiveType.Url, + { } t when t == typeof(byte) => new InputPrimitiveType(InputPrimitiveTypeKind.UInt8, "uint8", "TypeSpec.uint8"), + { } t when t == typeof(sbyte) => new InputPrimitiveType(InputPrimitiveTypeKind.Int8, "int8", "TypeSpec.int8"), { } t when t == typeof(BinaryData) => InputPrimitiveType.Base64, _ => null }; + if (underlyingType != null) + { + inputType = new InputNullableType(inputType!); + } + var inputOperation = InputFactory.Operation( "GetScalar", responses: [InputFactory.OperationResponse([200], inputType!)]); @@ -1600,9 +1620,319 @@ public void ScalarReturnTypeMethods(Type type) Assert.IsNotNull(methodCollection); var convenienceMethod = methodCollection.FirstOrDefault(m => m.Signature.Parameters.All(p => p.Name != "options") - && m.Signature.Name == $"{inputOperation.Name.ToIdentifierName()}"); + && m.Signature.Name == $"{inputOperation.Name.ToIdentifierName()}{(isAsync ? "Async" : "")}"); - Assert.AreEqual(Helpers.GetExpectedFromFile(type.Name), convenienceMethod!.BodyStatements!.ToDisplayString()); + var baselineName = underlyingType != null ? $"{underlyingType.Name}Nullable" : type.Name; + using var writer = new CodeWriter(); + writer.WriteMethod(convenienceMethod!); + Assert.AreEqual(Helpers.GetExpectedFromFile($"{baselineName}{(isAsync ? "Async" : "")}"), writer.ToString(false)); + } + + [TestCase(true, true, false)] + [TestCase(true, false, false)] + [TestCase(false, true, false)] + [TestCase(false, false, false)] + [TestCase(true, true, true)] + [TestCase(true, false, true)] + [TestCase(false, true, true)] + [TestCase(false, false, true)] + public void EnumReturnTypeMethods(bool isString, bool isExtensible, bool isNullable) + { + InputType inputType = isString + ? InputFactory.StringEnum("TestEnum", [("Value", "value")], isExtensible: isExtensible) + : InputFactory.Int32Enum("TestEnum", [("Value", 1)], isExtensible: isExtensible); + if (isNullable) + { + inputType = new InputNullableType(inputType); + } + + var operation = InputFactory.Operation("GetEnum", responses: [InputFactory.OperationResponse([200], inputType)]); + var serviceMethod = InputFactory.BasicServiceMethod("GetEnum", operation); + var inputClient = InputFactory.Client("TestClient", methods: [serviceMethod]); + + MockHelpers.LoadMockGenerator(); + var client = ScmCodeModelGenerator.Instance.TypeFactory.CreateClient(inputClient); + var method = new ScmMethodProviderCollection(serviceMethod, client!) + .Single(m => m.Kind == ScmMethodKind.Convenience && m.Signature.Name == "GetEnum"); + + using var writer = new CodeWriter(); + writer.WriteMethod(method); + Assert.AreEqual(Helpers.GetExpectedFromFile($"{isString},{isExtensible},{isNullable}"), writer.ToString(false)); + } + + [Test] + public void PlainTextReturnTypeMethods() + { + var operation = InputFactory.Operation("GetText", responses: + [InputFactory.OperationResponse([200], InputPrimitiveType.String, contentTypes: ["text/plain"])]); + var serviceMethod = InputFactory.BasicServiceMethod("GetText", operation); + var inputClient = InputFactory.Client("TestClient", methods: [serviceMethod]); + + MockHelpers.LoadMockGenerator(); + var client = ScmCodeModelGenerator.Instance.TypeFactory.CreateClient(inputClient); + var method = new ScmMethodProviderCollection(serviceMethod, client!) + .Single(m => m.Kind == ScmMethodKind.Convenience && m.Signature.Name == "GetText"); + + using var writer = new CodeWriter(); + writer.WriteMethod(method); + Assert.AreEqual(Helpers.GetExpectedFromFile(), writer.ToString(false)); + } + + [TestCase(typeof(int))] + [TestCase(typeof(int?))] + [TestCase(typeof(bool))] + [TestCase(typeof(bool?))] + [TestCase(typeof(TimeSpan))] + [TestCase(typeof(TimeSpan?))] + [TestCase(typeof(DateTimeOffset))] + [TestCase(typeof(Uri))] + [TestCase(typeof(byte))] + [TestCase(typeof(sbyte))] + public void PlainTextScalarReturnTypeMethods(Type type) + { + var underlyingType = Nullable.GetUnderlyingType(type); + InputType inputType = (underlyingType ?? type) switch + { + { } t when t == typeof(int) => InputPrimitiveType.Int32, + { } t when t == typeof(bool) => InputPrimitiveType.Boolean, + { } t when t == typeof(TimeSpan) => InputPrimitiveType.PlainTime, + { } t when t == typeof(DateTimeOffset) => InputPrimitiveType.PlainDate, + { } t when t == typeof(Uri) => InputPrimitiveType.Url, + { } t when t == typeof(byte) => new InputPrimitiveType(InputPrimitiveTypeKind.UInt8, "uint8", "TypeSpec.uint8"), + { } t when t == typeof(sbyte) => new InputPrimitiveType(InputPrimitiveTypeKind.Int8, "int8", "TypeSpec.int8"), + _ => throw new NotSupportedException() + }; + if (underlyingType != null) + { + inputType = new InputNullableType(inputType); + } + + var operation = InputFactory.Operation("GetPlainTextScalar", responses: + [InputFactory.OperationResponse([200], inputType, contentTypes: ["text/plain"])]); + var serviceMethod = InputFactory.BasicServiceMethod("GetPlainTextScalar", operation); + var inputClient = InputFactory.Client("TestClient", methods: [serviceMethod]); + + MockHelpers.LoadMockGenerator(); + var client = ScmCodeModelGenerator.Instance.TypeFactory.CreateClient(inputClient); + var method = new ScmMethodProviderCollection(serviceMethod, client!) + .Single(m => m.Kind == ScmMethodKind.Convenience && m.Signature.Name == "GetPlainTextScalar"); + + using var writer = new CodeWriter(); + writer.WriteMethod(method); + var baselineName = underlyingType != null ? $"{underlyingType.Name}Nullable" : type.Name; + Assert.AreEqual(Helpers.GetExpectedFromFile(baselineName), writer.ToString(false)); + } + + [TestCase("Text/Plain")] + [TestCase("text/plain; charset=utf-8")] + public void PlainTextScalarReturnTypeMethodsHandlesTextPlainMediaTypeVariants(string contentType) + { + var operation = InputFactory.Operation("GetPlainTextScalar", responses: + [InputFactory.OperationResponse([200], InputPrimitiveType.Int32, contentTypes: [contentType])]); + var serviceMethod = InputFactory.BasicServiceMethod("GetPlainTextScalar", operation); + var inputClient = InputFactory.Client("TestClient", methods: [serviceMethod]); + + MockHelpers.LoadMockGenerator(); + var client = ScmCodeModelGenerator.Instance.TypeFactory.CreateClient(inputClient); + var method = new ScmMethodProviderCollection(serviceMethod, client!) + .Single(m => m.Kind == ScmMethodKind.Convenience && m.Signature.Name == "GetPlainTextScalar"); + + using var writer = new CodeWriter(); + writer.WriteMethod(method); + Assert.AreEqual(Helpers.GetExpectedFromFile("Int32", nameof(PlainTextScalarReturnTypeMethods)), writer.ToString(false)); + } + + [Test] + public void JsonScalarReturnTypeMethodsDoNotMatchTextPlainMediaTypeParameter() + { + var operation = InputFactory.Operation("GetScalar", responses: + [InputFactory.OperationResponse([200], InputPrimitiveType.Int32, contentTypes: ["application/json; profile=\"text/plain\""])]); + var serviceMethod = InputFactory.BasicServiceMethod("GetScalar", operation); + var inputClient = InputFactory.Client("TestClient", methods: [serviceMethod]); + + MockHelpers.LoadMockGenerator(); + var client = ScmCodeModelGenerator.Instance.TypeFactory.CreateClient(inputClient); + var method = new ScmMethodProviderCollection(serviceMethod, client!) + .Single(m => m.Kind == ScmMethodKind.Convenience && m.Signature.Name == "GetScalar"); + + using var writer = new CodeWriter(); + writer.WriteMethod(method); + Assert.AreEqual(Helpers.GetExpectedFromFile("Int32", nameof(ScalarReturnTypeMethods)), writer.ToString(false)); + } + + [TestCase("Iso8601", null, false)] + [TestCase("Constant", null, false)] + [TestCase("Seconds", InputPrimitiveTypeKind.Int32, false)] + [TestCase("Seconds", InputPrimitiveTypeKind.Int64, false)] + [TestCase("Seconds", InputPrimitiveTypeKind.Float32, false)] + [TestCase("Seconds", InputPrimitiveTypeKind.Float64, false)] + [TestCase("Milliseconds", InputPrimitiveTypeKind.Int32, false)] + [TestCase("Milliseconds", InputPrimitiveTypeKind.Int64, false)] + [TestCase("Milliseconds", InputPrimitiveTypeKind.Float32, false)] + [TestCase("Milliseconds", InputPrimitiveTypeKind.Float64, false)] + [TestCase("Iso8601", null, true)] + [TestCase("Constant", null, true)] + [TestCase("Seconds", InputPrimitiveTypeKind.Int32, true)] + [TestCase("Seconds", InputPrimitiveTypeKind.Int64, true)] + [TestCase("Seconds", InputPrimitiveTypeKind.Float32, true)] + [TestCase("Seconds", InputPrimitiveTypeKind.Float64, true)] + [TestCase("Milliseconds", InputPrimitiveTypeKind.Int32, true)] + [TestCase("Milliseconds", InputPrimitiveTypeKind.Int64, true)] + [TestCase("Milliseconds", InputPrimitiveTypeKind.Float32, true)] + [TestCase("Milliseconds", InputPrimitiveTypeKind.Float64, true)] + public void PlainTextDurationReturnTypeMethods(string encoding, InputPrimitiveTypeKind? wireKind, bool isNullable) + { + DurationKnownEncoding durationEncoding = encoding switch + { + "Iso8601" => DurationKnownEncoding.Iso8601, + "Constant" => DurationKnownEncoding.Constant, + "Seconds" => DurationKnownEncoding.Seconds, + "Milliseconds" => DurationKnownEncoding.Milliseconds, + _ => throw new NotSupportedException() + }; + var wireType = wireKind is { } kind + ? new InputPrimitiveType(kind, kind.ToString().ToLowerInvariant(), $"TypeSpec.{kind.ToString().ToLowerInvariant()}") + : InputPrimitiveType.Int32; + InputType inputType = new InputDurationType(durationEncoding, "duration", "TypeSpec.duration", wireType, null); + if (isNullable) + { + inputType = new InputNullableType(inputType); + } + + var operation = InputFactory.Operation("GetPlainTextDuration", responses: + [InputFactory.OperationResponse([200], inputType, contentTypes: ["text/plain"])]); + var serviceMethod = InputFactory.BasicServiceMethod("GetPlainTextDuration", operation); + var inputClient = InputFactory.Client("TestClient", methods: [serviceMethod]); + + using var output = new MemoryStream(); + using var emitter = new Emitter(output); + var mockGenerator = MockHelpers.LoadMockGenerator(); + mockGenerator.SetupGet(p => p.Emitter).Returns(emitter); + var client = ScmCodeModelGenerator.Instance.TypeFactory.CreateClient(inputClient); + var method = new ScmMethodProviderCollection(serviceMethod, client!) + .Single(m => m.Kind == ScmMethodKind.Convenience && m.Signature.Name == "GetPlainTextDuration"); + + using var writer = new CodeWriter(); + writer.WriteMethod(method); + var baselineName = (wireKind is { } k ? $"{encoding}{k}" : encoding) + (isNullable ? "Nullable" : string.Empty); + Assert.AreEqual(Helpers.GetExpectedFromFile(baselineName), writer.ToString(false)); + + output.Position = 0; + using var reader = new StreamReader(output, Encoding.UTF8); + Assert.AreEqual(string.Empty, reader.ReadToEnd()); + } + + [Test] + public void PlainTextDurationReturnTypeMethodsReportsUnsupportedEncoding() + { + InputType inputType = new InputDurationType(new DurationKnownEncoding("Custom"), "duration", "TypeSpec.duration", InputPrimitiveType.Int32, null); + + var operation = InputFactory.Operation("GetPlainTextDuration", responses: + [InputFactory.OperationResponse([200], inputType, contentTypes: ["text/plain"])]); + var serviceMethod = InputFactory.BasicServiceMethod("GetPlainTextDuration", operation); + var inputClient = InputFactory.Client("TestClient", methods: [serviceMethod]); + + using var output = new MemoryStream(); + using var emitter = new Emitter(output); + var mockGenerator = MockHelpers.LoadMockGenerator(); + mockGenerator.SetupGet(p => p.Emitter).Returns(emitter); + var client = ScmCodeModelGenerator.Instance.TypeFactory.CreateClient(inputClient); + var methods = new ScmMethodProviderCollection(serviceMethod, client!); + var method = methods.Single(m => m.Kind == ScmMethodKind.Convenience && m.Signature.Name == "GetPlainTextDuration"); + + using var writer = new CodeWriter(); + writer.WriteMethod(method); + Assert.AreEqual(Helpers.GetExpectedFromFile("Custom"), writer.ToString(false)); + + output.Position = 0; + using var reader = new StreamReader(output, Encoding.UTF8); + StringAssert.Contains(@"""code"":""unsupported-serialization""", reader.ReadToEnd()); + } + + [TestCase(true, true, false)] + [TestCase(true, false, false)] + [TestCase(false, true, false)] + [TestCase(false, false, false)] + [TestCase(true, true, true)] + [TestCase(true, false, true)] + [TestCase(false, true, true)] + [TestCase(false, false, true)] + public void PlainTextEnumReturnTypeMethods(bool isString, bool isExtensible, bool isNullable) + { + InputType inputType = isString + ? InputFactory.StringEnum("TestEnum", [("Value", "value")], isExtensible: isExtensible) + : InputFactory.Int32Enum("TestEnum", [("Value", 1)], isExtensible: isExtensible); + if (isNullable) + { + inputType = new InputNullableType(inputType); + } + + var operation = InputFactory.Operation("GetPlainTextEnum", responses: + [InputFactory.OperationResponse([200], inputType, contentTypes: ["text/plain"])]); + var serviceMethod = InputFactory.BasicServiceMethod("GetPlainTextEnum", operation); + var inputClient = InputFactory.Client("TestClient", methods: [serviceMethod]); + + MockHelpers.LoadMockGenerator(); + var client = ScmCodeModelGenerator.Instance.TypeFactory.CreateClient(inputClient); + var method = new ScmMethodProviderCollection(serviceMethod, client!) + .Single(m => m.Kind == ScmMethodKind.Convenience && m.Signature.Name == "GetPlainTextEnum"); + + using var writer = new CodeWriter(); + writer.WriteMethod(method); + Assert.AreEqual(Helpers.GetExpectedFromFile($"{isString},{isExtensible},{isNullable}"), writer.ToString(false)); + } + + [TestCase("BinaryData")] + [TestCase("Model")] + [TestCase("List")] + [TestCase("Dictionary")] + public void PlainTextSpecialCaseResponsesPreserveExistingConversion(string kind) + { + // Raw binary, generated model and collection responses are not parsed from raw text even when text/plain + // is their only content type, they keep their existing conversion. + InputType inputType = kind switch + { + "BinaryData" => InputPrimitiveType.Any, + "Model" => InputFactory.Model("TestModel", properties: + [InputFactory.Property("name", InputPrimitiveType.String, isRequired: true)]), + "List" => InputFactory.Array(InputPrimitiveType.Int32), + "Dictionary" => InputFactory.Dictionary(InputPrimitiveType.Int32), + _ => throw new NotSupportedException() + }; + + var operation = InputFactory.Operation("GetSpecialCase", responses: + [InputFactory.OperationResponse([200], inputType, contentTypes: ["text/plain"])]); + var serviceMethod = InputFactory.BasicServiceMethod("GetSpecialCase", operation); + var inputClient = InputFactory.Client("TestClient", methods: [serviceMethod]); + + MockHelpers.LoadMockGenerator(); + var client = ScmCodeModelGenerator.Instance.TypeFactory.CreateClient(inputClient); + var method = new ScmMethodProviderCollection(serviceMethod, client!) + .Single(m => m.Kind == ScmMethodKind.Convenience && m.Signature.Name == "GetSpecialCase"); + + using var writer = new CodeWriter(); + writer.WriteMethod(method); + Assert.AreEqual(Helpers.GetExpectedFromFile(kind), writer.ToString(false)); + } + + [Test] + public void MixedContentTypeScalarResponseUsesJsonConversion() + { + // When a response declares text/plain alongside another content type (e.g. application/json), the + // wire format cannot be assumed to be raw text, so the JSON conversion path must be used instead. + var operation = InputFactory.Operation("GetScalar", responses: + [InputFactory.OperationResponse([200], InputPrimitiveType.Int32, contentTypes: ["application/json", "text/plain"])]); + var serviceMethod = InputFactory.BasicServiceMethod("GetScalar", operation); + var inputClient = InputFactory.Client("TestClient", methods: [serviceMethod]); + + MockHelpers.LoadMockGenerator(); + var client = ScmCodeModelGenerator.Instance.TypeFactory.CreateClient(inputClient); + var method = new ScmMethodProviderCollection(serviceMethod, client!) + .Single(m => m.Kind == ScmMethodKind.Convenience && m.Signature.Name == "GetScalar"); + + using var writer = new CodeWriter(); + writer.WriteMethod(method); + Assert.AreEqual(Helpers.GetExpectedFromFile(), writer.ToString(false)); } [Test] diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/EnumReturnTypeMethods(False,False,False).cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/EnumReturnTypeMethods(False,False,False).cs new file mode 100644 index 00000000000..832392216ab --- /dev/null +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/EnumReturnTypeMethods(False,False,False).cs @@ -0,0 +1,7 @@ +public virtual global::System.ClientModel.ClientResult GetEnum(global::System.Threading.CancellationToken cancellationToken = default) +{ + global::System.ClientModel.ClientResult result = this.GetEnum(cancellationToken.ToRequestOptions()); + using global::System.Text.Json.JsonDocument document = global::System.Text.Json.JsonDocument.Parse(result.GetRawResponse().Content); + global::Sample.Models.TestEnum value = document.RootElement.GetInt32().ToTestEnum(); + return global::System.ClientModel.ClientResult.FromValue(value, result.GetRawResponse()); +} diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/EnumReturnTypeMethods(False,False,True).cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/EnumReturnTypeMethods(False,False,True).cs new file mode 100644 index 00000000000..4ccce563a1c --- /dev/null +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/EnumReturnTypeMethods(False,False,True).cs @@ -0,0 +1,7 @@ +public virtual global::System.ClientModel.ClientResult GetEnum(global::System.Threading.CancellationToken cancellationToken = default) +{ + global::System.ClientModel.ClientResult result = this.GetEnum(cancellationToken.ToRequestOptions()); + using global::System.Text.Json.JsonDocument document = global::System.Text.Json.JsonDocument.Parse(result.GetRawResponse().Content); + global::Sample.Models.TestEnum? value = (document.RootElement.ValueKind == global::System.Text.Json.JsonValueKind.Null) ? ((global::Sample.Models.TestEnum?)null) : document.RootElement.GetInt32().ToTestEnum(); + return global::System.ClientModel.ClientResult.FromValue(value, result.GetRawResponse()); +} diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/EnumReturnTypeMethods(False,True,False).cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/EnumReturnTypeMethods(False,True,False).cs new file mode 100644 index 00000000000..bcad1492035 --- /dev/null +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/EnumReturnTypeMethods(False,True,False).cs @@ -0,0 +1,7 @@ +public virtual global::System.ClientModel.ClientResult GetEnum(global::System.Threading.CancellationToken cancellationToken = default) +{ + global::System.ClientModel.ClientResult result = this.GetEnum(cancellationToken.ToRequestOptions()); + using global::System.Text.Json.JsonDocument document = global::System.Text.Json.JsonDocument.Parse(result.GetRawResponse().Content); + global::Sample.Models.TestEnum value = new global::Sample.Models.TestEnum(document.RootElement.GetInt32()); + return global::System.ClientModel.ClientResult.FromValue(value, result.GetRawResponse()); +} diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/EnumReturnTypeMethods(False,True,True).cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/EnumReturnTypeMethods(False,True,True).cs new file mode 100644 index 00000000000..ef95fe3d8b8 --- /dev/null +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/EnumReturnTypeMethods(False,True,True).cs @@ -0,0 +1,7 @@ +public virtual global::System.ClientModel.ClientResult GetEnum(global::System.Threading.CancellationToken cancellationToken = default) +{ + global::System.ClientModel.ClientResult result = this.GetEnum(cancellationToken.ToRequestOptions()); + using global::System.Text.Json.JsonDocument document = global::System.Text.Json.JsonDocument.Parse(result.GetRawResponse().Content); + global::Sample.Models.TestEnum? value = (document.RootElement.ValueKind == global::System.Text.Json.JsonValueKind.Null) ? ((global::Sample.Models.TestEnum?)null) : new global::Sample.Models.TestEnum(document.RootElement.GetInt32()); + return global::System.ClientModel.ClientResult.FromValue(value, result.GetRawResponse()); +} diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/EnumReturnTypeMethods(True,False,False).cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/EnumReturnTypeMethods(True,False,False).cs new file mode 100644 index 00000000000..13e2c62aba1 --- /dev/null +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/EnumReturnTypeMethods(True,False,False).cs @@ -0,0 +1,7 @@ +public virtual global::System.ClientModel.ClientResult GetEnum(global::System.Threading.CancellationToken cancellationToken = default) +{ + global::System.ClientModel.ClientResult result = this.GetEnum(cancellationToken.ToRequestOptions()); + using global::System.Text.Json.JsonDocument document = global::System.Text.Json.JsonDocument.Parse(result.GetRawResponse().Content); + global::Sample.Models.TestEnum value = document.RootElement.GetString().ToTestEnum(); + return global::System.ClientModel.ClientResult.FromValue(value, result.GetRawResponse()); +} diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/EnumReturnTypeMethods(True,False,True).cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/EnumReturnTypeMethods(True,False,True).cs new file mode 100644 index 00000000000..a52f2c56f0a --- /dev/null +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/EnumReturnTypeMethods(True,False,True).cs @@ -0,0 +1,7 @@ +public virtual global::System.ClientModel.ClientResult GetEnum(global::System.Threading.CancellationToken cancellationToken = default) +{ + global::System.ClientModel.ClientResult result = this.GetEnum(cancellationToken.ToRequestOptions()); + using global::System.Text.Json.JsonDocument document = global::System.Text.Json.JsonDocument.Parse(result.GetRawResponse().Content); + global::Sample.Models.TestEnum? value = (document.RootElement.ValueKind == global::System.Text.Json.JsonValueKind.Null) ? ((global::Sample.Models.TestEnum?)null) : document.RootElement.GetString().ToTestEnum(); + return global::System.ClientModel.ClientResult.FromValue(value, result.GetRawResponse()); +} diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/EnumReturnTypeMethods(True,True,False).cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/EnumReturnTypeMethods(True,True,False).cs new file mode 100644 index 00000000000..29554182046 --- /dev/null +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/EnumReturnTypeMethods(True,True,False).cs @@ -0,0 +1,7 @@ +public virtual global::System.ClientModel.ClientResult GetEnum(global::System.Threading.CancellationToken cancellationToken = default) +{ + global::System.ClientModel.ClientResult result = this.GetEnum(cancellationToken.ToRequestOptions()); + using global::System.Text.Json.JsonDocument document = global::System.Text.Json.JsonDocument.Parse(result.GetRawResponse().Content); + global::Sample.Models.TestEnum value = new global::Sample.Models.TestEnum(document.RootElement.GetString()); + return global::System.ClientModel.ClientResult.FromValue(value, result.GetRawResponse()); +} diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/EnumReturnTypeMethods(True,True,True).cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/EnumReturnTypeMethods(True,True,True).cs new file mode 100644 index 00000000000..f90bec4edca --- /dev/null +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/EnumReturnTypeMethods(True,True,True).cs @@ -0,0 +1,7 @@ +public virtual global::System.ClientModel.ClientResult GetEnum(global::System.Threading.CancellationToken cancellationToken = default) +{ + global::System.ClientModel.ClientResult result = this.GetEnum(cancellationToken.ToRequestOptions()); + using global::System.Text.Json.JsonDocument document = global::System.Text.Json.JsonDocument.Parse(result.GetRawResponse().Content); + global::Sample.Models.TestEnum? value = (document.RootElement.ValueKind == global::System.Text.Json.JsonValueKind.Null) ? ((global::Sample.Models.TestEnum?)null) : new global::Sample.Models.TestEnum(document.RootElement.GetString()); + return global::System.ClientModel.ClientResult.FromValue(value, result.GetRawResponse()); +} diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/MixedContentTypeScalarResponseUsesJsonConversion.cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/MixedContentTypeScalarResponseUsesJsonConversion.cs new file mode 100644 index 00000000000..f74bcf62f77 --- /dev/null +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/MixedContentTypeScalarResponseUsesJsonConversion.cs @@ -0,0 +1,7 @@ +public virtual global::System.ClientModel.ClientResult GetScalar(global::System.Threading.CancellationToken cancellationToken = default) +{ + global::System.ClientModel.ClientResult result = this.GetScalar(cancellationToken.ToRequestOptions()); + using global::System.Text.Json.JsonDocument document = global::System.Text.Json.JsonDocument.Parse(result.GetRawResponse().Content); + int value = document.RootElement.GetInt32(); + return global::System.ClientModel.ClientResult.FromValue(value, result.GetRawResponse()); +} diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextDurationReturnTypeMethods(Constant).cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextDurationReturnTypeMethods(Constant).cs new file mode 100644 index 00000000000..31948d34930 --- /dev/null +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextDurationReturnTypeMethods(Constant).cs @@ -0,0 +1,6 @@ +public virtual global::System.ClientModel.ClientResult GetPlainTextDuration(global::System.Threading.CancellationToken cancellationToken = default) +{ + global::System.ClientModel.ClientResult result = this.GetPlainTextDuration(cancellationToken.ToRequestOptions()); + global::System.TimeSpan value = global::Sample.TypeFormatters.ParseTimeSpan(result.GetRawResponse().Content.ToString(), "c"); + return global::System.ClientModel.ClientResult.FromValue(value, result.GetRawResponse()); +} diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextDurationReturnTypeMethods(ConstantNullable).cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextDurationReturnTypeMethods(ConstantNullable).cs new file mode 100644 index 00000000000..4fd4de600a8 --- /dev/null +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextDurationReturnTypeMethods(ConstantNullable).cs @@ -0,0 +1,6 @@ +public virtual global::System.ClientModel.ClientResult GetPlainTextDuration(global::System.Threading.CancellationToken cancellationToken = default) +{ + global::System.ClientModel.ClientResult result = this.GetPlainTextDuration(cancellationToken.ToRequestOptions()); + global::System.TimeSpan? value = (result.GetRawResponse().Content.ToString().Trim() == "null") ? ((global::System.TimeSpan?)null) : global::Sample.TypeFormatters.ParseTimeSpan(result.GetRawResponse().Content.ToString(), "c"); + return global::System.ClientModel.ClientResult.FromValue(value, result.GetRawResponse()); +} diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextDurationReturnTypeMethods(Iso8601).cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextDurationReturnTypeMethods(Iso8601).cs new file mode 100644 index 00000000000..10cd828377f --- /dev/null +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextDurationReturnTypeMethods(Iso8601).cs @@ -0,0 +1,6 @@ +public virtual global::System.ClientModel.ClientResult GetPlainTextDuration(global::System.Threading.CancellationToken cancellationToken = default) +{ + global::System.ClientModel.ClientResult result = this.GetPlainTextDuration(cancellationToken.ToRequestOptions()); + global::System.TimeSpan value = global::Sample.TypeFormatters.ParseTimeSpan(result.GetRawResponse().Content.ToString(), "P"); + return global::System.ClientModel.ClientResult.FromValue(value, result.GetRawResponse()); +} diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextDurationReturnTypeMethods(Iso8601Nullable).cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextDurationReturnTypeMethods(Iso8601Nullable).cs new file mode 100644 index 00000000000..684057a69d7 --- /dev/null +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextDurationReturnTypeMethods(Iso8601Nullable).cs @@ -0,0 +1,6 @@ +public virtual global::System.ClientModel.ClientResult GetPlainTextDuration(global::System.Threading.CancellationToken cancellationToken = default) +{ + global::System.ClientModel.ClientResult result = this.GetPlainTextDuration(cancellationToken.ToRequestOptions()); + global::System.TimeSpan? value = (result.GetRawResponse().Content.ToString().Trim() == "null") ? ((global::System.TimeSpan?)null) : global::Sample.TypeFormatters.ParseTimeSpan(result.GetRawResponse().Content.ToString(), "P"); + return global::System.ClientModel.ClientResult.FromValue(value, result.GetRawResponse()); +} diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextDurationReturnTypeMethods(MillisecondsFloat32).cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextDurationReturnTypeMethods(MillisecondsFloat32).cs new file mode 100644 index 00000000000..8af31802799 --- /dev/null +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextDurationReturnTypeMethods(MillisecondsFloat32).cs @@ -0,0 +1,6 @@ +public virtual global::System.ClientModel.ClientResult GetPlainTextDuration(global::System.Threading.CancellationToken cancellationToken = default) +{ + global::System.ClientModel.ClientResult result = this.GetPlainTextDuration(cancellationToken.ToRequestOptions()); + global::System.TimeSpan value = global::System.TimeSpan.FromMilliseconds(double.Parse(result.GetRawResponse().Content.ToString(), global::System.Globalization.CultureInfo.InvariantCulture)); + return global::System.ClientModel.ClientResult.FromValue(value, result.GetRawResponse()); +} diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextDurationReturnTypeMethods(MillisecondsFloat32Nullable).cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextDurationReturnTypeMethods(MillisecondsFloat32Nullable).cs new file mode 100644 index 00000000000..8d3a055fd56 --- /dev/null +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextDurationReturnTypeMethods(MillisecondsFloat32Nullable).cs @@ -0,0 +1,6 @@ +public virtual global::System.ClientModel.ClientResult GetPlainTextDuration(global::System.Threading.CancellationToken cancellationToken = default) +{ + global::System.ClientModel.ClientResult result = this.GetPlainTextDuration(cancellationToken.ToRequestOptions()); + global::System.TimeSpan? value = (result.GetRawResponse().Content.ToString().Trim() == "null") ? ((global::System.TimeSpan?)null) : global::System.TimeSpan.FromMilliseconds(double.Parse(result.GetRawResponse().Content.ToString(), global::System.Globalization.CultureInfo.InvariantCulture)); + return global::System.ClientModel.ClientResult.FromValue(value, result.GetRawResponse()); +} diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextDurationReturnTypeMethods(MillisecondsFloat64).cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextDurationReturnTypeMethods(MillisecondsFloat64).cs new file mode 100644 index 00000000000..8af31802799 --- /dev/null +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextDurationReturnTypeMethods(MillisecondsFloat64).cs @@ -0,0 +1,6 @@ +public virtual global::System.ClientModel.ClientResult GetPlainTextDuration(global::System.Threading.CancellationToken cancellationToken = default) +{ + global::System.ClientModel.ClientResult result = this.GetPlainTextDuration(cancellationToken.ToRequestOptions()); + global::System.TimeSpan value = global::System.TimeSpan.FromMilliseconds(double.Parse(result.GetRawResponse().Content.ToString(), global::System.Globalization.CultureInfo.InvariantCulture)); + return global::System.ClientModel.ClientResult.FromValue(value, result.GetRawResponse()); +} diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextDurationReturnTypeMethods(MillisecondsFloat64Nullable).cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextDurationReturnTypeMethods(MillisecondsFloat64Nullable).cs new file mode 100644 index 00000000000..8d3a055fd56 --- /dev/null +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextDurationReturnTypeMethods(MillisecondsFloat64Nullable).cs @@ -0,0 +1,6 @@ +public virtual global::System.ClientModel.ClientResult GetPlainTextDuration(global::System.Threading.CancellationToken cancellationToken = default) +{ + global::System.ClientModel.ClientResult result = this.GetPlainTextDuration(cancellationToken.ToRequestOptions()); + global::System.TimeSpan? value = (result.GetRawResponse().Content.ToString().Trim() == "null") ? ((global::System.TimeSpan?)null) : global::System.TimeSpan.FromMilliseconds(double.Parse(result.GetRawResponse().Content.ToString(), global::System.Globalization.CultureInfo.InvariantCulture)); + return global::System.ClientModel.ClientResult.FromValue(value, result.GetRawResponse()); +} diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextDurationReturnTypeMethods(MillisecondsInt32).cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextDurationReturnTypeMethods(MillisecondsInt32).cs new file mode 100644 index 00000000000..ea0374f921c --- /dev/null +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextDurationReturnTypeMethods(MillisecondsInt32).cs @@ -0,0 +1,6 @@ +public virtual global::System.ClientModel.ClientResult GetPlainTextDuration(global::System.Threading.CancellationToken cancellationToken = default) +{ + global::System.ClientModel.ClientResult result = this.GetPlainTextDuration(cancellationToken.ToRequestOptions()); + global::System.TimeSpan value = global::System.TimeSpan.FromMilliseconds(int.Parse(result.GetRawResponse().Content.ToString(), global::System.Globalization.CultureInfo.InvariantCulture)); + return global::System.ClientModel.ClientResult.FromValue(value, result.GetRawResponse()); +} diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextDurationReturnTypeMethods(MillisecondsInt32Nullable).cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextDurationReturnTypeMethods(MillisecondsInt32Nullable).cs new file mode 100644 index 00000000000..836f9ff9e8a --- /dev/null +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextDurationReturnTypeMethods(MillisecondsInt32Nullable).cs @@ -0,0 +1,6 @@ +public virtual global::System.ClientModel.ClientResult GetPlainTextDuration(global::System.Threading.CancellationToken cancellationToken = default) +{ + global::System.ClientModel.ClientResult result = this.GetPlainTextDuration(cancellationToken.ToRequestOptions()); + global::System.TimeSpan? value = (result.GetRawResponse().Content.ToString().Trim() == "null") ? ((global::System.TimeSpan?)null) : global::System.TimeSpan.FromMilliseconds(int.Parse(result.GetRawResponse().Content.ToString(), global::System.Globalization.CultureInfo.InvariantCulture)); + return global::System.ClientModel.ClientResult.FromValue(value, result.GetRawResponse()); +} diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextDurationReturnTypeMethods(MillisecondsInt64).cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextDurationReturnTypeMethods(MillisecondsInt64).cs new file mode 100644 index 00000000000..f267d6110c6 --- /dev/null +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextDurationReturnTypeMethods(MillisecondsInt64).cs @@ -0,0 +1,6 @@ +public virtual global::System.ClientModel.ClientResult GetPlainTextDuration(global::System.Threading.CancellationToken cancellationToken = default) +{ + global::System.ClientModel.ClientResult result = this.GetPlainTextDuration(cancellationToken.ToRequestOptions()); + global::System.TimeSpan value = global::System.TimeSpan.FromMilliseconds(long.Parse(result.GetRawResponse().Content.ToString(), global::System.Globalization.CultureInfo.InvariantCulture)); + return global::System.ClientModel.ClientResult.FromValue(value, result.GetRawResponse()); +} diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextDurationReturnTypeMethods(MillisecondsInt64Nullable).cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextDurationReturnTypeMethods(MillisecondsInt64Nullable).cs new file mode 100644 index 00000000000..633fefa5671 --- /dev/null +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextDurationReturnTypeMethods(MillisecondsInt64Nullable).cs @@ -0,0 +1,6 @@ +public virtual global::System.ClientModel.ClientResult GetPlainTextDuration(global::System.Threading.CancellationToken cancellationToken = default) +{ + global::System.ClientModel.ClientResult result = this.GetPlainTextDuration(cancellationToken.ToRequestOptions()); + global::System.TimeSpan? value = (result.GetRawResponse().Content.ToString().Trim() == "null") ? ((global::System.TimeSpan?)null) : global::System.TimeSpan.FromMilliseconds(long.Parse(result.GetRawResponse().Content.ToString(), global::System.Globalization.CultureInfo.InvariantCulture)); + return global::System.ClientModel.ClientResult.FromValue(value, result.GetRawResponse()); +} diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextDurationReturnTypeMethods(SecondsFloat32).cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextDurationReturnTypeMethods(SecondsFloat32).cs new file mode 100644 index 00000000000..2a40fa9746d --- /dev/null +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextDurationReturnTypeMethods(SecondsFloat32).cs @@ -0,0 +1,6 @@ +public virtual global::System.ClientModel.ClientResult GetPlainTextDuration(global::System.Threading.CancellationToken cancellationToken = default) +{ + global::System.ClientModel.ClientResult result = this.GetPlainTextDuration(cancellationToken.ToRequestOptions()); + global::System.TimeSpan value = global::System.TimeSpan.FromSeconds(double.Parse(result.GetRawResponse().Content.ToString(), global::System.Globalization.CultureInfo.InvariantCulture)); + return global::System.ClientModel.ClientResult.FromValue(value, result.GetRawResponse()); +} diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextDurationReturnTypeMethods(SecondsFloat32Nullable).cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextDurationReturnTypeMethods(SecondsFloat32Nullable).cs new file mode 100644 index 00000000000..59035730cd0 --- /dev/null +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextDurationReturnTypeMethods(SecondsFloat32Nullable).cs @@ -0,0 +1,6 @@ +public virtual global::System.ClientModel.ClientResult GetPlainTextDuration(global::System.Threading.CancellationToken cancellationToken = default) +{ + global::System.ClientModel.ClientResult result = this.GetPlainTextDuration(cancellationToken.ToRequestOptions()); + global::System.TimeSpan? value = (result.GetRawResponse().Content.ToString().Trim() == "null") ? ((global::System.TimeSpan?)null) : global::System.TimeSpan.FromSeconds(double.Parse(result.GetRawResponse().Content.ToString(), global::System.Globalization.CultureInfo.InvariantCulture)); + return global::System.ClientModel.ClientResult.FromValue(value, result.GetRawResponse()); +} diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextDurationReturnTypeMethods(SecondsFloat64).cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextDurationReturnTypeMethods(SecondsFloat64).cs new file mode 100644 index 00000000000..2a40fa9746d --- /dev/null +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextDurationReturnTypeMethods(SecondsFloat64).cs @@ -0,0 +1,6 @@ +public virtual global::System.ClientModel.ClientResult GetPlainTextDuration(global::System.Threading.CancellationToken cancellationToken = default) +{ + global::System.ClientModel.ClientResult result = this.GetPlainTextDuration(cancellationToken.ToRequestOptions()); + global::System.TimeSpan value = global::System.TimeSpan.FromSeconds(double.Parse(result.GetRawResponse().Content.ToString(), global::System.Globalization.CultureInfo.InvariantCulture)); + return global::System.ClientModel.ClientResult.FromValue(value, result.GetRawResponse()); +} diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextDurationReturnTypeMethods(SecondsFloat64Nullable).cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextDurationReturnTypeMethods(SecondsFloat64Nullable).cs new file mode 100644 index 00000000000..59035730cd0 --- /dev/null +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextDurationReturnTypeMethods(SecondsFloat64Nullable).cs @@ -0,0 +1,6 @@ +public virtual global::System.ClientModel.ClientResult GetPlainTextDuration(global::System.Threading.CancellationToken cancellationToken = default) +{ + global::System.ClientModel.ClientResult result = this.GetPlainTextDuration(cancellationToken.ToRequestOptions()); + global::System.TimeSpan? value = (result.GetRawResponse().Content.ToString().Trim() == "null") ? ((global::System.TimeSpan?)null) : global::System.TimeSpan.FromSeconds(double.Parse(result.GetRawResponse().Content.ToString(), global::System.Globalization.CultureInfo.InvariantCulture)); + return global::System.ClientModel.ClientResult.FromValue(value, result.GetRawResponse()); +} diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextDurationReturnTypeMethods(SecondsInt32).cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextDurationReturnTypeMethods(SecondsInt32).cs new file mode 100644 index 00000000000..b266a987ede --- /dev/null +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextDurationReturnTypeMethods(SecondsInt32).cs @@ -0,0 +1,6 @@ +public virtual global::System.ClientModel.ClientResult GetPlainTextDuration(global::System.Threading.CancellationToken cancellationToken = default) +{ + global::System.ClientModel.ClientResult result = this.GetPlainTextDuration(cancellationToken.ToRequestOptions()); + global::System.TimeSpan value = global::System.TimeSpan.FromSeconds(int.Parse(result.GetRawResponse().Content.ToString(), global::System.Globalization.CultureInfo.InvariantCulture)); + return global::System.ClientModel.ClientResult.FromValue(value, result.GetRawResponse()); +} diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextDurationReturnTypeMethods(SecondsInt32Nullable).cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextDurationReturnTypeMethods(SecondsInt32Nullable).cs new file mode 100644 index 00000000000..085552346c9 --- /dev/null +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextDurationReturnTypeMethods(SecondsInt32Nullable).cs @@ -0,0 +1,6 @@ +public virtual global::System.ClientModel.ClientResult GetPlainTextDuration(global::System.Threading.CancellationToken cancellationToken = default) +{ + global::System.ClientModel.ClientResult result = this.GetPlainTextDuration(cancellationToken.ToRequestOptions()); + global::System.TimeSpan? value = (result.GetRawResponse().Content.ToString().Trim() == "null") ? ((global::System.TimeSpan?)null) : global::System.TimeSpan.FromSeconds(int.Parse(result.GetRawResponse().Content.ToString(), global::System.Globalization.CultureInfo.InvariantCulture)); + return global::System.ClientModel.ClientResult.FromValue(value, result.GetRawResponse()); +} diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextDurationReturnTypeMethods(SecondsInt64).cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextDurationReturnTypeMethods(SecondsInt64).cs new file mode 100644 index 00000000000..1141f80e2a9 --- /dev/null +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextDurationReturnTypeMethods(SecondsInt64).cs @@ -0,0 +1,6 @@ +public virtual global::System.ClientModel.ClientResult GetPlainTextDuration(global::System.Threading.CancellationToken cancellationToken = default) +{ + global::System.ClientModel.ClientResult result = this.GetPlainTextDuration(cancellationToken.ToRequestOptions()); + global::System.TimeSpan value = global::System.TimeSpan.FromSeconds(long.Parse(result.GetRawResponse().Content.ToString(), global::System.Globalization.CultureInfo.InvariantCulture)); + return global::System.ClientModel.ClientResult.FromValue(value, result.GetRawResponse()); +} diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextDurationReturnTypeMethods(SecondsInt64Nullable).cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextDurationReturnTypeMethods(SecondsInt64Nullable).cs new file mode 100644 index 00000000000..fba9b6ecf21 --- /dev/null +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextDurationReturnTypeMethods(SecondsInt64Nullable).cs @@ -0,0 +1,6 @@ +public virtual global::System.ClientModel.ClientResult GetPlainTextDuration(global::System.Threading.CancellationToken cancellationToken = default) +{ + global::System.ClientModel.ClientResult result = this.GetPlainTextDuration(cancellationToken.ToRequestOptions()); + global::System.TimeSpan? value = (result.GetRawResponse().Content.ToString().Trim() == "null") ? ((global::System.TimeSpan?)null) : global::System.TimeSpan.FromSeconds(long.Parse(result.GetRawResponse().Content.ToString(), global::System.Globalization.CultureInfo.InvariantCulture)); + return global::System.ClientModel.ClientResult.FromValue(value, result.GetRawResponse()); +} diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextDurationReturnTypeMethodsReportsUnsupportedEncoding(Custom).cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextDurationReturnTypeMethodsReportsUnsupportedEncoding(Custom).cs new file mode 100644 index 00000000000..31948d34930 --- /dev/null +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextDurationReturnTypeMethodsReportsUnsupportedEncoding(Custom).cs @@ -0,0 +1,6 @@ +public virtual global::System.ClientModel.ClientResult GetPlainTextDuration(global::System.Threading.CancellationToken cancellationToken = default) +{ + global::System.ClientModel.ClientResult result = this.GetPlainTextDuration(cancellationToken.ToRequestOptions()); + global::System.TimeSpan value = global::Sample.TypeFormatters.ParseTimeSpan(result.GetRawResponse().Content.ToString(), "c"); + return global::System.ClientModel.ClientResult.FromValue(value, result.GetRawResponse()); +} diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextEnumReturnTypeMethods(False,False,False).cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextEnumReturnTypeMethods(False,False,False).cs new file mode 100644 index 00000000000..16f1513d957 --- /dev/null +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextEnumReturnTypeMethods(False,False,False).cs @@ -0,0 +1,6 @@ +public virtual global::System.ClientModel.ClientResult GetPlainTextEnum(global::System.Threading.CancellationToken cancellationToken = default) +{ + global::System.ClientModel.ClientResult result = this.GetPlainTextEnum(cancellationToken.ToRequestOptions()); + global::Sample.Models.TestEnum value = int.Parse(result.GetRawResponse().Content.ToString(), global::System.Globalization.CultureInfo.InvariantCulture).ToTestEnum(); + return global::System.ClientModel.ClientResult.FromValue(value, result.GetRawResponse()); +} diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextEnumReturnTypeMethods(False,False,True).cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextEnumReturnTypeMethods(False,False,True).cs new file mode 100644 index 00000000000..2261133f28a --- /dev/null +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextEnumReturnTypeMethods(False,False,True).cs @@ -0,0 +1,6 @@ +public virtual global::System.ClientModel.ClientResult GetPlainTextEnum(global::System.Threading.CancellationToken cancellationToken = default) +{ + global::System.ClientModel.ClientResult result = this.GetPlainTextEnum(cancellationToken.ToRequestOptions()); + global::Sample.Models.TestEnum? value = (result.GetRawResponse().Content.ToString().Trim() == "null") ? ((global::Sample.Models.TestEnum?)null) : int.Parse(result.GetRawResponse().Content.ToString(), global::System.Globalization.CultureInfo.InvariantCulture).ToTestEnum(); + return global::System.ClientModel.ClientResult.FromValue(value, result.GetRawResponse()); +} diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextEnumReturnTypeMethods(False,True,False).cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextEnumReturnTypeMethods(False,True,False).cs new file mode 100644 index 00000000000..cffbb91ac8a --- /dev/null +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextEnumReturnTypeMethods(False,True,False).cs @@ -0,0 +1,6 @@ +public virtual global::System.ClientModel.ClientResult GetPlainTextEnum(global::System.Threading.CancellationToken cancellationToken = default) +{ + global::System.ClientModel.ClientResult result = this.GetPlainTextEnum(cancellationToken.ToRequestOptions()); + global::Sample.Models.TestEnum value = new global::Sample.Models.TestEnum(int.Parse(result.GetRawResponse().Content.ToString(), global::System.Globalization.CultureInfo.InvariantCulture)); + return global::System.ClientModel.ClientResult.FromValue(value, result.GetRawResponse()); +} diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextEnumReturnTypeMethods(False,True,True).cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextEnumReturnTypeMethods(False,True,True).cs new file mode 100644 index 00000000000..2287c802558 --- /dev/null +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextEnumReturnTypeMethods(False,True,True).cs @@ -0,0 +1,6 @@ +public virtual global::System.ClientModel.ClientResult GetPlainTextEnum(global::System.Threading.CancellationToken cancellationToken = default) +{ + global::System.ClientModel.ClientResult result = this.GetPlainTextEnum(cancellationToken.ToRequestOptions()); + global::Sample.Models.TestEnum? value = (result.GetRawResponse().Content.ToString().Trim() == "null") ? ((global::Sample.Models.TestEnum?)null) : new global::Sample.Models.TestEnum(int.Parse(result.GetRawResponse().Content.ToString(), global::System.Globalization.CultureInfo.InvariantCulture)); + return global::System.ClientModel.ClientResult.FromValue(value, result.GetRawResponse()); +} diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextEnumReturnTypeMethods(True,False,False).cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextEnumReturnTypeMethods(True,False,False).cs new file mode 100644 index 00000000000..055da862dae --- /dev/null +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextEnumReturnTypeMethods(True,False,False).cs @@ -0,0 +1,6 @@ +public virtual global::System.ClientModel.ClientResult GetPlainTextEnum(global::System.Threading.CancellationToken cancellationToken = default) +{ + global::System.ClientModel.ClientResult result = this.GetPlainTextEnum(cancellationToken.ToRequestOptions()); + global::Sample.Models.TestEnum value = result.GetRawResponse().Content.ToString().ToTestEnum(); + return global::System.ClientModel.ClientResult.FromValue(value, result.GetRawResponse()); +} diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextEnumReturnTypeMethods(True,False,True).cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextEnumReturnTypeMethods(True,False,True).cs new file mode 100644 index 00000000000..66cbe3bd949 --- /dev/null +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextEnumReturnTypeMethods(True,False,True).cs @@ -0,0 +1,6 @@ +public virtual global::System.ClientModel.ClientResult GetPlainTextEnum(global::System.Threading.CancellationToken cancellationToken = default) +{ + global::System.ClientModel.ClientResult result = this.GetPlainTextEnum(cancellationToken.ToRequestOptions()); + global::Sample.Models.TestEnum? value = (result.GetRawResponse().Content.ToString().Trim() == "null") ? ((global::Sample.Models.TestEnum?)null) : result.GetRawResponse().Content.ToString().ToTestEnum(); + return global::System.ClientModel.ClientResult.FromValue(value, result.GetRawResponse()); +} diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextEnumReturnTypeMethods(True,True,False).cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextEnumReturnTypeMethods(True,True,False).cs new file mode 100644 index 00000000000..c7346c5252b --- /dev/null +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextEnumReturnTypeMethods(True,True,False).cs @@ -0,0 +1,6 @@ +public virtual global::System.ClientModel.ClientResult GetPlainTextEnum(global::System.Threading.CancellationToken cancellationToken = default) +{ + global::System.ClientModel.ClientResult result = this.GetPlainTextEnum(cancellationToken.ToRequestOptions()); + global::Sample.Models.TestEnum value = new global::Sample.Models.TestEnum(result.GetRawResponse().Content.ToString()); + return global::System.ClientModel.ClientResult.FromValue(value, result.GetRawResponse()); +} diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextEnumReturnTypeMethods(True,True,True).cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextEnumReturnTypeMethods(True,True,True).cs new file mode 100644 index 00000000000..c1461d8a36b --- /dev/null +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextEnumReturnTypeMethods(True,True,True).cs @@ -0,0 +1,6 @@ +public virtual global::System.ClientModel.ClientResult GetPlainTextEnum(global::System.Threading.CancellationToken cancellationToken = default) +{ + global::System.ClientModel.ClientResult result = this.GetPlainTextEnum(cancellationToken.ToRequestOptions()); + global::Sample.Models.TestEnum? value = (result.GetRawResponse().Content.ToString().Trim() == "null") ? ((global::Sample.Models.TestEnum?)null) : new global::Sample.Models.TestEnum(result.GetRawResponse().Content.ToString()); + return global::System.ClientModel.ClientResult.FromValue(value, result.GetRawResponse()); +} diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextReturnTypeMethods.cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextReturnTypeMethods.cs new file mode 100644 index 00000000000..95920e5f6ba --- /dev/null +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextReturnTypeMethods.cs @@ -0,0 +1,5 @@ +public virtual global::System.ClientModel.ClientResult GetText(global::System.Threading.CancellationToken cancellationToken = default) +{ + global::System.ClientModel.ClientResult result = this.GetText(cancellationToken.ToRequestOptions()); + return global::System.ClientModel.ClientResult.FromValue(result.GetRawResponse().Content.ToString(), result.GetRawResponse()); +} diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextScalarReturnTypeMethods(Boolean).cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextScalarReturnTypeMethods(Boolean).cs new file mode 100644 index 00000000000..7f3da3146bb --- /dev/null +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextScalarReturnTypeMethods(Boolean).cs @@ -0,0 +1,6 @@ +public virtual global::System.ClientModel.ClientResult GetPlainTextScalar(global::System.Threading.CancellationToken cancellationToken = default) +{ + global::System.ClientModel.ClientResult result = this.GetPlainTextScalar(cancellationToken.ToRequestOptions()); + bool value = bool.Parse(result.GetRawResponse().Content.ToString()); + return global::System.ClientModel.ClientResult.FromValue(value, result.GetRawResponse()); +} diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextScalarReturnTypeMethods(BooleanNullable).cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextScalarReturnTypeMethods(BooleanNullable).cs new file mode 100644 index 00000000000..eaa8edcdbfe --- /dev/null +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextScalarReturnTypeMethods(BooleanNullable).cs @@ -0,0 +1,6 @@ +public virtual global::System.ClientModel.ClientResult GetPlainTextScalar(global::System.Threading.CancellationToken cancellationToken = default) +{ + global::System.ClientModel.ClientResult result = this.GetPlainTextScalar(cancellationToken.ToRequestOptions()); + bool? value = (result.GetRawResponse().Content.ToString().Trim() == "null") ? ((bool?)null) : bool.Parse(result.GetRawResponse().Content.ToString()); + return global::System.ClientModel.ClientResult.FromValue(value, result.GetRawResponse()); +} diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextScalarReturnTypeMethods(Byte).cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextScalarReturnTypeMethods(Byte).cs new file mode 100644 index 00000000000..74cb6f79e6e --- /dev/null +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextScalarReturnTypeMethods(Byte).cs @@ -0,0 +1,6 @@ +public virtual global::System.ClientModel.ClientResult GetPlainTextScalar(global::System.Threading.CancellationToken cancellationToken = default) +{ + global::System.ClientModel.ClientResult result = this.GetPlainTextScalar(cancellationToken.ToRequestOptions()); + byte value = byte.Parse(result.GetRawResponse().Content.ToString(), global::System.Globalization.CultureInfo.InvariantCulture); + return global::System.ClientModel.ClientResult.FromValue(value, result.GetRawResponse()); +} diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextScalarReturnTypeMethods(DateTimeOffset).cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextScalarReturnTypeMethods(DateTimeOffset).cs new file mode 100644 index 00000000000..216026abda2 --- /dev/null +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextScalarReturnTypeMethods(DateTimeOffset).cs @@ -0,0 +1,6 @@ +public virtual global::System.ClientModel.ClientResult GetPlainTextScalar(global::System.Threading.CancellationToken cancellationToken = default) +{ + global::System.ClientModel.ClientResult result = this.GetPlainTextScalar(cancellationToken.ToRequestOptions()); + global::System.DateTimeOffset value = global::Sample.TypeFormatters.ParseDateTimeOffset(result.GetRawResponse().Content.ToString(), "D"); + return global::System.ClientModel.ClientResult.FromValue(value, result.GetRawResponse()); +} diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextScalarReturnTypeMethods(Int32).cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextScalarReturnTypeMethods(Int32).cs new file mode 100644 index 00000000000..13d3ccecf4d --- /dev/null +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextScalarReturnTypeMethods(Int32).cs @@ -0,0 +1,6 @@ +public virtual global::System.ClientModel.ClientResult GetPlainTextScalar(global::System.Threading.CancellationToken cancellationToken = default) +{ + global::System.ClientModel.ClientResult result = this.GetPlainTextScalar(cancellationToken.ToRequestOptions()); + int value = int.Parse(result.GetRawResponse().Content.ToString(), global::System.Globalization.CultureInfo.InvariantCulture); + return global::System.ClientModel.ClientResult.FromValue(value, result.GetRawResponse()); +} diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextScalarReturnTypeMethods(Int32Nullable).cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextScalarReturnTypeMethods(Int32Nullable).cs new file mode 100644 index 00000000000..2b632b0d293 --- /dev/null +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextScalarReturnTypeMethods(Int32Nullable).cs @@ -0,0 +1,6 @@ +public virtual global::System.ClientModel.ClientResult GetPlainTextScalar(global::System.Threading.CancellationToken cancellationToken = default) +{ + global::System.ClientModel.ClientResult result = this.GetPlainTextScalar(cancellationToken.ToRequestOptions()); + int? value = (result.GetRawResponse().Content.ToString().Trim() == "null") ? ((int?)null) : int.Parse(result.GetRawResponse().Content.ToString(), global::System.Globalization.CultureInfo.InvariantCulture); + return global::System.ClientModel.ClientResult.FromValue(value, result.GetRawResponse()); +} diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextScalarReturnTypeMethods(SByte).cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextScalarReturnTypeMethods(SByte).cs new file mode 100644 index 00000000000..52064786a8c --- /dev/null +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextScalarReturnTypeMethods(SByte).cs @@ -0,0 +1,6 @@ +public virtual global::System.ClientModel.ClientResult GetPlainTextScalar(global::System.Threading.CancellationToken cancellationToken = default) +{ + global::System.ClientModel.ClientResult result = this.GetPlainTextScalar(cancellationToken.ToRequestOptions()); + sbyte value = sbyte.Parse(result.GetRawResponse().Content.ToString(), global::System.Globalization.CultureInfo.InvariantCulture); + return global::System.ClientModel.ClientResult.FromValue(value, result.GetRawResponse()); +} diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextScalarReturnTypeMethods(TimeSpan).cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextScalarReturnTypeMethods(TimeSpan).cs new file mode 100644 index 00000000000..7fa72748f9f --- /dev/null +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextScalarReturnTypeMethods(TimeSpan).cs @@ -0,0 +1,6 @@ +public virtual global::System.ClientModel.ClientResult GetPlainTextScalar(global::System.Threading.CancellationToken cancellationToken = default) +{ + global::System.ClientModel.ClientResult result = this.GetPlainTextScalar(cancellationToken.ToRequestOptions()); + global::System.TimeSpan value = global::Sample.TypeFormatters.ParseTimeSpan(result.GetRawResponse().Content.ToString(), "T"); + return global::System.ClientModel.ClientResult.FromValue(value, result.GetRawResponse()); +} diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextScalarReturnTypeMethods(TimeSpanNullable).cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextScalarReturnTypeMethods(TimeSpanNullable).cs new file mode 100644 index 00000000000..5c02cd85b6b --- /dev/null +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextScalarReturnTypeMethods(TimeSpanNullable).cs @@ -0,0 +1,6 @@ +public virtual global::System.ClientModel.ClientResult GetPlainTextScalar(global::System.Threading.CancellationToken cancellationToken = default) +{ + global::System.ClientModel.ClientResult result = this.GetPlainTextScalar(cancellationToken.ToRequestOptions()); + global::System.TimeSpan? value = (result.GetRawResponse().Content.ToString().Trim() == "null") ? ((global::System.TimeSpan?)null) : global::Sample.TypeFormatters.ParseTimeSpan(result.GetRawResponse().Content.ToString(), "T"); + return global::System.ClientModel.ClientResult.FromValue(value, result.GetRawResponse()); +} diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextScalarReturnTypeMethods(Uri).cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextScalarReturnTypeMethods(Uri).cs new file mode 100644 index 00000000000..300e3802ab8 --- /dev/null +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextScalarReturnTypeMethods(Uri).cs @@ -0,0 +1,6 @@ +public virtual global::System.ClientModel.ClientResult GetPlainTextScalar(global::System.Threading.CancellationToken cancellationToken = default) +{ + global::System.ClientModel.ClientResult result = this.GetPlainTextScalar(cancellationToken.ToRequestOptions()); + global::System.Uri value = new global::System.Uri(result.GetRawResponse().Content.ToString(), global::System.UriKind.RelativeOrAbsolute); + return global::System.ClientModel.ClientResult.FromValue(value, result.GetRawResponse()); +} diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextSpecialCaseResponsesPreserveExistingConversion(BinaryData).cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextSpecialCaseResponsesPreserveExistingConversion(BinaryData).cs new file mode 100644 index 00000000000..887f2fc78ac --- /dev/null +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextSpecialCaseResponsesPreserveExistingConversion(BinaryData).cs @@ -0,0 +1,5 @@ +public virtual global::System.ClientModel.ClientResult GetSpecialCase(global::System.Threading.CancellationToken cancellationToken = default) +{ + global::System.ClientModel.ClientResult result = this.GetSpecialCase(cancellationToken.ToRequestOptions()); + return global::System.ClientModel.ClientResult.FromValue(result.GetRawResponse().Content, result.GetRawResponse()); +} diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextSpecialCaseResponsesPreserveExistingConversion(Dictionary).cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextSpecialCaseResponsesPreserveExistingConversion(Dictionary).cs new file mode 100644 index 00000000000..38cd0036aa4 --- /dev/null +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextSpecialCaseResponsesPreserveExistingConversion(Dictionary).cs @@ -0,0 +1,19 @@ +public virtual global::System.ClientModel.ClientResult> GetSpecialCase(global::System.Threading.CancellationToken cancellationToken = default) +{ + global::System.ClientModel.ClientResult result = this.GetSpecialCase(cancellationToken.ToRequestOptions()); + global::System.Collections.Generic.IDictionary value = new global::System.Collections.Generic.Dictionary(); + 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.GetInt32()); + } + return global::System.ClientModel.ClientResult.FromValue(((global::System.Collections.Generic.IReadOnlyDictionary)value), result.GetRawResponse()); +} diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextSpecialCaseResponsesPreserveExistingConversion(List).cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextSpecialCaseResponsesPreserveExistingConversion(List).cs new file mode 100644 index 00000000000..e5c154635df --- /dev/null +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextSpecialCaseResponsesPreserveExistingConversion(List).cs @@ -0,0 +1,17 @@ +public virtual global::System.ClientModel.ClientResult> GetSpecialCase(global::System.Threading.CancellationToken cancellationToken = default) +{ + global::System.ClientModel.ClientResult result = this.GetSpecialCase(cancellationToken.ToRequestOptions()); + global::System.Collections.Generic.List value = new global::System.Collections.Generic.List(); + 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.GetInt32()); + } + return global::System.ClientModel.ClientResult.FromValue(((global::System.Collections.Generic.IReadOnlyList)value), result.GetRawResponse()); +} diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextSpecialCaseResponsesPreserveExistingConversion(Model).cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextSpecialCaseResponsesPreserveExistingConversion(Model).cs new file mode 100644 index 00000000000..bd099dc8b94 --- /dev/null +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/PlainTextSpecialCaseResponsesPreserveExistingConversion(Model).cs @@ -0,0 +1,5 @@ +public virtual global::System.ClientModel.ClientResult GetSpecialCase(global::System.Threading.CancellationToken cancellationToken = default) +{ + global::System.ClientModel.ClientResult result = this.GetSpecialCase(cancellationToken.ToRequestOptions()); + return global::System.ClientModel.ClientResult.FromValue(((global::Sample.Models.TestModel)result), result.GetRawResponse()); +} diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/ScalarReturnTypeMethods(BinaryData).cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/ScalarReturnTypeMethods(BinaryData).cs index efcc1bbf33a..5069a349ea3 100644 --- a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/ScalarReturnTypeMethods(BinaryData).cs +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/ScalarReturnTypeMethods(BinaryData).cs @@ -1,2 +1,5 @@ -global::System.ClientModel.ClientResult result = this.GetScalar(cancellationToken.ToRequestOptions()); -return global::System.ClientModel.ClientResult.FromValue(result.GetRawResponse().Content, result.GetRawResponse()); +public virtual global::System.ClientModel.ClientResult GetScalar(global::System.Threading.CancellationToken cancellationToken = default) +{ + global::System.ClientModel.ClientResult result = this.GetScalar(cancellationToken.ToRequestOptions()); + return global::System.ClientModel.ClientResult.FromValue(result.GetRawResponse().Content, result.GetRawResponse()); +} diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/ScalarReturnTypeMethods(Boolean).cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/ScalarReturnTypeMethods(Boolean).cs index a418e708844..f63132d6c5b 100644 --- a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/ScalarReturnTypeMethods(Boolean).cs +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/ScalarReturnTypeMethods(Boolean).cs @@ -1,2 +1,7 @@ -global::System.ClientModel.ClientResult result = this.GetScalar(cancellationToken.ToRequestOptions()); -return global::System.ClientModel.ClientResult.FromValue(result.GetRawResponse().Content.ToObjectFromJson(), result.GetRawResponse()); +public virtual global::System.ClientModel.ClientResult GetScalar(global::System.Threading.CancellationToken cancellationToken = default) +{ + global::System.ClientModel.ClientResult result = this.GetScalar(cancellationToken.ToRequestOptions()); + using global::System.Text.Json.JsonDocument document = global::System.Text.Json.JsonDocument.Parse(result.GetRawResponse().Content); + bool value = document.RootElement.GetBoolean(); + return global::System.ClientModel.ClientResult.FromValue(value, result.GetRawResponse()); +} diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/ScalarReturnTypeMethods(BooleanNullable).cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/ScalarReturnTypeMethods(BooleanNullable).cs new file mode 100644 index 00000000000..ea4ec45fb30 --- /dev/null +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/ScalarReturnTypeMethods(BooleanNullable).cs @@ -0,0 +1,7 @@ +public virtual global::System.ClientModel.ClientResult GetScalar(global::System.Threading.CancellationToken cancellationToken = default) +{ + global::System.ClientModel.ClientResult result = this.GetScalar(cancellationToken.ToRequestOptions()); + using global::System.Text.Json.JsonDocument document = global::System.Text.Json.JsonDocument.Parse(result.GetRawResponse().Content); + bool? value = (document.RootElement.ValueKind == global::System.Text.Json.JsonValueKind.Null) ? ((bool?)null) : document.RootElement.GetBoolean(); + return global::System.ClientModel.ClientResult.FromValue(value, result.GetRawResponse()); +} diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/ScalarReturnTypeMethods(Byte).cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/ScalarReturnTypeMethods(Byte).cs new file mode 100644 index 00000000000..547644c070e --- /dev/null +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/ScalarReturnTypeMethods(Byte).cs @@ -0,0 +1,7 @@ +public virtual global::System.ClientModel.ClientResult GetScalar(global::System.Threading.CancellationToken cancellationToken = default) +{ + global::System.ClientModel.ClientResult result = this.GetScalar(cancellationToken.ToRequestOptions()); + using global::System.Text.Json.JsonDocument document = global::System.Text.Json.JsonDocument.Parse(result.GetRawResponse().Content); + byte value = document.RootElement.GetByte(); + return global::System.ClientModel.ClientResult.FromValue(value, result.GetRawResponse()); +} diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/ScalarReturnTypeMethods(DateTimeOffset).cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/ScalarReturnTypeMethods(DateTimeOffset).cs index dd967f8292f..87a71809421 100644 --- a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/ScalarReturnTypeMethods(DateTimeOffset).cs +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/ScalarReturnTypeMethods(DateTimeOffset).cs @@ -1,2 +1,7 @@ -global::System.ClientModel.ClientResult result = this.GetScalar(cancellationToken.ToRequestOptions()); -return global::System.ClientModel.ClientResult.FromValue(result.GetRawResponse().Content.ToObjectFromJson(), result.GetRawResponse()); +public virtual global::System.ClientModel.ClientResult GetScalar(global::System.Threading.CancellationToken cancellationToken = default) +{ + global::System.ClientModel.ClientResult result = this.GetScalar(cancellationToken.ToRequestOptions()); + using global::System.Text.Json.JsonDocument document = global::System.Text.Json.JsonDocument.Parse(result.GetRawResponse().Content); + global::System.DateTimeOffset value = document.RootElement.GetDateTimeOffset(); + return global::System.ClientModel.ClientResult.FromValue(value, result.GetRawResponse()); +} diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/ScalarReturnTypeMethods(Decimal).cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/ScalarReturnTypeMethods(Decimal).cs new file mode 100644 index 00000000000..730ef745679 --- /dev/null +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/ScalarReturnTypeMethods(Decimal).cs @@ -0,0 +1,7 @@ +public virtual global::System.ClientModel.ClientResult GetScalar(global::System.Threading.CancellationToken cancellationToken = default) +{ + global::System.ClientModel.ClientResult result = this.GetScalar(cancellationToken.ToRequestOptions()); + using global::System.Text.Json.JsonDocument document = global::System.Text.Json.JsonDocument.Parse(result.GetRawResponse().Content); + decimal value = document.RootElement.GetDecimal(); + return global::System.ClientModel.ClientResult.FromValue(value, result.GetRawResponse()); +} diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/ScalarReturnTypeMethods(Double).cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/ScalarReturnTypeMethods(Double).cs index b6d44cd9c10..7651c3d213f 100644 --- a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/ScalarReturnTypeMethods(Double).cs +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/ScalarReturnTypeMethods(Double).cs @@ -1,2 +1,7 @@ -global::System.ClientModel.ClientResult result = this.GetScalar(cancellationToken.ToRequestOptions()); -return global::System.ClientModel.ClientResult.FromValue(result.GetRawResponse().Content.ToObjectFromJson(), result.GetRawResponse()); +public virtual global::System.ClientModel.ClientResult GetScalar(global::System.Threading.CancellationToken cancellationToken = default) +{ + global::System.ClientModel.ClientResult result = this.GetScalar(cancellationToken.ToRequestOptions()); + using global::System.Text.Json.JsonDocument document = global::System.Text.Json.JsonDocument.Parse(result.GetRawResponse().Content); + double value = document.RootElement.GetDouble(); + return global::System.ClientModel.ClientResult.FromValue(value, result.GetRawResponse()); +} diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/ScalarReturnTypeMethods(Int32).cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/ScalarReturnTypeMethods(Int32).cs index cb0644c9c72..f74bcf62f77 100644 --- a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/ScalarReturnTypeMethods(Int32).cs +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/ScalarReturnTypeMethods(Int32).cs @@ -1,2 +1,7 @@ -global::System.ClientModel.ClientResult result = this.GetScalar(cancellationToken.ToRequestOptions()); -return global::System.ClientModel.ClientResult.FromValue(result.GetRawResponse().Content.ToObjectFromJson(), result.GetRawResponse()); +public virtual global::System.ClientModel.ClientResult GetScalar(global::System.Threading.CancellationToken cancellationToken = default) +{ + global::System.ClientModel.ClientResult result = this.GetScalar(cancellationToken.ToRequestOptions()); + using global::System.Text.Json.JsonDocument document = global::System.Text.Json.JsonDocument.Parse(result.GetRawResponse().Content); + int value = document.RootElement.GetInt32(); + return global::System.ClientModel.ClientResult.FromValue(value, result.GetRawResponse()); +} diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/ScalarReturnTypeMethods(Int32Async).cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/ScalarReturnTypeMethods(Int32Async).cs new file mode 100644 index 00000000000..ec5d20e2a31 --- /dev/null +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/ScalarReturnTypeMethods(Int32Async).cs @@ -0,0 +1,7 @@ +public virtual async global::System.Threading.Tasks.Task> GetScalarAsync(global::System.Threading.CancellationToken cancellationToken = default) +{ + global::System.ClientModel.ClientResult result = await this.GetScalarAsync(cancellationToken.ToRequestOptions()).ConfigureAwait(false); + using global::System.Text.Json.JsonDocument document = global::System.Text.Json.JsonDocument.Parse(result.GetRawResponse().Content); + int value = document.RootElement.GetInt32(); + return global::System.ClientModel.ClientResult.FromValue(value, result.GetRawResponse()); +} diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/ScalarReturnTypeMethods(Int32Nullable).cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/ScalarReturnTypeMethods(Int32Nullable).cs new file mode 100644 index 00000000000..2514bcd7e46 --- /dev/null +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/ScalarReturnTypeMethods(Int32Nullable).cs @@ -0,0 +1,7 @@ +public virtual global::System.ClientModel.ClientResult GetScalar(global::System.Threading.CancellationToken cancellationToken = default) +{ + global::System.ClientModel.ClientResult result = this.GetScalar(cancellationToken.ToRequestOptions()); + using global::System.Text.Json.JsonDocument document = global::System.Text.Json.JsonDocument.Parse(result.GetRawResponse().Content); + int? value = (document.RootElement.ValueKind == global::System.Text.Json.JsonValueKind.Null) ? ((int?)null) : document.RootElement.GetInt32(); + return global::System.ClientModel.ClientResult.FromValue(value, result.GetRawResponse()); +} diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/ScalarReturnTypeMethods(Int32NullableAsync).cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/ScalarReturnTypeMethods(Int32NullableAsync).cs new file mode 100644 index 00000000000..f1e37ab2e4b --- /dev/null +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/ScalarReturnTypeMethods(Int32NullableAsync).cs @@ -0,0 +1,7 @@ +public virtual async global::System.Threading.Tasks.Task> GetScalarAsync(global::System.Threading.CancellationToken cancellationToken = default) +{ + global::System.ClientModel.ClientResult result = await this.GetScalarAsync(cancellationToken.ToRequestOptions()).ConfigureAwait(false); + using global::System.Text.Json.JsonDocument document = global::System.Text.Json.JsonDocument.Parse(result.GetRawResponse().Content); + int? value = (document.RootElement.ValueKind == global::System.Text.Json.JsonValueKind.Null) ? ((int?)null) : document.RootElement.GetInt32(); + return global::System.ClientModel.ClientResult.FromValue(value, result.GetRawResponse()); +} diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/ScalarReturnTypeMethods(Int64).cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/ScalarReturnTypeMethods(Int64).cs index 558d73a47ea..4a044c38e5c 100644 --- a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/ScalarReturnTypeMethods(Int64).cs +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/ScalarReturnTypeMethods(Int64).cs @@ -1,2 +1,7 @@ -global::System.ClientModel.ClientResult result = this.GetScalar(cancellationToken.ToRequestOptions()); -return global::System.ClientModel.ClientResult.FromValue(result.GetRawResponse().Content.ToObjectFromJson(), result.GetRawResponse()); +public virtual global::System.ClientModel.ClientResult GetScalar(global::System.Threading.CancellationToken cancellationToken = default) +{ + global::System.ClientModel.ClientResult result = this.GetScalar(cancellationToken.ToRequestOptions()); + using global::System.Text.Json.JsonDocument document = global::System.Text.Json.JsonDocument.Parse(result.GetRawResponse().Content); + long value = document.RootElement.GetInt64(); + return global::System.ClientModel.ClientResult.FromValue(value, result.GetRawResponse()); +} diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/ScalarReturnTypeMethods(SByte).cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/ScalarReturnTypeMethods(SByte).cs new file mode 100644 index 00000000000..8ecf52e4e9f --- /dev/null +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/ScalarReturnTypeMethods(SByte).cs @@ -0,0 +1,7 @@ +public virtual global::System.ClientModel.ClientResult GetScalar(global::System.Threading.CancellationToken cancellationToken = default) +{ + global::System.ClientModel.ClientResult result = this.GetScalar(cancellationToken.ToRequestOptions()); + using global::System.Text.Json.JsonDocument document = global::System.Text.Json.JsonDocument.Parse(result.GetRawResponse().Content); + sbyte value = document.RootElement.GetSByte(); + return global::System.ClientModel.ClientResult.FromValue(value, result.GetRawResponse()); +} diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/ScalarReturnTypeMethods(Single).cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/ScalarReturnTypeMethods(Single).cs index df2eb3be24c..5609e7604ca 100644 --- a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/ScalarReturnTypeMethods(Single).cs +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/ScalarReturnTypeMethods(Single).cs @@ -1,2 +1,7 @@ -global::System.ClientModel.ClientResult result = this.GetScalar(cancellationToken.ToRequestOptions()); -return global::System.ClientModel.ClientResult.FromValue(result.GetRawResponse().Content.ToObjectFromJson(), result.GetRawResponse()); +public virtual global::System.ClientModel.ClientResult GetScalar(global::System.Threading.CancellationToken cancellationToken = default) +{ + global::System.ClientModel.ClientResult result = this.GetScalar(cancellationToken.ToRequestOptions()); + using global::System.Text.Json.JsonDocument document = global::System.Text.Json.JsonDocument.Parse(result.GetRawResponse().Content); + float value = document.RootElement.GetSingle(); + return global::System.ClientModel.ClientResult.FromValue(value, result.GetRawResponse()); +} diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/ScalarReturnTypeMethods(String).cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/ScalarReturnTypeMethods(String).cs index 1cf02c42f1e..196feb5e4c4 100644 --- a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/ScalarReturnTypeMethods(String).cs +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/ScalarReturnTypeMethods(String).cs @@ -1,2 +1,7 @@ -global::System.ClientModel.ClientResult result = this.GetScalar(cancellationToken.ToRequestOptions()); -return global::System.ClientModel.ClientResult.FromValue(result.GetRawResponse().Content.ToObjectFromJson(), result.GetRawResponse()); +public virtual global::System.ClientModel.ClientResult GetScalar(global::System.Threading.CancellationToken cancellationToken = default) +{ + global::System.ClientModel.ClientResult result = this.GetScalar(cancellationToken.ToRequestOptions()); + using global::System.Text.Json.JsonDocument document = global::System.Text.Json.JsonDocument.Parse(result.GetRawResponse().Content); + string value = document.RootElement.GetString(); + return global::System.ClientModel.ClientResult.FromValue(value, result.GetRawResponse()); +} diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/ScalarReturnTypeMethods(TimeSpan).cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/ScalarReturnTypeMethods(TimeSpan).cs index 9012ebf182c..3196af47bea 100644 --- a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/ScalarReturnTypeMethods(TimeSpan).cs +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/ScalarReturnTypeMethods(TimeSpan).cs @@ -1,2 +1,7 @@ -global::System.ClientModel.ClientResult result = this.GetScalar(cancellationToken.ToRequestOptions()); -return global::System.ClientModel.ClientResult.FromValue(result.GetRawResponse().Content.ToObjectFromJson(), result.GetRawResponse()); +public virtual global::System.ClientModel.ClientResult GetScalar(global::System.Threading.CancellationToken cancellationToken = default) +{ + global::System.ClientModel.ClientResult result = this.GetScalar(cancellationToken.ToRequestOptions()); + using global::System.Text.Json.JsonDocument document = global::System.Text.Json.JsonDocument.Parse(result.GetRawResponse().Content); + global::System.TimeSpan value = document.RootElement.GetTimeSpan("c"); + return global::System.ClientModel.ClientResult.FromValue(value, result.GetRawResponse()); +} diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/ScalarReturnTypeMethods(TimeSpanNullable).cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/ScalarReturnTypeMethods(TimeSpanNullable).cs new file mode 100644 index 00000000000..ee7791725a9 --- /dev/null +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/ScalarReturnTypeMethods(TimeSpanNullable).cs @@ -0,0 +1,7 @@ +public virtual global::System.ClientModel.ClientResult GetScalar(global::System.Threading.CancellationToken cancellationToken = default) +{ + global::System.ClientModel.ClientResult result = this.GetScalar(cancellationToken.ToRequestOptions()); + using global::System.Text.Json.JsonDocument document = global::System.Text.Json.JsonDocument.Parse(result.GetRawResponse().Content); + global::System.TimeSpan? value = (document.RootElement.ValueKind == global::System.Text.Json.JsonValueKind.Null) ? ((global::System.TimeSpan?)null) : document.RootElement.GetTimeSpan("c"); + return global::System.ClientModel.ClientResult.FromValue(value, result.GetRawResponse()); +} diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/ScalarReturnTypeMethods(Uri).cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/ScalarReturnTypeMethods(Uri).cs index 3a0a7cfdd2e..04e2feca393 100644 --- a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/ScalarReturnTypeMethods(Uri).cs +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/TestData/ScmMethodProviderCollectionTests/ScalarReturnTypeMethods(Uri).cs @@ -1,5 +1,7 @@ -global::System.ClientModel.ClientResult result = this.GetScalar(cancellationToken.ToRequestOptions()); -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(), global::System.UriKind.RelativeOrAbsolute), result.GetRawResponse()); +public virtual global::System.ClientModel.ClientResult GetScalar(global::System.Threading.CancellationToken cancellationToken = default) +{ + global::System.ClientModel.ClientResult result = this.GetScalar(cancellationToken.ToRequestOptions()); + using global::System.Text.Json.JsonDocument document = global::System.Text.Json.JsonDocument.Parse(result.GetRawResponse().Content); + global::System.Uri value = new global::System.Uri(document.RootElement.GetString(), global::System.UriKind.RelativeOrAbsolute); + return global::System.ClientModel.ClientResult.FromValue(value, result.GetRawResponse()); +} diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/src/Snippets/StringSnippets.cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/src/Snippets/StringSnippets.cs index 41a9b42a01e..9850d18d12e 100644 --- a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/src/Snippets/StringSnippets.cs +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/src/Snippets/StringSnippets.cs @@ -33,6 +33,9 @@ public static ValueExpression Split(this ScopedApi stringExpression, Val public static ScopedApi Substring(this ScopedApi stringExpression, ValueExpression startIndex) => stringExpression.Invoke(nameof(string.Substring), [startIndex], null, false).As(); + public static ScopedApi Trim(this ScopedApi stringExpression) + => stringExpression.Invoke(nameof(string.Trim)).As(); + public static ValueExpression ToCharArray(this ScopedApi stringExpression) => stringExpression.Invoke(nameof(string.ToCharArray), Array.Empty(), null, false); diff --git a/packages/http-client-csharp/generator/TestProjects/Local.Tests/ExtensibleEnumTests.cs b/packages/http-client-csharp/generator/TestProjects/Local.Tests/ExtensibleEnumTests.cs index c348067affa..09ad1945975 100644 --- a/packages/http-client-csharp/generator/TestProjects/Local.Tests/ExtensibleEnumTests.cs +++ b/packages/http-client-csharp/generator/TestProjects/Local.Tests/ExtensibleEnumTests.cs @@ -2,9 +2,13 @@ // Licensed under the MIT License. using System; +using System.ClientModel; +using System.ClientModel.Primitives; using System.Collections.Generic; using System.Linq; using System.Reflection; +using System.Threading.Tasks; +using Moq; using NUnit.Framework; using SampleTypeSpec; @@ -12,6 +16,27 @@ namespace TestProjects.Local.Tests { public class ExtensibleEnumTests { + [TestCase(false)] + [TestCase(true)] + public async Task EnumResponseDeserialization(bool isAsync) + { + var content = BinaryData.FromString("Monday"); + var response = new Mock(); + response.SetupGet(r => r.Content).Returns(content); + var protocolResult = ClientResult.FromResponse(response.Object); + var client = new Mock { CallBase = true }; + client.Setup(c => c.GetUnknownValue(It.IsAny())).Returns(protocolResult); + client.Setup(c => c.GetUnknownValueAsync(It.IsAny())).ReturnsAsync(protocolResult); + + var result = isAsync + ? await client.Object.GetUnknownValueAsync() + : client.Object.GetUnknownValue(); + + Assert.AreEqual("Monday", result.Value.ToString()); + Assert.AreSame(response.Object, result.GetRawResponse()); + Assert.AreSame(content, result.GetRawResponse().Content); + } + [TestCase("a", "A", true)] [TestCase("A", "A", true)] [TestCase("A", "B", false)] diff --git a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/SampleTypeSpecClient.cs b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/SampleTypeSpecClient.cs index 9e3d6da09ad..f9f6970f13a 100644 --- a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/SampleTypeSpecClient.cs +++ b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/SampleTypeSpecClient.cs @@ -1086,7 +1086,8 @@ public virtual async Task GetUnknownValueAsync(RequestOptions opti public virtual ClientResult GetUnknownValue(CancellationToken cancellationToken = default) { ClientResult result = GetUnknownValue(cancellationToken.ToRequestOptions()); - return ClientResult.FromValue(new DaysOfWeekExtensibleEnum(result.GetRawResponse().Content.ToObjectFromJson()), result.GetRawResponse()); + DaysOfWeekExtensibleEnum value = new DaysOfWeekExtensibleEnum(result.GetRawResponse().Content.ToString()); + return ClientResult.FromValue(value, result.GetRawResponse()); } /// get extensible enum. @@ -1095,7 +1096,8 @@ public virtual ClientResult GetUnknownValue(Cancellati public virtual async Task> GetUnknownValueAsync(CancellationToken cancellationToken = default) { ClientResult result = await GetUnknownValueAsync(cancellationToken.ToRequestOptions()).ConfigureAwait(false); - return ClientResult.FromValue(new DaysOfWeekExtensibleEnum(result.GetRawResponse().Content.ToObjectFromJson()), result.GetRawResponse()); + DaysOfWeekExtensibleEnum value = new DaysOfWeekExtensibleEnum(result.GetRawResponse().Content.ToString()); + return ClientResult.FromValue(value, result.GetRawResponse()); } ///