Skip to content

Commit

Permalink
fix: Small fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
HavenDV committed Jun 13, 2024
1 parent da77c5c commit 5284865
Show file tree
Hide file tree
Showing 63 changed files with 5,346 additions and 26 deletions.
12 changes: 6 additions & 6 deletions src/libs/OpenApiGenerator.Core/Generation/Data.cs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ .. includedTags.Select(x => PropertyData.Default with
.Select(x => new AnyOfData("AllOf", x.Items.AllOf.Count, settings.JsonSerializerType, isTrimming, "System", string.Empty, string.Empty, ImmutableArray<TypeData>.Empty)))
.Distinct()
.ToImmutableArray();
anyOfs = anyOfs
anyOfs = settings.GenerateSdk || settings.GenerateModels ? anyOfs
.Concat(includedSchemas
.Where(x => x.Value.AnyOf is { Count: >0 })
.Select(schema => new AnyOfData(
Expand All @@ -232,8 +232,8 @@ .. includedTags.Select(x => PropertyData.Default with
schema.Key,
schema.Value.GetSummary(),
schema.Value.AnyOf.Select((x, i) => TypeData.FromSchema(x.UseReferenceIdOrKey(schema.Key + $"Variant{i + 1}"), settings)).ToImmutableArray())))
.ToImmutableArray();
oneOfs = oneOfs
.ToImmutableArray() : [];
oneOfs = settings.GenerateSdk || settings.GenerateModels ? oneOfs
.Concat(includedSchemas
.Where(x => x.Value.OneOf is { Count: >0 })
.Select(schema => new AnyOfData(
Expand All @@ -245,8 +245,8 @@ .. includedTags.Select(x => PropertyData.Default with
schema.Key,
schema.Value.GetSummary(),
schema.Value.OneOf.Select((x, i) => TypeData.FromSchema(x.UseReferenceIdOrKey(schema.Key + $"Variant{i + 1}"), settings)).ToImmutableArray())))
.ToImmutableArray();
allOfs = allOfs
.ToImmutableArray() : [];
allOfs = settings.GenerateSdk || settings.GenerateModels ? allOfs
.Concat(includedSchemas
.Where(x => x.Value.AllOf is { Count: >0 })
.Select(schema => new AnyOfData(
Expand All @@ -258,7 +258,7 @@ .. includedTags.Select(x => PropertyData.Default with
schema.Key,
schema.Value.GetSummary(),
schema.Value.AllOf.Select((x, i) => TypeData.FromSchema(x.UseReferenceIdOrKey(schema.Key + $"Variant{i + 1}"), settings)).ToImmutableArray())))
.ToImmutableArray();
.ToImmutableArray() : [];

AnyOfData[] anyOfDatas =
[
Expand Down
3 changes: 2 additions & 1 deletion src/libs/OpenApiGenerator.Core/Generation/Sources.AnyOf.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ public static string GenerateAnyOf(
}}" : " ";

return $@"using System.Linq;
{(fixedTypes.IsEmpty ? "" : @"#pragma warning disable CS0618 // Type or member is obsolete
")}
#nullable enable
namespace {@namespace}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ public static string GenerateAnyOfJsonConverter(
}).ToImmutableArray();

return $@"#nullable enable
{(fixedTypes.IsEmpty ? "" : @"#pragma warning disable CS0618 // Type or member is obsolete
")}
namespace OpenApiGenerator.JsonConverters
{{
/// <inheritdoc />
Expand Down
6 changes: 3 additions & 3 deletions src/libs/OpenApiGenerator.Core/Generation/Sources.Methods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public static string GenerateMethod(
requestUri: new global::System.Uri(_httpClient.BaseAddress?.AbsoluteUri + {endPoint.Path}, global::System.UriKind.RelativeOrAbsolute));
{(string.IsNullOrWhiteSpace(endPoint.RequestType.CSharpType) ? " " : $@"
httpRequest.Content = new global::System.Net.Http.StringContent(
content: {jsonSerializer.GenerateSerializeCall(endPoint.RequestType.CSharpTypeWithoutNullability, endPoint.JsonSerializerContext)},
content: {jsonSerializer.GenerateSerializeCall(endPoint.RequestType, endPoint.JsonSerializerContext)},
encoding: global::System.Text.Encoding.UTF8,
mediaType: ""application/json"");")}
Expand All @@ -117,7 +117,7 @@ public static string GenerateMethod(
var __content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
return
{jsonSerializer.GenerateDeserializeCall(endPoint.ResponseType.CSharpTypeWithNullability, endPoint.JsonSerializerContext)} ??
{jsonSerializer.GenerateDeserializeCall(endPoint.ResponseType, endPoint.JsonSerializerContext)} ??
throw new global::System.InvalidOperationException($""Response deserialization failed for \""{{__content}}\"" "");")}
{(endPoint.Stream ? $@"
using var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);
Expand All @@ -126,7 +126,7 @@ public static string GenerateMethod(
while (!reader.EndOfStream && !cancellationToken.IsCancellationRequested)
{{
var __content = await reader.ReadLineAsync().ConfigureAwait(false) ?? string.Empty;
var streamedResponse = {jsonSerializer.GenerateDeserializeCall(endPoint.ResponseType.CSharpTypeWithNullability, endPoint.JsonSerializerContext)} ??
var streamedResponse = {jsonSerializer.GenerateDeserializeCall(endPoint.ResponseType, endPoint.JsonSerializerContext)} ??
throw new global::System.InvalidOperationException($""Response deserialization failed for \""{{__content}}\"" "");
yield return streamedResponse;
Expand Down
6 changes: 4 additions & 2 deletions src/libs/OpenApiGenerator.Core/Json/IJsonSerializer.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
using OpenApiGenerator.Core.Models;

namespace OpenApiGenerator.Core.Json;

public interface IJsonSerializer
{
public string GeneratePropertyAttribute(string id, bool isRequired);
string GenerateExtensionDataAttribute();
string GenerateRequiredAttribute();
string GenerateDeserializeCall(string type, string jsonSerializerContext);
string GenerateSerializeCall(string type, string jsonSerializerContext);
string GenerateDeserializeCall(TypeData type, string jsonSerializerContext);
string GenerateSerializeCall(TypeData type, string jsonSerializerContext);
string GenerateConverterAttribute(string type);
string GetOptionsType();
string CreateDefaultSettings(IReadOnlyList<string> converters);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using OpenApiGenerator.Core.Extensions;
using OpenApiGenerator.Core.Models;

namespace OpenApiGenerator.Core.Json;

Expand Down Expand Up @@ -37,13 +38,13 @@ public string GenerateConverterAttribute(string type)
return string.Empty;
}

public string GenerateSerializeCall(string type, string jsonSerializerContext)
public string GenerateSerializeCall(TypeData type, string jsonSerializerContext)
{
return "global::Newtonsoft.Json.JsonConvert.SerializeObject(request, _jsonSerializerOptions)";
}

public string GenerateDeserializeCall(string type, string jsonSerializerContext)
public string GenerateDeserializeCall(TypeData type, string jsonSerializerContext)
{
return $"global::Newtonsoft.Json.JsonConvert.DeserializeObject<{type}>(__content, _jsonSerializerOptions)";
return $"global::Newtonsoft.Json.JsonConvert.DeserializeObject<{type.CSharpTypeWithNullability}>(__content, _jsonSerializerOptions)";
}
}
15 changes: 6 additions & 9 deletions src/libs/OpenApiGenerator.Core/Json/SystemTextJsonSerializer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using OpenApiGenerator.Core.Extensions;
using OpenApiGenerator.Core.Models;

namespace OpenApiGenerator.Core.Json;

Expand Down Expand Up @@ -93,21 +94,17 @@ public static string GetContextType(string type)
return result;
}

public string GenerateSerializeCall(string type, string jsonSerializerContext)
public string GenerateSerializeCall(TypeData type, string jsonSerializerContext)
{
type = type ?? throw new ArgumentNullException(nameof(type));

return string.IsNullOrWhiteSpace(jsonSerializerContext)
? "global::System.Text.Json.JsonSerializer.Serialize(request, _jsonSerializerOptions)"
: $"global::System.Text.Json.JsonSerializer.Serialize(request, global::{jsonSerializerContext}.Default.{GetContextType(type)})";
: $"global::System.Text.Json.JsonSerializer.Serialize(request, global::{jsonSerializerContext}.Default.{GetContextType(type.ShortCSharpTypeWithoutNullability)})";
}

public string GenerateDeserializeCall(string type, string jsonSerializerContext)
public string GenerateDeserializeCall(TypeData type, string jsonSerializerContext)
{
type = type ?? throw new ArgumentNullException(nameof(type));

return string.IsNullOrWhiteSpace(jsonSerializerContext)
? $"global::System.Text.Json.JsonSerializer.Deserialize<{type}>(__content, _jsonSerializerOptions)"
: $"global::System.Text.Json.JsonSerializer.Deserialize(__content, global::{jsonSerializerContext}.Default.{GetContextType(type)})";
? $"global::System.Text.Json.JsonSerializer.Deserialize<{type.CSharpTypeWithNullability}>(__content, _jsonSerializerOptions)"
: $"global::System.Text.Json.JsonSerializer.Deserialize(__content, global::{jsonSerializerContext}.Default.{GetContextType(type.ShortCSharpTypeWithNullability)})";
}
}
4 changes: 3 additions & 1 deletion src/libs/OpenApiGenerator.Core/Models/TypeData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,12 @@ public readonly record struct TypeData(

public string CSharpTypeWithoutNullability => CSharpType.TrimEnd('?');
public string CSharpTypeWithNullability => CSharpTypeWithoutNullability + "?";
public string ShortCSharpTypeWithoutNullability => CSharpTypeWithoutNullability.Replace($"global::{Namespace}.", string.Empty);
public string ShortCSharpTypeWithNullability => ShortCSharpTypeWithoutNullability + "?";
public bool IsAnyOf => AnyOfCount > 0 || OneOfCount > 0 || AllOfCount > 0;

public string ConverterType => IsEnum
? $"global::OpenApiGenerator.JsonConverters.{CSharpTypeWithoutNullability.Replace($"global::{Namespace}.", string.Empty)}JsonConverter"
? $"global::OpenApiGenerator.JsonConverters.{ShortCSharpTypeWithoutNullability}JsonConverter"
: AnyOfCount > 0
? $"global::OpenApiGenerator.JsonConverters.AnyOfJsonConverterFactory{AnyOfCount}"
: OneOfCount > 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<Import Project="../../libs/OpenApiGenerator/OpenApiGenerator.props" />

<PropertyGroup>
<OpenApiGenerator_Namespace>OpenApiGenerator.IntegrationTests</OpenApiGenerator_Namespace>
<OpenApiGenerator_GenerateSdk>false</OpenApiGenerator_GenerateSdk>
<OpenApiGenerator_GenerateMethods>true</OpenApiGenerator_GenerateMethods>
<OpenApiGenerator_GenerateConstructors>true</OpenApiGenerator_GenerateConstructors>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<Import Project="../../libs/OpenApiGenerator/OpenApiGenerator.props" />

<PropertyGroup>
<OpenApiGenerator_Namespace>OpenApiGenerator.IntegrationTests</OpenApiGenerator_Namespace>
<OpenApiGenerator_GenerateSdk>false</OpenApiGenerator_GenerateSdk>
<OpenApiGenerator_GenerateMethods>true</OpenApiGenerator_GenerateMethods>
<OpenApiGenerator_GenerateConstructors>true</OpenApiGenerator_GenerateConstructors>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<Import Project="../../libs/OpenApiGenerator/OpenApiGenerator.props" />

<PropertyGroup>
<OpenApiGenerator_Namespace>OpenApiGenerator.IntegrationTests</OpenApiGenerator_Namespace>
<OpenApiGenerator_GenerateSdk>false</OpenApiGenerator_GenerateSdk>
<OpenApiGenerator_GenerateMethods>true</OpenApiGenerator_GenerateMethods>
<OpenApiGenerator_GenerateConstructors>true</OpenApiGenerator_GenerateConstructors>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//HintName: G.Models.CreateModelStatus.g.cs
using System.Linq;
#pragma warning disable CS0618 // Type or member is obsolete

#nullable enable

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//HintName: G.Models.PullModelStatus.g.cs
using System.Linq;
#pragma warning disable CS0618 // Type or member is obsolete

#nullable enable

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//HintName: G.Models.PushModelStatus.g.cs
using System.Linq;
#pragma warning disable CS0618 // Type or member is obsolete

#nullable enable

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//HintName: G.Models.CreateModelStatus.g.cs
using System.Linq;
#pragma warning disable CS0618 // Type or member is obsolete

#nullable enable

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//HintName: G.Models.PullModelStatus.g.cs
using System.Linq;
#pragma warning disable CS0618 // Type or member is obsolete

#nullable enable

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//HintName: G.Models.PushModelStatus.g.cs
using System.Linq;
#pragma warning disable CS0618 // Type or member is obsolete

#nullable enable

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//HintName: JsonConverters.CreateModelStatus.g.cs
#nullable enable
#pragma warning disable CS0618 // Type or member is obsolete

namespace OpenApiGenerator.JsonConverters
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//HintName: JsonConverters.PullModelStatus.g.cs
#nullable enable
#pragma warning disable CS0618 // Type or member is obsolete

namespace OpenApiGenerator.JsonConverters
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//HintName: JsonConverters.PushModelStatus.g.cs
#nullable enable
#pragma warning disable CS0618 // Type or member is obsolete

namespace OpenApiGenerator.JsonConverters
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//HintName: G.Models.AssistantStreamEvent.g.cs
using System.Linq;
#pragma warning disable CS0618 // Type or member is obsolete

#nullable enable

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//HintName: G.Models.AssistantsApiResponseFormatOption.g.cs
using System.Linq;
#pragma warning disable CS0618 // Type or member is obsolete

#nullable enable

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//HintName: G.Models.AssistantsApiToolChoiceOption.g.cs
using System.Linq;
#pragma warning disable CS0618 // Type or member is obsolete

#nullable enable

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//HintName: G.Models.ChatCompletionRequestMessage.g.cs
using System.Linq;
#pragma warning disable CS0618 // Type or member is obsolete

#nullable enable

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//HintName: G.Models.ChatCompletionRequestMessageContentPart.g.cs
using System.Linq;
#pragma warning disable CS0618 // Type or member is obsolete

#nullable enable

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//HintName: G.Models.ChatCompletionToolChoiceOption.g.cs
using System.Linq;
#pragma warning disable CS0618 // Type or member is obsolete

#nullable enable

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//HintName: G.Models.MessageStreamEvent.g.cs
using System.Linq;
#pragma warning disable CS0618 // Type or member is obsolete

#nullable enable

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//HintName: G.Models.RunStepStreamEvent.g.cs
using System.Linq;
#pragma warning disable CS0618 // Type or member is obsolete

#nullable enable

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//HintName: G.Models.RunStreamEvent.g.cs
using System.Linq;
#pragma warning disable CS0618 // Type or member is obsolete

#nullable enable

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//HintName: G.Models.ThreadStreamEvent.g.cs
using System.Linq;
#pragma warning disable CS0618 // Type or member is obsolete

#nullable enable

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//HintName: G.Models.AssistantStreamEvent.g.cs
using System.Linq;
#pragma warning disable CS0618 // Type or member is obsolete

#nullable enable

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//HintName: G.Models.AssistantsApiResponseFormatOption.g.cs
using System.Linq;
#pragma warning disable CS0618 // Type or member is obsolete

#nullable enable

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//HintName: G.Models.AssistantsApiToolChoiceOption.g.cs
using System.Linq;
#pragma warning disable CS0618 // Type or member is obsolete

#nullable enable

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//HintName: G.Models.ChatCompletionRequestMessage.g.cs
using System.Linq;
#pragma warning disable CS0618 // Type or member is obsolete

#nullable enable

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//HintName: G.Models.ChatCompletionRequestMessageContentPart.g.cs
using System.Linq;
#pragma warning disable CS0618 // Type or member is obsolete

#nullable enable

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//HintName: G.Models.ChatCompletionToolChoiceOption.g.cs
using System.Linq;
#pragma warning disable CS0618 // Type or member is obsolete

#nullable enable

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//HintName: G.Models.MessageStreamEvent.g.cs
using System.Linq;
#pragma warning disable CS0618 // Type or member is obsolete

#nullable enable

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//HintName: G.Models.RunStepStreamEvent.g.cs
using System.Linq;
#pragma warning disable CS0618 // Type or member is obsolete

#nullable enable

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//HintName: G.Models.RunStreamEvent.g.cs
using System.Linq;
#pragma warning disable CS0618 // Type or member is obsolete

#nullable enable

Expand Down
Loading

0 comments on commit 5284865

Please sign in to comment.