Skip to content

Commit 442f238

Browse files
committed
fix: Fixed incorrect generation of SourceGenerationContext names
1 parent 80f7292 commit 442f238

File tree

592 files changed

+2167
-1397
lines changed

Some content is hidden

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

592 files changed

+2167
-1397
lines changed

src/libs/OpenApiGenerator.Core/Json/SystemTextJsonSerializer.cs

Lines changed: 37 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -52,73 +52,56 @@ public string GenerateConverterAttribute(string type)
5252
return $"[global::System.Text.Json.Serialization.JsonConverter(typeof({type}))]";
5353
}
5454

55-
private static readonly char[] ContextTypeSeparators = [',', '<', '>', '['];
56-
57-
public static string GetContextType(string type)
55+
public static string GetContextType(TypeData typeData, bool makeNullableRootIfValueType)
5856
{
59-
type = type ?? throw new ArgumentNullException(nameof(type));
60-
61-
var result = string.Concat(type
62-
.Replace("global::", string.Empty)
63-
.Replace("?", string.Empty)
64-
.Replace("System.Collections.Generic.", string.Empty)
65-
.Replace("System.", string.Empty)
66-
.Split(ContextTypeSeparators, StringSplitOptions.RemoveEmptyEntries)
67-
.Select(x => x.Trim() switch
68-
{
69-
"]" => "Array",
70-
"bool" => "Boolean",
71-
"short" => "Int16",
72-
"int" => "Int32",
73-
"long" => "Int64",
74-
"sbyte" => "SByte",
75-
"ushort" => "UInt16",
76-
"uint" => "UInt32",
77-
"ulong" => "UInt64",
78-
"float" => "Single",
79-
"double" => "Double",
80-
"decimal" => "Decimal",
81-
"string" => "String",
82-
"char" => "Char",
83-
"byte" => "Byte",
84-
"object" => "Object",
85-
_ => x.Trim(),
86-
}));
87-
result =
88-
((result.StartsWith("Boolean", StringComparison.Ordinal) ||
89-
result.StartsWith("Int16", StringComparison.Ordinal) ||
90-
result.StartsWith("Int32", StringComparison.Ordinal) ||
91-
result.StartsWith("Int64", StringComparison.Ordinal) ||
92-
result.StartsWith("SByte", StringComparison.Ordinal) ||
93-
result.StartsWith("UInt16", StringComparison.Ordinal) ||
94-
result.StartsWith("UInt32", StringComparison.Ordinal) ||
95-
result.StartsWith("UInt64", StringComparison.Ordinal) ||
96-
result.Equals("Single", StringComparison.Ordinal) ||
97-
result.Equals("Double", StringComparison.Ordinal) ||
98-
result.Equals("Decimal", StringComparison.Ordinal) ||
99-
result.Equals("Char", StringComparison.Ordinal) ||
100-
result.Equals("Byte", StringComparison.Ordinal)) &&
101-
!result.EndsWith("Array", StringComparison.Ordinal)) ||
102-
result.StartsWith("AnyOf", StringComparison.Ordinal) ||
103-
result.StartsWith("OneOf", StringComparison.Ordinal) ||
104-
result.StartsWith("AllOf", StringComparison.Ordinal)
105-
? "Nullable" + result
106-
: result;
57+
var shortTypeWithoutSubTypes = typeData.ShortCSharpTypeWithoutNullability.Contains("<") ?
58+
typeData.ShortCSharpTypeWithoutNullability[..typeData.ShortCSharpTypeWithoutNullability.IndexOf('<')] :
59+
typeData.ShortCSharpTypeWithoutNullability;
10760

108-
return result;
61+
return (typeData.IsValueType &&
62+
(typeData.CSharpType.EndsWith("?", StringComparison.Ordinal) ||
63+
makeNullableRootIfValueType) ? "Nullable" : string.Empty) + typeData switch
64+
{
65+
_ when typeData.IsBase64 || typeData.IsBinary => string.Empty,
66+
{ IsArray: true, SubTypes.Length: 1 } => "IList",
67+
{ AnyOfCount: > 0, IsComponent: false } => "AnyOf",
68+
{ OneOfCount: > 0, IsComponent: false } => "OneOf",
69+
{ AllOfCount: > 0, IsComponent: false } => "AllOf",
70+
{ ShortCSharpTypeWithoutNullability: "bool" } => "Boolean",
71+
{ ShortCSharpTypeWithoutNullability: "string" } => "String",
72+
{ ShortCSharpTypeWithoutNullability: "short" } => "Int16",
73+
{ ShortCSharpTypeWithoutNullability: "int" } => "Int32",
74+
{ ShortCSharpTypeWithoutNullability: "long" } => "Int64",
75+
{ ShortCSharpTypeWithoutNullability: "sbyte" } => "SByte",
76+
{ ShortCSharpTypeWithoutNullability: "ushort" } => "UInt16",
77+
{ ShortCSharpTypeWithoutNullability: "uint" } => "UInt32",
78+
{ ShortCSharpTypeWithoutNullability: "ulong" } => "UInt64",
79+
{ ShortCSharpTypeWithoutNullability: "float" } => "Single",
80+
{ ShortCSharpTypeWithoutNullability: "double" } => "Double",
81+
{ ShortCSharpTypeWithoutNullability: "decimal" } => "Decimal",
82+
{ ShortCSharpTypeWithoutNullability: "char" } => "Char",
83+
{ ShortCSharpTypeWithoutNullability: "byte" } => "Byte",
84+
{ ShortCSharpTypeWithoutNullability: "object" } => "Object",
85+
_ => shortTypeWithoutSubTypes,
86+
} + (typeData is { IsComponent: true, IsAnyOf: true }
87+
? string.Empty
88+
: string.Concat(typeData.SubTypes.Select(x => GetContextType(x, makeNullableRootIfValueType: false))))
89+
+ (typeData.IsBase64 || typeData.IsBinary
90+
? "Array"
91+
: string.Empty);
10992
}
11093

