Skip to content

Commit

Permalink
feat: Replaced AnyOf JsonConverterFactory with explicit generic conve…
Browse files Browse the repository at this point in the history
…rters.
  • Loading branch information
HavenDV committed Nov 13, 2024
1 parent 7d8f442 commit f630ae0
Show file tree
Hide file tree
Showing 250 changed files with 1,174 additions and 1,391 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ jobs:
git checkout -b ${{ steps.branch.outputs.branch_name }} origin/main
git rebase main
- name: Install .NET SDK
uses: actions/setup-dotnet@v4
with:
dotnet-version: 9.0.x

- name: Generate code
run: |
cd src/libs/$SolutionName$
Expand Down
10 changes: 5 additions & 5 deletions src/libs/AutoSDK.SourceGenerators/SdkGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,11 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
.SelectAndReportExceptions((x, c) => Sources.AnyOfJsonConverter(x, c)
.AsFileWithName(), context, Id)
.AddSource(context);
data
.SelectMany(static (x, _) => x.AnyOfs)
.SelectAndReportExceptions((x, c) => Sources.AnyOfJsonConverterFactory(x, c)
.AsFileWithName(), context, Id)
.AddSource(context);
// data
// .SelectMany(static (x, _) => x.AnyOfs)
// .SelectAndReportExceptions((x, c) => Sources.AnyOfJsonConverterFactory(x, c)
// .AsFileWithName(), context, Id)
// .AddSource(context);

data
.Select(static (x, _) => x.Converters)
Expand Down
6 changes: 3 additions & 3 deletions src/libs/AutoSDK/Models/TypeData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,11 @@ CSharpTypeWithoutNullability is "string" ||
: IsEnum || (IsAnyOfLike && (IsComponent || HasDiscriminator))
? $"global::{Settings.Namespace}.JsonConverters.{ShortCSharpTypeWithoutNullability}JsonConverter"
: AnyOfCount > 0
? $"global::{Settings.Namespace}.JsonConverters.AnyOfJsonConverterFactory{AnyOfCount}"
? $"global::{Settings.Namespace}.JsonConverters.AnyOfJsonConverter<{string.Join(", ", SubTypes.Select(y => y.CSharpTypeWithNullabilityForValueTypes))}>"
: OneOfCount > 0
? $"global::{Settings.Namespace}.JsonConverters.OneOfJsonConverterFactory{OneOfCount}"
? $"global::{Settings.Namespace}.JsonConverters.OneOfJsonConverter<{string.Join(", ", SubTypes.Select(y => y.CSharpTypeWithNullabilityForValueTypes))}>"
: AllOfCount > 0
? $"global::{Settings.Namespace}.JsonConverters.AllOfJsonConverterFactory{AllOfCount}"
? $"global::{Settings.Namespace}.JsonConverters.AllOfJsonConverter<{string.Join(", ", SubTypes.Select(y => y.CSharpTypeWithNullabilityForValueTypes))}>"
: string.Empty;

