Skip to content

[http-client-csharp] Generated methods trigger AOT warnings #9142

@JonathanCrd

Description

@JonathanCrd

While working on this PR to migrate a client library, Azure/azure-sdk-for-net#54156, I discovered that the actual output is not AOT safe for certain methods with specific return types, as string[].

We must avoid generating returns with ToObjectFromJson() and use ModelReaderWriter.Read instead.

The problem is in:

private ValueExpression GetResultConversion(ClientResponseApi result, HttpResponseApi response, CSharpType responseBodyType, Dictionary<string, ValueExpression> declarations)
{
if (responseBodyType.Equals(typeof(BinaryData)))
{
return response.Content();
}
if (responseBodyType.IsList)
{
if (!responseBodyType.Arguments[0].IsFrameworkType || responseBodyType.Arguments[0].Equals(typeof(TimeSpan)) || responseBodyType.Arguments[0].Equals(typeof(BinaryData)))
{
return declarations["value"].CastTo(new CSharpType(responseBodyType.OutputType.FrameworkType, responseBodyType.Arguments[0]));
}
else
{
return response.Content().ToObjectFromJson(responseBodyType.OutputType);
}
}
if (responseBodyType.IsDictionary)
{
if (!responseBodyType.Arguments[1].IsFrameworkType || responseBodyType.Arguments[1].Equals(typeof(TimeSpan)) || responseBodyType.Arguments[1].Equals(typeof(BinaryData)))
{
return declarations["value"].CastTo(new CSharpType(responseBodyType.OutputType.FrameworkType, responseBodyType.Arguments[0], responseBodyType.Arguments[1]));
}
else
{
return response.Content().ToObjectFromJson(responseBodyType.OutputType);
}
}
if (responseBodyType.Equals(typeof(string)) && ServiceMethod.Operation.Responses.Any(r => r.IsErrorResponse is false && r.ContentTypes.Contains("text/plain")))
{
return response.Content().InvokeToString();
}
if (responseBodyType.IsFrameworkType)
{
return response.Content().ToObjectFromJson(responseBodyType);
}
if (responseBodyType.IsEnum)
{
return responseBodyType.ToEnum(response.Content().ToObjectFromJson(responseBodyType.UnderlyingEnumType));
}
return result.CastTo(responseBodyType);
}

The change must be done for the following types:

  • List
  • Dictionary
  • FrameworkType
  • Enum

Expected output

We expect:

return ClientResult.FromValue(ModelReaderWriter.Read<IReadOnlyList<string>>(result.GetRawResponse().Content, ModelSerializationExtensions.WireOptions, SampleTypeSpecContext.Default), result.GetRawResponse());

instead of:

return ClientResult.FromValue(result.GetRawResponse().Content.ToObjectFromJson<IReadOnlyList<string>>(), result.GetRawResponse());

Metadata

Metadata

Assignees

Labels

emitter:client:csharpIssue for the C# client emitter: @typespec/http-client-csharp

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions