Skip to content

Commit f630ae0

Browse files
committed
feat: Replaced AnyOf JsonConverterFactory with explicit generic converters.
1 parent 7d8f442 commit f630ae0

File tree

250 files changed

+1174
-1391
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

250 files changed

+1174
-1391
lines changed

src/libs/AutoSDK.CLI/Resources/.github_workflows_auto-update.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ jobs:
2929
git checkout -b ${{ steps.branch.outputs.branch_name }} origin/main
3030
git rebase main
3131
32+
- name: Install .NET SDK
33+
uses: actions/setup-dotnet@v4
34+
with:
35+
dotnet-version: 9.0.x
36+
3237
- name: Generate code
3338
run: |
3439
cd src/libs/$SolutionName$

src/libs/AutoSDK.SourceGenerators/SdkGenerator.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,11 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
121121
.SelectAndReportExceptions((x, c) => Sources.AnyOfJsonConverter(x, c)
122122
.AsFileWithName(), context, Id)
123123
.AddSource(context);
124-
data
125-
.SelectMany(static (x, _) => x.AnyOfs)
126-
.SelectAndReportExceptions((x, c) => Sources.AnyOfJsonConverterFactory(x, c)
127-
.AsFileWithName(), context, Id)
128-
.AddSource(context);
124+
// data
125+
// .SelectMany(static (x, _) => x.AnyOfs)
126+
// .SelectAndReportExceptions((x, c) => Sources.AnyOfJsonConverterFactory(x, c)
127+
// .AsFileWithName(), context, Id)
128+
// .AddSource(context);
129129

130130
data
131131
.Select(static (x, _) => x.Converters)

src/libs/AutoSDK/Models/TypeData.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,11 @@ CSharpTypeWithoutNullability is "string" ||
7575
: IsEnum || (IsAnyOfLike && (IsComponent || HasDiscriminator))
7676
? $"global::{Settings.Namespace}.JsonConverters.{ShortCSharpTypeWithoutNullability}JsonConverter"
7777
: AnyOfCount > 0
78-
? $"global::{Settings.Namespace}.JsonConverters.AnyOfJsonConverterFactory{AnyOfCount}"
78+
? $"global::{Settings.Namespace}.JsonConverters.AnyOfJsonConverter<{string.Join(", ", SubTypes.Select(y => y.CSharpTypeWithNullabilityForValueTypes))}>"
7979
: OneOfCount > 0
80-
? $"global::{Settings.Namespace}.JsonConverters.OneOfJsonConverterFactory{OneOfCount}"
80+
? $"global::{Settings.Namespace}.JsonConverters.OneOfJsonConverter<{string.Join(", ", SubTypes.Select(y => y.CSharpTypeWithNullabilityForValueTypes))}>"
8181
: AllOfCount > 0
82-
? $"global::{Settings.Namespace}.JsonConverters.AllOfJsonConverterFactory{AllOfCount}"
82+
? $"global::{Settings.Namespace}.JsonConverters.AllOfJsonConverter<{string.Join(", ", SubTypes.Select(y => y.CSharpTypeWithNullabilityForValueTypes))}>"
8383
: string.Empty;
8484

8585
public static TypeData FromSchemaContext(SchemaContext context)