public static TypeData FromSchemaContext(SchemaContext context)
Expand Down
39 changes: 26 additions & 13 deletions src/libs/AutoSDK/Sources/Data.cs
Original file line number Diff line number Diff line change
Expand Up @@ -200,20 +200,33 @@ public static Models.Data Prepare(
.Select(x => Authorization.FromOpenApiSecurityScheme(x.Key, settings))
.ToArray();

var converters = enums
.Where(x =>
x.Style == ModelStyle.Enumeration &&
x.Settings.JsonSerializerType != JsonSerializerType.NewtonsoftJson)
.SelectMany(x => new[]
{
$"global::{settings.Namespace}.JsonConverters.{x.ClassName}JsonConverter",
$"global::{settings.Namespace}.JsonConverters.{x.ClassName}NullableJsonConverter"
})
var converters =
// Enum converters
enums
.Where(x =>
x.Style == ModelStyle.Enumeration &&
x.Settings.JsonSerializerType != JsonSerializerType.NewtonsoftJson)
.SelectMany(x => new[]
{
$"global::{settings.Namespace}.JsonConverters.{x.ClassName}JsonConverter",
$"global::{settings.Namespace}.JsonConverters.{x.ClassName}NullableJsonConverter"
})
// Named AnyOf converters
.Concat(anyOfDatas
.Where(x => x.Settings.JsonSerializerType == JsonSerializerType.SystemTextJson)
.Select(x => string.IsNullOrWhiteSpace(x.Name)
? $"global::{settings.Namespace}.JsonConverters.{x.SubType}JsonConverterFactory{x.Count}"
: $"global::{settings.Namespace}.JsonConverters.{x.Name}JsonConverter"))
.Where(x =>
x.Settings.JsonSerializerType == JsonSerializerType.SystemTextJson &&
!string.IsNullOrWhiteSpace(x.Name))
.Select(x => $"global::{settings.Namespace}.JsonConverters.{x.Name}JsonConverter"))
// Generic AnyOf converters
.Concat(filteredSchemas
.Where(x =>
x.Settings.JsonSerializerType == JsonSerializerType.SystemTextJson &&
x.AnyOfData.HasValue &&
string.IsNullOrWhiteSpace(x.AnyOfData.Value.Name))
.Select(x => $"global::{settings.Namespace}.JsonConverters.{x.AnyOfData?.SubType}JsonConverter<{
string.Join(", ", x.Children
.Where(y => y.Hint is Hint.AnyOf or Hint.OneOf or Hint.AllOf)
.Select(y => y.TypeData.CSharpTypeWithNullabilityForValueTypes))}>"))
.ToImmutableArray();

var includedTags = allTags
Expand Down
29 changes: 15 additions & 14 deletions src/libs/AutoSDK/Sources/Sources.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,20 +139,21 @@ public static FileWithName EnumNullableJsonConverter(
Text: GenerateEnumNullableJsonConverter(data, cancellationToken: cancellationToken));
}

public static FileWithName AnyOfJsonConverterFactory(
AnyOfData anyOf,
CancellationToken cancellationToken = default)
{
if (anyOf.Settings.JsonSerializerType == JsonSerializerType.NewtonsoftJson ||
anyOf.IsNamed)
{
return FileWithName.Empty;
}

return new FileWithName(
Name: $"JsonConverters.{anyOf.SubType}Factory{anyOf.Count}.g.cs",
Text: GenerateAnyOfJsonConverterFactory(anyOf, cancellationToken: cancellationToken));
}
// Not used in the current implementation because not compatible with NativeAOT
// public static FileWithName AnyOfJsonConverterFactory(
// AnyOfData anyOf,
// CancellationToken cancellationToken = default)
// {
// if (anyOf.Settings.JsonSerializerType == JsonSerializerType.NewtonsoftJson ||
// anyOf.IsNamed)
// {
// return FileWithName.Empty;
// }
//
// return new FileWithName(
// Name: $"JsonConverters.{anyOf.SubType}Factory{anyOf.Count}.g.cs",
// Text: GenerateAnyOfJsonConverterFactory(anyOf, cancellationToken: cancellationToken));
// }