11194
public string GenerateSerializeCall(TypeData type, string jsonSerializerContext)
11295
{
11396
return string.IsNullOrWhiteSpace(jsonSerializerContext)
11497
? "global::System.Text.Json.JsonSerializer.Serialize(request, _jsonSerializerOptions)"
115-
: $"global::System.Text.Json.JsonSerializer.Serialize(request, global::{jsonSerializerContext}.Default.{GetContextType(type.ShortCSharpTypeWithoutNullability)})";
98+
: $"global::System.Text.Json.JsonSerializer.Serialize(request, global::{jsonSerializerContext}.Default.{GetContextType(type, makeNullableRootIfValueType: true)})";
11699
}
117100

118101
public string GenerateDeserializeCall(TypeData type, string jsonSerializerContext)
119102
{
120103
return string.IsNullOrWhiteSpace(jsonSerializerContext)
121104
? $"global::System.Text.Json.JsonSerializer.Deserialize<{type.CSharpTypeWithNullability}>(__content, _jsonSerializerOptions)"
122-
: $"global::System.Text.Json.JsonSerializer.Deserialize(__content, global::{jsonSerializerContext}.Default.{GetContextType(type.ShortCSharpTypeWithNullability)})";
105+
: $"global::System.Text.Json.JsonSerializer.Deserialize(__content, global::{jsonSerializerContext}.Default.{GetContextType(type, makeNullableRootIfValueType: true)})";
123106
}
124107
}

src/libs/OpenApiGenerator.Core/Models/TypeData.cs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,24 @@ public static TypeData FromSchemaContext(SchemaContext context)
107107
}
108108
if (context.Schema.IsArray())
109109
{
110-
subTypes = [Default with
111-
{
112-
IsEnum = context.Schema.Items.IsEnum(),
113-
}];
110+
subTypes = [
111+
context.Children
112+
.FirstOrDefault(x => x is { Hint: Hint.ArrayItem, TypeData: not null })
113+
?.TypeData ??
114+
Default with
115+
{
116+
IsEnum = context.Schema.Items.IsEnum(),
117+
},
118+
];
119+
}
120+
if (context.Schema.IsBinary() || context.Schema.IsBase64())
121+
{
122+
subTypes = [
123+
Default with
124+
{
125+
CSharpType = "byte",
126+
},
127+
];
114128
}
115129

