Skip to content

Commit ea5cb40

Browse files
added support for basic async APIs (Azure#46974)
* added support for basic async APIs * PR feedback * removed dependency on deprecated package
1 parent 9e4950e commit ea5cb40

File tree

3 files changed

+81
-6
lines changed

3 files changed

+81
-6
lines changed

sdk/provisioning/Azure.Provisioning.CloudMachine/src/Tsp/TypeSpecWriter.cs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.IO;
66
using System.Net;
77
using System.Reflection;
8+
using System.Threading.Tasks;
89

910
namespace System.ClientModel.TypeSpec;
1011
public static class TypeSpecWriter
@@ -104,7 +105,9 @@ private static void WriteOperation(StreamWriter writer, MethodInfo method, HashS
104105
{
105106
string httpVerb = ReadHttpVerb(method);
106107

107-
writer.Write($"{httpVerb} @route(\"{ToCamel(method.Name)}\") {method.Name}(");
108+
var methodName = method.Name;
109+
if (methodName.EndsWith("Async")) methodName = methodName.Substring(0, methodName.Length - "Async".Length);
110+
writer.Write($"{httpVerb} @route(\"{ToCamel(methodName)}\") {methodName}(");
108111

109112
bool first = true;
110113
foreach (var parameter in method.GetParameters())
@@ -136,11 +139,16 @@ private static void WriteOperation(StreamWriter writer, MethodInfo method, HashS
136139
}
137140
writer.WriteLine(") : {");
138141
writer.WriteLine($" @statusCode statusCode: 200;");
139-
if (method.ReflectedType != typeof(void))
140-
writer.WriteLine($" @body response : {method.ReturnType.ToTspType()};");
142+
143+
var returnType = method.ReturnType;
144+
if (returnType == typeof(Task)) returnType = typeof(void);
145+
else if (returnType.IsGenericType && returnType.GetGenericTypeDefinition() == typeof(Task<>))
146+
returnType = returnType.GetGenericArguments()[0];
147+
148+
writer.WriteLine($" @body response : {returnType.ToTspType()};");
141149
writer.WriteLine(" };");
142-
if (method.ReturnType.IsModel())
143-
models.Add(method.ReturnType);
150+
if (returnType.IsModel())
151+
models.Add(returnType);
144152
}
145153

146154
private static string ReadParameterLocation(ParameterInfo parameter)

sdk/provisioning/Azure.Provisioning.CloudMachine/tests/Azure.Provisioning.CloudMachine.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<LangVersion>12</LangVersion>
44
</PropertyGroup>
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
#nullable enable
5+
6+
using System;
7+
using System.ClientModel.TypeSpec;
8+
using System.IO;
9+
using System.Threading.Tasks;
10+
using Microsoft.AspNetCore.Http;
11+
using NUnit.Framework;
12+
13+
namespace Azure.CloudMachine.Tests;
14+
15+
public class TdkTests
16+
{
17+
[Test]
18+
public void GenerateIAssistantService()
19+
{
20+
MemoryStream stream = new();
21+
TypeSpecWriter.WriteServer<IAssistantService>(stream);
22+
stream.Position = 0;
23+
24+
BinaryData data = BinaryData.FromStream(stream);
25+
Assert.AreEqual(IAssistantServiceTsp, data.ToString());
26+
}
27+
28+
private static string IAssistantServiceTsp =
29+
"""
30+
import "@typespec/http";
31+
import "@typespec/rest";
32+
import "@azure-tools/typespec-client-generator-core";
33+
34+
@service({
35+
title: "AssistantService",
36+
})
37+
38+
namespace Azure.CloudMachine.Tests;
39+
40+
using TypeSpec.Http;
41+
using TypeSpec.Rest;
42+
using Azure.ClientGenerator.Core;
43+
44+
@client interface AssistantServiceClient {
45+
@put @route("upload") Upload(@header contentType: "application/octet-stream", @body document: bytes) : {
46+
@statusCode statusCode: 200;
47+
@body response : void;
48+
};
49+
@get @route("send") Send(@query message: string) : {
50+
@statusCode statusCode: 200;
51+
@body response : string;
52+
};
53+
}
54+
55+
56+
""";
57+
}
58+
59+
internal interface IAssistantService
60+
{
61+
[HttpPut]
62+
Task UploadAsync(HttpRequest document);
63+
Task<string> SendAsync([FromQuery] string message);
64+
}
65+
66+
internal class FromQueryAttribute : Attribute { }
67+
internal class HttpPutAttribute : Attribute { }

0 commit comments

Comments
 (0)