Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions eng/Packages.Data.props
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@
<PackageReference Update="Microsoft.ApplicationInsights" Version="2.20.0" />
<PackageReference Update="Microsoft.Azure.ApplicationInsights.Query" Version="1.0.0" />
<PackageReference Update="Microsoft.AspNetCore" Version="2.2.0" />
<PackageReference Update="Microsoft.AspNetCore.Mvc.Core" Version="2.2.0" />
<PackageReference Update="Microsoft.AspNetCore.Mvc.Testing" Version="2.2.0" />
<PackageReference Update="Microsoft.AspNetCore.Server.Kestrel" Version="2.1.3" />
<PackageReference Update="Microsoft.AspNetCore.Server.Kestrel.Core" Version="2.1.25" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
<PropertyGroup>
<LangVersion>12</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core"/>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\src\Azure.Provisioning.CloudMachine.csproj" />
<ProjectReference Include="..\..\Azure.Provisioning.Deployment\src\Azure.Provisioning.Deployment.csproj" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// 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 Microsoft.AspNetCore.Mvc;
using NUnit.Framework;

namespace Azure.CloudMachine.Tests;

public class TdkTests
{
[Test]
public void GenerateIAssistantService()
{
MemoryStream stream = new();
TypeSpecWriter.WriteServer<IAssistantService>(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<string> SendAsync([FromQuery] string message);
}