Skip to content

Commit

Permalink
Remove invalid trim suppression in AIJsonUtilities
Browse files Browse the repository at this point in the history
Instead use RequiresUnreferencedCode to show that these APIs may not work in trimmed apps.

Fix dotnet#5629
  • Loading branch information
eerhardt committed Nov 12, 2024
1 parent 5b33969 commit 2b17dff
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ public static partial class AIJsonUtilities
/// <param name="serializerOptions">The options used to extract the schema from the specified type.</param>
/// <param name="inferenceOptions">The options controlling schema inference.</param>
/// <returns>A JSON schema document encoded as a <see cref="JsonElement"/>.</returns>
#if !NET9_0_OR_GREATER
[RequiresUnreferencedCode("Pre STJ-9 schema extraction can fail with a runtime exception if certain reflection metadata have been trimmed.")]
#endif
public static JsonElement ResolveParameterJsonSchema(
AIFunctionParameterMetadata parameterMetadata,
AIFunctionMetadata functionMetadata,
Expand Down Expand Up @@ -107,6 +110,9 @@ public static JsonElement ResolveParameterJsonSchema(
/// <param name="serializerOptions">The options used to extract the schema from the specified type.</param>
/// <param name="inferenceOptions">The options controlling schema inference.</param>
/// <returns>A JSON schema document encoded as a <see cref="JsonElement"/>.</returns>
#if !NET9_0_OR_GREATER
[RequiresUnreferencedCode("Pre STJ-9 schema extraction can fail with a runtime exception if certain reflection metadata have been trimmed.")]
#endif
public static JsonElement CreateParameterJsonSchema(
Type? type,
string parameterName,
Expand Down Expand Up @@ -144,6 +150,9 @@ public static JsonElement CreateParameterJsonSchema(
/// <param name="serializerOptions">The options used to extract the schema from the specified type.</param>
/// <param name="inferenceOptions">The options controlling schema inference.</param>
/// <returns>A <see cref="JsonElement"/> representing the schema.</returns>
#if !NET9_0_OR_GREATER
[RequiresUnreferencedCode("Pre STJ-9 schema extraction can fail with a runtime exception if certain reflection metadata have been trimmed.")]
#endif
public static JsonElement CreateJsonSchema(
Type? type,
string? description = null,
Expand All @@ -170,6 +179,9 @@ public static JsonElement CreateJsonSchema(
return GetJsonSchemaCached(serializerOptions, key);
}

#if !NET9_0_OR_GREATER
[RequiresUnreferencedCode("Pre STJ-9 schema extraction can fail with a runtime exception if certain reflection metadata have been trimmed.")]
#endif
private static JsonElement GetJsonSchemaCached(JsonSerializerOptions options, FunctionParameterKey key)
{
options.MakeReadOnly();
Expand All @@ -191,9 +203,7 @@ private static JsonElement GetJsonSchemaCached(JsonSerializerOptions options, Fu
}

#if !NET9_0_OR_GREATER
[UnconditionalSuppressMessage("Trimming", "IL2026:Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access",
Justification = "Pre STJ-9 schema extraction can fail with a runtime exception if certain reflection metadata have been trimmed. " +
"The exception message will guide users to turn off 'IlcTrimMetadata' which resolves all issues.")]
[RequiresUnreferencedCode("Pre STJ-9 schema extraction can fail with a runtime exception if certain reflection metadata have been trimmed.")]
#endif
private static JsonElement GetJsonSchemaCore(JsonSerializerOptions options, FunctionParameterKey key)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
#if !NET9_0_OR_GREATER
using System.Diagnostics.CodeAnalysis;
#endif
using System.Reflection;
using System.Text.Json;
using System.Text.Json.Nodes;
Expand Down Expand Up @@ -41,6 +44,9 @@ public static class ChatClientStructuredOutputExtensions
/// by the client, including any messages for roundtrips to the model as part of the implementation of this request, will be included.
/// </remarks>
/// <typeparam name="T">The type of structured output to request.</typeparam>
#if !NET9_0_OR_GREATER
[RequiresUnreferencedCode("Pre STJ-9 schema extraction can fail with a runtime exception if certain reflection metadata have been trimmed.")]
#endif
public static Task<ChatCompletion<T>> CompleteAsync<T>(
this IChatClient chatClient,
IList<ChatMessage> chatMessages,
Expand All @@ -61,6 +67,9 @@ public static Task<ChatCompletion<T>> CompleteAsync<T>(
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
/// <returns>The response messages generated by the client.</returns>
/// <typeparam name="T">The type of structured output to request.</typeparam>
#if !NET9_0_OR_GREATER
[RequiresUnreferencedCode("Pre STJ-9 schema extraction can fail with a runtime exception if certain reflection metadata have been trimmed.")]
#endif
public static Task<ChatCompletion<T>> CompleteAsync<T>(
this IChatClient chatClient,
string chatMessage,
Expand All @@ -82,6 +91,9 @@ public static Task<ChatCompletion<T>> CompleteAsync<T>(
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
/// <returns>The response messages generated by the client.</returns>
/// <typeparam name="T">The type of structured output to request.</typeparam>
#if !NET9_0_OR_GREATER
[RequiresUnreferencedCode("Pre STJ-9 schema extraction can fail with a runtime exception if certain reflection metadata have been trimmed.")]
#endif
public static Task<ChatCompletion<T>> CompleteAsync<T>(
this IChatClient chatClient,
string chatMessage,
Expand All @@ -108,6 +120,9 @@ public static Task<ChatCompletion<T>> CompleteAsync<T>(
/// by the client, including any messages for roundtrips to the model as part of the implementation of this request, will be included.
/// </remarks>
/// <typeparam name="T">The type of structured output to request.</typeparam>
#if !NET9_0_OR_GREATER
[RequiresUnreferencedCode("Pre STJ-9 schema extraction can fail with a runtime exception if certain reflection metadata have been trimmed.")]
#endif
public static async Task<ChatCompletion<T>> CompleteAsync<T>(
this IChatClient chatClient,
IList<ChatMessage> chatMessages,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
#if !NET9_0_OR_GREATER
using System.Diagnostics.CodeAnalysis;
#endif
using System.IO;
using System.Linq;
using System.Reflection;
Expand All @@ -28,6 +31,9 @@ public static partial class AIFunctionFactory
/// <param name="method">The method to be represented via the created <see cref="AIFunction"/>.</param>
/// <param name="options">Metadata to use to override defaults inferred from <paramref name="method"/>.</param>
/// <returns>The created <see cref="AIFunction"/> for invoking <paramref name="method"/>.</returns>
#if !NET9_0_OR_GREATER
[RequiresUnreferencedCode("Pre STJ-9 schema extraction can fail with a runtime exception if certain reflection metadata have been trimmed.")]
#endif
public static AIFunction Create(Delegate method, AIFunctionFactoryCreateOptions? options)
{
_ = Throw.IfNull(method);
Expand All @@ -41,6 +47,9 @@ public static AIFunction Create(Delegate method, AIFunctionFactoryCreateOptions?
/// <param name="description">The description to use for the <see cref="AIFunction"/>.</param>
/// <param name="serializerOptions">The <see cref="JsonSerializerOptions"/> used to marshal function parameters and any return value.</param>
/// <returns>The created <see cref="AIFunction"/> for invoking <paramref name="method"/>.</returns>
#if !NET9_0_OR_GREATER
[RequiresUnreferencedCode("Pre STJ-9 schema extraction can fail with a runtime exception if certain reflection metadata have been trimmed.")]
#endif
public static AIFunction Create(Delegate method, string? name = null, string? description = null, JsonSerializerOptions? serializerOptions = null)
{
_ = Throw.IfNull(method);
Expand Down Expand Up @@ -68,6 +77,9 @@ public static AIFunction Create(Delegate method, string? name = null, string? de
/// </param>
/// <param name="options">Metadata to use to override defaults inferred from <paramref name="method"/>.</param>
/// <returns>The created <see cref="AIFunction"/> for invoking <paramref name="method"/>.</returns>
#if !NET9_0_OR_GREATER
[RequiresUnreferencedCode("Pre STJ-9 schema extraction can fail with a runtime exception if certain reflection metadata have been trimmed.")]
#endif
public static AIFunction Create(MethodInfo method, object? target, AIFunctionFactoryCreateOptions? options)
{
_ = Throw.IfNull(method);
Expand All @@ -87,6 +99,9 @@ public static AIFunction Create(MethodInfo method, object? target, AIFunctionFac
/// <param name="description">The description to use for the <see cref="AIFunction"/>.</param>
/// <param name="serializerOptions">The <see cref="JsonSerializerOptions"/> used to marshal function parameters and return value.</param>
/// <returns>The created <see cref="AIFunction"/> for invoking <paramref name="method"/>.</returns>
#if !NET9_0_OR_GREATER
[RequiresUnreferencedCode("Pre STJ-9 schema extraction can fail with a runtime exception if certain reflection metadata have been trimmed.")]
#endif
public static AIFunction Create(MethodInfo method, object? target, string? name = null, string? description = null, JsonSerializerOptions? serializerOptions = null)
{
_ = Throw.IfNull(method);
Expand Down Expand Up @@ -122,6 +137,9 @@ private sealed class ReflectionAIFunction : AIFunction
/// This should be <see langword="null"/> if and only if <paramref name="method"/> is a static method.
/// </param>
/// <param name="options">Function creation options.</param>
#if !NET9_0_OR_GREATER
[RequiresUnreferencedCode("Pre STJ-9 schema extraction can fail with a runtime exception if certain reflection metadata have been trimmed.")]
#endif
public ReflectionAIFunction(MethodInfo method, object? target, AIFunctionFactoryCreateOptions options)
{
_ = Throw.IfNull(method);
Expand Down Expand Up @@ -271,6 +289,9 @@ static bool IsAsyncMethod(MethodInfo method)
/// <summary>
/// Gets a delegate for handling the marshaling of a parameter.
/// </summary>
#if !NET9_0_OR_GREATER
[RequiresUnreferencedCode("Pre STJ-9 schema extraction can fail with a runtime exception if certain reflection metadata have been trimmed.")]
#endif
private static AIFunctionParameterMetadata? GetParameterMarshaller(
AIFunctionFactoryCreateOptions options,
ParameterInfo parameter,
Expand Down

0 comments on commit 2b17dff

Please sign in to comment.