public static FileWithName UnixTimestampJsonConverter(
Settings settings,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,12 @@ public sealed partial class Api : global::G.IApi, global::System.IDisposable
new global::G.JsonConverters.UserMessageRoleNullableJsonConverter(),
new global::G.JsonConverters.LanguageStudioApiServerDataTypesChatChatRequestMessageDiscriminatorRoleJsonConverter(),
new global::G.JsonConverters.LanguageStudioApiServerDataTypesChatChatRequestMessageDiscriminatorRoleNullableJsonConverter(),
new global::G.JsonConverters.AnyOfJsonConverterFactory2(),
new global::G.JsonConverters.QueryFilterJsonConverter(),
new global::G.JsonConverters.MessagesItemJsonConverter(),
new global::G.JsonConverters.AnyOfJsonConverter<global::G.ChatStreamingFirstDelta, global::G.ChatStreamingContentDelta>(),
new global::G.JsonConverters.AnyOfJsonConverter<string, int?>(),
new global::G.JsonConverters.AnyOfJsonConverter<string, global::System.Collections.Generic.IList<string>>(),
new global::G.JsonConverters.AnyOfJsonConverter<global::G.ChatCompletion, global::System.Collections.Generic.IList<global::G.ChatCompletionVllmStreamingMessage>>(),
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public sealed partial class ChatCompletionResponseDeltaChoice
/// - **Subsequent messages** will have an object `{"content": __token__}` with the generated token.
/// </summary>
[global::System.Text.Json.Serialization.JsonPropertyName("delta")]
[global::System.Text.Json.Serialization.JsonConverter(typeof(global::G.JsonConverters.AnyOfJsonConverterFactory2))]
[global::System.Text.Json.Serialization.JsonConverter(typeof(global::G.JsonConverters.AnyOfJsonConverter<global::G.ChatStreamingFirstDelta, global::G.ChatStreamingContentDelta>))]
[global::System.Text.Json.Serialization.JsonRequired]
public required global::G.AnyOf<global::G.ChatStreamingFirstDelta, global::G.ChatStreamingContentDelta> Delta { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public sealed partial class LanguageStudioApiServerDataTypesChatChatRequest
///
/// </summary>
[global::System.Text.Json.Serialization.JsonPropertyName("stop")]
[global::System.Text.Json.Serialization.JsonConverter(typeof(global::G.JsonConverters.AnyOfJsonConverterFactory2))]
[global::System.Text.Json.Serialization.JsonConverter(typeof(global::G.JsonConverters.AnyOfJsonConverter<string, global::System.Collections.Generic.IList<string>>))]
public global::G.AnyOf<string, global::System.Collections.Generic.IList<string>>? Stop { get; set; }

/// <summary>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,12 @@ internal sealed partial class JsonSerializerContextConverters
typeof(global::G.JsonConverters.UserMessageRoleNullableJsonConverter),
typeof(global::G.JsonConverters.LanguageStudioApiServerDataTypesChatChatRequestMessageDiscriminatorRoleJsonConverter),
typeof(global::G.JsonConverters.LanguageStudioApiServerDataTypesChatChatRequestMessageDiscriminatorRoleNullableJsonConverter),
typeof(global::G.JsonConverters.AnyOfJsonConverterFactory2),
typeof(global::G.JsonConverters.QueryFilterJsonConverter),
typeof(global::G.JsonConverters.MessagesItemJsonConverter),
typeof(global::G.JsonConverters.AnyOfJsonConverter<global::G.ChatStreamingFirstDelta, global::G.ChatStreamingContentDelta>),
typeof(global::G.JsonConverters.AnyOfJsonConverter<string, int?>),
typeof(global::G.JsonConverters.AnyOfJsonConverter<string, global::System.Collections.Generic.IList<string>>),
typeof(global::G.JsonConverters.AnyOfJsonConverter<global::G.ChatCompletion, global::System.Collections.Generic.IList<global::G.ChatCompletionVllmStreamingMessage>>),
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,14 @@ public sealed partial class Api : global::G.IApi, global::System.IDisposable
new global::G.JsonConverters.BlockDeltaDiscriminatorTypeNullableJsonConverter(),
new global::G.JsonConverters.MessageStreamEventDiscriminatorTypeJsonConverter(),
new global::G.JsonConverters.MessageStreamEventDiscriminatorTypeNullableJsonConverter(),
new global::G.JsonConverters.AnyOfJsonConverterFactory2(),
new global::G.JsonConverters.OneOfJsonConverterFactory2(),
new global::G.JsonConverters.BlockJsonConverter(),
new global::G.JsonConverters.ToolJsonConverter(),
new global::G.JsonConverters.MessageStreamEventJsonConverter(),
new global::G.JsonConverters.BlockDeltaJsonConverter(),
new global::G.JsonConverters.AnyOfJsonConverter<string, global::G.CreateMessageRequestModel?>(),
new global::G.JsonConverters.OneOfJsonConverter<string, global::System.Collections.Generic.IList<global::G.Block>>(),
new global::G.JsonConverters.OneOfJsonConverter<string, global::System.Collections.Generic.IList<global::G.Block>>(),
new global::G.JsonConverters.OneOfJsonConverter<string, global::System.Collections.Generic.IList<global::G.Block>>(),
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public sealed partial class CreateMessageRequest
/// </summary>
/// <example>claude-3-5-sonnet-20241022</example>
[global::System.Text.Json.Serialization.JsonPropertyName("model")]
[global::System.Text.Json.Serialization.JsonConverter(typeof(global::G.JsonConverters.AnyOfJsonConverterFactory2))]
[global::System.Text.Json.Serialization.JsonConverter(typeof(global::G.JsonConverters.AnyOfJsonConverter<string, global::G.CreateMessageRequestModel?>))]
[global::System.Text.Json.Serialization.JsonRequired]
public required global::G.AnyOf<string, global::G.CreateMessageRequestModel?> Model { get; set; }

Expand Down Expand Up @@ -135,7 +135,7 @@ public sealed partial class CreateMessageRequest
/// [guide to system prompts](https://docs.anthropic.com/en/docs/system-prompts).
/// </summary>
[global::System.Text.Json.Serialization.JsonPropertyName("system")]
[global::System.Text.Json.Serialization.JsonConverter(typeof(global::G.JsonConverters.OneOfJsonConverterFactory2))]
[global::System.Text.Json.Serialization.JsonConverter(typeof(global::G.JsonConverters.OneOfJsonConverter<string, global::System.Collections.Generic.IList<global::G.Block>>))]
public global::G.OneOf<string, global::System.Collections.Generic.IList<global::G.Block>>? System { get; set; }

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public sealed partial class Message
/// The content of the message.
/// </summary>
[global::System.Text.Json.Serialization.JsonPropertyName("content")]
[global::System.Text.Json.Serialization.JsonConverter(typeof(global::G.JsonConverters.OneOfJsonConverterFactory2))]
[global::System.Text.Json.Serialization.JsonConverter(typeof(global::G.JsonConverters.OneOfJsonConverter<string, global::System.Collections.Generic.IList<global::G.Block>>))]
[global::System.Text.Json.Serialization.JsonRequired]
public required global::G.OneOf<string, global::System.Collections.Generic.IList<global::G.Block>> Content { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public sealed partial class ToolResultBlock
/// These content blocks can use the text or image types.
/// </summary>
[global::System.Text.Json.Serialization.JsonPropertyName("content")]
[global::System.Text.Json.Serialization.JsonConverter(typeof(global::G.JsonConverters.OneOfJsonConverterFactory2))]
[global::System.Text.Json.Serialization.JsonConverter(typeof(global::G.JsonConverters.OneOfJsonConverter<string, global::System.Collections.Generic.IList<global::G.Block>>))]
[global::System.Text.Json.Serialization.JsonRequired]
public required global::G.OneOf<string, global::System.Collections.Generic.IList<global::G.Block>> Content { get; set; }

Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,14 @@ internal sealed partial class JsonSerializerContextConverters
typeof(global::G.JsonConverters.BlockDeltaDiscriminatorTypeNullableJsonConverter),
typeof(global::G.JsonConverters.MessageStreamEventDiscriminatorTypeJsonConverter),
typeof(global::G.JsonConverters.MessageStreamEventDiscriminatorTypeNullableJsonConverter),
typeof(global::G.JsonConverters.AnyOfJsonConverterFactory2),
typeof(global::G.JsonConverters.OneOfJsonConverterFactory2),
typeof(global::G.JsonConverters.BlockJsonConverter),
typeof(global::G.JsonConverters.ToolJsonConverter),
typeof(global::G.JsonConverters.MessageStreamEventJsonConverter),
typeof(global::G.JsonConverters.BlockDeltaJsonConverter),
typeof(global::G.JsonConverters.AnyOfJsonConverter<string, global::G.CreateMessageRequestModel?>),
typeof(global::G.JsonConverters.OneOfJsonConverter<string, global::System.Collections.Generic.IList<global::G.Block>>),
typeof(global::G.JsonConverters.OneOfJsonConverter<string, global::System.Collections.Generic.IList<global::G.Block>>),
typeof(global::G.JsonConverters.OneOfJsonConverter<string, global::System.Collections.Generic.IList<global::G.Block>>),
};
}
}
Loading

0 comments on commit f630ae0

Please sign in to comment.