116130
var enumValues = ImmutableArray<string>.Empty;
@@ -129,7 +143,7 @@ public static TypeData FromSchemaContext(SchemaContext context)
129143
return new TypeData(
130144
CSharpType: GetCSharpType(context),
131145
IsValueType: ContextIsValueType(context),
132-
IsArray: context.Schema.Type == "array",
146+
IsArray: context.Schema.IsArray(),
133147
IsEnum: context.Schema.IsEnum(),
134148
IsBase64: context.Schema.IsBase64(),
135149
IsDate: context.Schema.IsDate(),
@@ -160,7 +174,6 @@ public static bool ContextIsValueType(SchemaContext context)
160174
("boolean", _) => true,
161175
("integer", _) => true,
162176
("number", _) => true,
163-
("string", null) => true,
164177
("string", "date") => true,
165178
("string", "date-time") => true,
166179
("string", "password") => true,

src/tests/OpenApiGenerator.SnapshotTests/Snapshots/Ai21/NewtonsoftJson/_#G.JambaCompleteClient.V1ChatComplete.g.verified.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ partial void ProcessV1ChatCompleteResponseContent(
155155
int maxTokens = 4096,
156156
double temperature = default,
157157
double topP = 1,
158-
global::System.AnyOf<string?, global::System.Collections.Generic.IList<string>>? stop = default,
158+
global::System.AnyOf<string, global::System.Collections.Generic.IList<string>>? stop = default,
159159
bool stream = false,
160160
global::G.MockResponseConfig? mockResponse = default,
161161
global::System.Collections.Generic.IList<global::G.DocumentSchema>? documents = default,

src/tests/OpenApiGenerator.SnapshotTests/Snapshots/Ai21/NewtonsoftJson/_#G.Models.LanguageStudioApiServerDataTypesChatChatRequest.g.verified.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public sealed partial class LanguageStudioApiServerDataTypesChatChatRequest
5757
///
5858
/// </summary>
5959
[global::Newtonsoft.Json.JsonProperty("stop")]
60-
public global::System.AnyOf<string?, global::System.Collections.Generic.IList<string>>? Stop { get; set; }
60+
public global::System.AnyOf<string, global::System.Collections.Generic.IList<string>>? Stop { get; set; }
6161

6262
/// <summary>
6363
/// Whether or not to stream the result one token at a time using<br/>

src/tests/OpenApiGenerator.SnapshotTests/Snapshots/Ai21/NewtonsoftJson/_#G.Models.ValidationError.g.verified.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public sealed partial class ValidationError
1515
///
1616
/// </summary>
1717
[global::Newtonsoft.Json.JsonProperty("loc", Required = global::Newtonsoft.Json.Required.Always)]
18-
public global::System.Collections.Generic.IList<global::System.AnyOf<string?, int?>> Loc { get; set; } = default!;
18+
public global::System.Collections.Generic.IList<global::System.AnyOf<string, int?>> Loc { get; set; } = default!;
1919

2020
/// <summary>
2121
///

src/tests/OpenApiGenerator.SnapshotTests/Snapshots/Ai21/SystemTextJson/_#G.JambaCompleteClient.V1ChatComplete.g.verified.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ partial void ProcessV1ChatCompleteResponseContent(
155155
int maxTokens = 4096,
156156
double temperature = default,
157157
double topP = 1,
158-
global::System.AnyOf<string?, global::System.Collections.Generic.IList<string>>? stop = default,
158+
global::System.AnyOf<string, global::System.Collections.Generic.IList<string>>? stop = default,
159159
bool stream = false,
160160
global::G.MockResponseConfig? mockResponse = default,
161161
global::System.Collections.Generic.IList<global::G.DocumentSchema>? documents = default,

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

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

6666
/// <summary>
6767
/// Whether or not to stream the result one token at a time using<br/>

src/tests/OpenApiGenerator.SnapshotTests/Snapshots/Ai21/SystemTextJson/_#G.Models.ValidationError.g.verified.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public sealed partial class ValidationError
1616
/// </summary>
1717
[global::System.Text.Json.Serialization.JsonPropertyName("loc")]
1818
[global::System.Text.Json.Serialization.JsonRequired]
19-
public required global::System.Collections.Generic.IList<global::System.AnyOf<string?, int?>> Loc { get; set; }
19+
public required global::System.Collections.Generic.IList<global::System.AnyOf<string, int?>> Loc { get; set; }
2020

2121
/// <summary>
2222
///

src/tests/OpenApiGenerator.SnapshotTests/Snapshots/Anthropic/NewtonsoftJson/_#G.Api.CreateMessage.g.verified.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ partial void ProcessCreateMessageResponseContent(
308308
/// <param name="cancellationToken">The token to cancel the operation with</param>
309309
/// <exception cref="global::System.InvalidOperationException"></exception>
310310
public async global::System.Threading.Tasks.Task<global::G.Message> CreateMessageAsync(
311-
global::System.AnyOf<string?, global::G.CreateMessageRequestModel?> model,
311+
global::System.AnyOf<string, global::G.CreateMessageRequestModel?> model,
312312
global::System.Collections.Generic.IList<global::G.Message> messages,
313313
int maxTokens,
314314
global::G.CreateMessageRequestMetadata? metadata = default,

src/tests/OpenApiGenerator.SnapshotTests/Snapshots/Anthropic/NewtonsoftJson/_#G.Models.CreateMessageRequest.g.verified.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public sealed partial class CreateMessageRequest
1818
/// Example: claude-3-5-sonnet-20240620
1919
/// </summary>
2020
[global::Newtonsoft.Json.JsonProperty("model", Required = global::Newtonsoft.Json.Required.Always)]
21-
public global::System.AnyOf<string?, global::G.CreateMessageRequestModel?> Model { get; set; } = default!;
21+
public global::System.AnyOf<string, global::G.CreateMessageRequestModel?> Model { get; set; } = default!;
2222

2323
/// <summary>
2424
/// Input messages.<br/>

0 commit comments

Comments
 (0)