src/libs/AutoSDK/Sources/Data.cs

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -200,20 +200,33 @@ public static Models.Data Prepare(
200200
.Select(x => Authorization.FromOpenApiSecurityScheme(x.Key, settings))
201201
.ToArray();
202202

203-
var converters = enums
204-
.Where(x =>
205-
x.Style == ModelStyle.Enumeration &&
206-
x.Settings.JsonSerializerType != JsonSerializerType.NewtonsoftJson)
207-
.SelectMany(x => new[]
208-
{
209-
$"global::{settings.Namespace}.JsonConverters.{x.ClassName}JsonConverter",
210-
$"global::{settings.Namespace}.JsonConverters.{x.ClassName}NullableJsonConverter"
211-
})
203+
var converters =
204+
// Enum converters
205+
enums
206+
.Where(x =>
207+
x.Style == ModelStyle.Enumeration &&
208+
x.Settings.JsonSerializerType != JsonSerializerType.NewtonsoftJson)
209+
.SelectMany(x => new[]
210+
{
211+
$"global::{settings.Namespace}.JsonConverters.{x.ClassName}JsonConverter",
212+
$"global::{settings.Namespace}.JsonConverters.{x.ClassName}NullableJsonConverter"
213+
})
214+
// Named AnyOf converters
212215
.Concat(anyOfDatas
213-
.Where(x => x.Settings.JsonSerializerType == JsonSerializerType.SystemTextJson)
214-
.Select(x => string.IsNullOrWhiteSpace(x.Name)
215-
? $"global::{settings.Namespace}.JsonConverters.{x.SubType}JsonConverterFactory{x.Count}"
216-
: $"global::{settings.Namespace}.JsonConverters.{x.Name}JsonConverter"))
216+
.Where(x =>
217+
x.Settings.JsonSerializerType == JsonSerializerType.SystemTextJson &&
218+
!string.IsNullOrWhiteSpace(x.Name))
219+
.Select(x => $"global::{settings.Namespace}.JsonConverters.{x.Name}JsonConverter"))
220+
// Generic AnyOf converters
221+
.Concat(filteredSchemas
222+
.Where(x =>
223+
x.Settings.JsonSerializerType == JsonSerializerType.SystemTextJson &&
224+
x.AnyOfData.HasValue &&
225+
string.IsNullOrWhiteSpace(x.AnyOfData.Value.Name))
226+
.Select(x => $"global::{settings.Namespace}.JsonConverters.{x.AnyOfData?.SubType}JsonConverter<{
227+
string.Join(", ", x.Children
228+
.Where(y => y.Hint is Hint.AnyOf or Hint.OneOf or Hint.AllOf)
229+
.Select(y => y.TypeData.CSharpTypeWithNullabilityForValueTypes))}>"))
217230
.ToImmutableArray();
218231

219232
var includedTags = allTags

src/libs/AutoSDK/Sources/Sources.cs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -139,20 +139,21 @@ public static FileWithName EnumNullableJsonConverter(
139139
Text: GenerateEnumNullableJsonConverter(data, cancellationToken: cancellationToken));
140140
}
141141

142-
public static FileWithName AnyOfJsonConverterFactory(
143-
AnyOfData anyOf,
144-
CancellationToken cancellationToken = default)
145-
{
146-
if (anyOf.Settings.JsonSerializerType == JsonSerializerType.NewtonsoftJson ||
147-
anyOf.IsNamed)
148-
{
149-
return FileWithName.Empty;
150-
}
151-
152-
return new FileWithName(
153-
Name: $"JsonConverters.{anyOf.SubType}Factory{anyOf.Count}.g.cs",
154-
Text: GenerateAnyOfJsonConverterFactory(anyOf, cancellationToken: cancellationToken));
155-
}
142+
// Not used in the current implementation because not compatible with NativeAOT
143+
// public static FileWithName AnyOfJsonConverterFactory(
144+
// AnyOfData anyOf,
145+
// CancellationToken cancellationToken = default)
146+
// {
147+
// if (anyOf.Settings.JsonSerializerType == JsonSerializerType.NewtonsoftJson ||
148+
// anyOf.IsNamed)
149+
// {
150+
// return FileWithName.Empty;
151+
// }
152+
//
153+
// return new FileWithName(
154+
// Name: $"JsonConverters.{anyOf.SubType}Factory{anyOf.Count}.g.cs",
155+
// Text: GenerateAnyOfJsonConverterFactory(anyOf, cancellationToken: cancellationToken));
156+
// }
156157

157158
public static FileWithName UnixTimestampJsonConverter(
158159
Settings settings,

src/tests/AutoSDK.SnapshotTests/Snapshots/Ai21/SystemTextJson/_#G.Api.g.verified.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,12 @@ public sealed partial class Api : global::G.IApi, global::System.IDisposable
9191
new global::G.JsonConverters.UserMessageRoleNullableJsonConverter(),
9292
new global::G.JsonConverters.LanguageStudioApiServerDataTypesChatChatRequestMessageDiscriminatorRoleJsonConverter(),
9393
new global::G.JsonConverters.LanguageStudioApiServerDataTypesChatChatRequestMessageDiscriminatorRoleNullableJsonConverter(),
94-
new global::G.JsonConverters.AnyOfJsonConverterFactory2(),
9594
new global::G.JsonConverters.QueryFilterJsonConverter(),
9695
new global::G.JsonConverters.MessagesItemJsonConverter(),
96+
new global::G.JsonConverters.AnyOfJsonConverter<global::G.ChatStreamingFirstDelta, global::G.ChatStreamingContentDelta>(),
97+
new global::G.JsonConverters.AnyOfJsonConverter<string, int?>(),
98+
new global::G.JsonConverters.AnyOfJsonConverter<string, global::System.Collections.Generic.IList<string>>(),
99+
new global::G.JsonConverters.AnyOfJsonConverter<global::G.ChatCompletion, global::System.Collections.Generic.IList<global::G.ChatCompletionVllmStreamingMessage>>(),
97100
}
98101
};
99102

