diff --git a/sdk/provisioning/Azure.Provisioning.CloudMachine/src/Tsp/TypeSpecWriter.cs b/sdk/provisioning/Azure.Provisioning.CloudMachine/src/Tsp/TypeSpecWriter.cs index a08ba361a898e..f8beed28929d7 100644 --- a/sdk/provisioning/Azure.Provisioning.CloudMachine/src/Tsp/TypeSpecWriter.cs +++ b/sdk/provisioning/Azure.Provisioning.CloudMachine/src/Tsp/TypeSpecWriter.cs @@ -5,6 +5,7 @@ using System.IO; using System.Net; using System.Reflection; +using System.Threading.Tasks; namespace System.ClientModel.TypeSpec; public static class TypeSpecWriter @@ -104,7 +105,9 @@ private static void WriteOperation(StreamWriter writer, MethodInfo method, HashS { string httpVerb = ReadHttpVerb(method); - writer.Write($"{httpVerb} @route(\"{ToCamel(method.Name)}\") {method.Name}("); + var methodName = method.Name; + if (methodName.EndsWith("Async")) methodName = methodName.Substring(0, methodName.Length - "Async".Length); + writer.Write($"{httpVerb} @route(\"{ToCamel(methodName)}\") {methodName}("); bool first = true; foreach (var parameter in method.GetParameters()) @@ -136,11 +139,16 @@ private static void WriteOperation(StreamWriter writer, MethodInfo method, HashS } writer.WriteLine(") : {"); writer.WriteLine($" @statusCode statusCode: 200;"); - if (method.ReflectedType != typeof(void)) - writer.WriteLine($" @body response : {method.ReturnType.ToTspType()};"); + + var returnType = method.ReturnType; + if (returnType == typeof(Task)) returnType = typeof(void); + else if (returnType.IsGenericType && returnType.GetGenericTypeDefinition() == typeof(Task<>)) + returnType = returnType.GetGenericArguments()[0]; + + writer.WriteLine($" @body response : {returnType.ToTspType()};"); writer.WriteLine(" };"); - if (method.ReturnType.IsModel()) - models.Add(method.ReturnType); + if (returnType.IsModel()) + models.Add(returnType); } private static string ReadParameterLocation(ParameterInfo parameter) diff --git a/sdk/provisioning/Azure.Provisioning.CloudMachine/tests/Azure.Provisioning.CloudMachine.Tests.csproj b/sdk/provisioning/Azure.Provisioning.CloudMachine/tests/Azure.Provisioning.CloudMachine.Tests.csproj index ac4afb158ed33..681498fc5e434 100644 --- a/sdk/provisioning/Azure.Provisioning.CloudMachine/tests/Azure.Provisioning.CloudMachine.Tests.csproj +++ b/sdk/provisioning/Azure.Provisioning.CloudMachine/tests/Azure.Provisioning.CloudMachine.Tests.csproj @@ -1,4 +1,4 @@ - + 12 diff --git a/sdk/provisioning/Azure.Provisioning.CloudMachine/tests/TdkTests.cs b/sdk/provisioning/Azure.Provisioning.CloudMachine/tests/TdkTests.cs new file mode 100644 index 0000000000000..7951f968ace07 --- /dev/null +++ b/sdk/provisioning/Azure.Provisioning.CloudMachine/tests/TdkTests.cs @@ -0,0 +1,67 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +#nullable enable + +using System; +using System.ClientModel.TypeSpec; +using System.IO; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Http; +using NUnit.Framework; + +namespace Azure.CloudMachine.Tests; + +public class TdkTests +{ + [Test] + public void GenerateIAssistantService() + { + MemoryStream stream = new(); + TypeSpecWriter.WriteServer(stream); + stream.Position = 0; + + BinaryData data = BinaryData.FromStream(stream); + Assert.AreEqual(IAssistantServiceTsp, data.ToString()); + } + + private static string IAssistantServiceTsp = + """ + import "@typespec/http"; + import "@typespec/rest"; + import "@azure-tools/typespec-client-generator-core"; + + @service({ + title: "AssistantService", + }) + + namespace Azure.CloudMachine.Tests; + + using TypeSpec.Http; + using TypeSpec.Rest; + using Azure.ClientGenerator.Core; + + @client interface AssistantServiceClient { + @put @route("upload") Upload(@header contentType: "application/octet-stream", @body document: bytes) : { + @statusCode statusCode: 200; + @body response : void; + }; + @get @route("send") Send(@query message: string) : { + @statusCode statusCode: 200; + @body response : string; + }; + } + + + """; +} + +internal interface IAssistantService +{ + [HttpPut] + Task UploadAsync(HttpRequest document); + Task SendAsync([FromQuery] string message); +} + +internal class FromQueryAttribute : Attribute { } +internal class HttpPutAttribute : Attribute { }