src/tests/AutoSDK.SnapshotTests/Snapshots/Ai21/SystemTextJson/_#G.Models.ChatCompletionResponseDeltaChoice.g.verified.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public sealed partial class ChatCompletionResponseDeltaChoice
2323
/// - **Subsequent messages** will have an object `{"content": __token__}` with the generated token.
2424
/// </summary>
2525
[global::System.Text.Json.Serialization.JsonPropertyName("delta")]
26-
[global::System.Text.Json.Serialization.JsonConverter(typeof(global::G.JsonConverters.AnyOfJsonConverterFactory2))]
26+
[global::System.Text.Json.Serialization.JsonConverter(typeof(global::G.JsonConverters.AnyOfJsonConverter<global::G.ChatStreamingFirstDelta, global::G.ChatStreamingContentDelta>))]
2727
[global::System.Text.Json.Serialization.JsonRequired]
2828
public required global::G.AnyOf<global::G.ChatStreamingFirstDelta, global::G.ChatStreamingContentDelta> Delta { get; set; }
2929

src/tests/AutoSDK.SnapshotTests/Snapshots/Ai21/SystemTextJson/_#G.Models.LanguageStudioApiServerDataTypesChatChatRequest.g.verified.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public sealed partial class LanguageStudioApiServerDataTypesChatChatRequest
6060
///
6161
/// </summary>
6262
[global::System.Text.Json.Serialization.JsonPropertyName("stop")]
63-
[global::System.Text.Json.Serialization.JsonConverter(typeof(global::G.JsonConverters.AnyOfJsonConverterFactory2))]
63+
[global::System.Text.Json.Serialization.JsonConverter(typeof(global::G.JsonConverters.AnyOfJsonConverter<string, global::System.Collections.Generic.IList<string>>))]
6464
public global::G.AnyOf<string, global::System.Collections.Generic.IList<string>>? Stop { get; set; }
6565

6666
/// <summary>

src/tests/AutoSDK.SnapshotTests/Snapshots/Ai21/SystemTextJson/_#JsonConverters.AnyOfFactory2.g.verified.cs

Lines changed: 0 additions & 26 deletions
This file was deleted.

src/tests/AutoSDK.SnapshotTests/Snapshots/Ai21/SystemTextJson/_#JsonSerializerContextConverters.g.verified.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,12 @@ internal sealed partial class JsonSerializerContextConverters
6161
typeof(global::G.JsonConverters.UserMessageRoleNullableJsonConverter),
6262
typeof(global::G.JsonConverters.LanguageStudioApiServerDataTypesChatChatRequestMessageDiscriminatorRoleJsonConverter),
6363
typeof(global::G.JsonConverters.LanguageStudioApiServerDataTypesChatChatRequestMessageDiscriminatorRoleNullableJsonConverter),
64-
typeof(global::G.JsonConverters.AnyOfJsonConverterFactory2),
6564
typeof(global::G.JsonConverters.QueryFilterJsonConverter),
6665
typeof(global::G.JsonConverters.MessagesItemJsonConverter),
66+
typeof(global::G.JsonConverters.AnyOfJsonConverter<global::G.ChatStreamingFirstDelta, global::G.ChatStreamingContentDelta>),
67+
typeof(global::G.JsonConverters.AnyOfJsonConverter<string, int?>),
68+
typeof(global::G.JsonConverters.AnyOfJsonConverter<string, global::System.Collections.Generic.IList<string>>),
69+
typeof(global::G.JsonConverters.AnyOfJsonConverter<global::G.ChatCompletion, global::System.Collections.Generic.IList<global::G.ChatCompletionVllmStreamingMessage>>),
6770
};
6871
}
6972
}

0 commit comments

Comments
 (0)