Skip to content

Commit

Permalink
refactor: Added Settings to some Data structs.
Browse files Browse the repository at this point in the history
  • Loading branch information
HavenDV committed Jun 16, 2024
1 parent 3af0761 commit fa434b5
Show file tree
Hide file tree
Showing 21 changed files with 12,558 additions and 1,026 deletions.
14 changes: 1 addition & 13 deletions src/libs/OpenApiGenerator.Core/Generation/Data.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,6 @@ .. includedTags.Select(x => PropertyData.Default with
})
]
: [],
TargetFramework: settings.TargetFramework,
JsonSerializerType: settings.JsonSerializerType,
JsonSerializerContext: settings.JsonSerializerContext,
GenerateJsonSerializerContextTypes: settings.GenerateJsonSerializerContextTypes,
HttpMethod: OperationType.Get,
Summary: openApiDocument.Info?.Description?.ClearForXml() ?? string.Empty,
BaseUrlSummary: openApiDocument.Servers.FirstOrDefault()?.Description?.ClearForXml() ?? string.Empty,
Expand All @@ -168,10 +164,6 @@ .. includedTags.Select(x => PropertyData.Default with
Path: string.Empty,
AuthorizationScheme: string.Empty,
Properties: ImmutableArray<PropertyData>.Empty,
TargetFramework: settings.TargetFramework,
JsonSerializerType: settings.JsonSerializerType,
JsonSerializerContext: settings.JsonSerializerContext,
GenerateJsonSerializerContextTypes: settings.GenerateJsonSerializerContextTypes,
HttpMethod: OperationType.Get,
Summary: x.Description?.ClearForXml() ?? string.Empty,
BaseUrlSummary: openApiDocument.Servers.FirstOrDefault()?.Description?.ClearForXml() ?? string.Empty,
Expand Down Expand Up @@ -304,7 +296,7 @@ .. includedTags.Select(x => PropertyData.Default with
.ToImmutableArray() : [];

var converters = models
.Where(x => x.Style == ModelStyle.Enumeration && x.JsonSerializerType != JsonSerializerType.NewtonsoftJson)
.Where(x => x.Style == ModelStyle.Enumeration && x.Settings.JsonSerializerType != JsonSerializerType.NewtonsoftJson)
.SelectMany(x => new[]
{
$"global::OpenApiGenerator.JsonConverters.{x.ClassName}JsonConverter",
Expand Down Expand Up @@ -339,10 +331,6 @@ .. includedTags.Select(x => PropertyData.Default with
Path: string.Empty,
AuthorizationScheme: string.Empty,
Properties: [],
TargetFramework: settings.TargetFramework,
JsonSerializerType: settings.JsonSerializerType,
JsonSerializerContext: settings.JsonSerializerContext,
GenerateJsonSerializerContextTypes: settings.GenerateJsonSerializerContextTypes,
HttpMethod: OperationType.Get,
Summary: string.Empty,
BaseUrlSummary: string.Empty,
Expand Down
4 changes: 2 additions & 2 deletions src/libs/OpenApiGenerator.Core/Generation/Sources.Clients.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ public static partial class Sources
public static string GenerateConstructors(
EndPoint endPoint)
{
var serializer = endPoint.JsonSerializerType.GetSerializer();
var hasOptions = string.IsNullOrWhiteSpace(endPoint.JsonSerializerContext);
var serializer = endPoint.Settings.JsonSerializerType.GetSerializer();
var hasOptions = string.IsNullOrWhiteSpace(endPoint.Settings.JsonSerializerContext);

return $@"
#nullable enable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public static string GenerateEnumJsonConverter(
ModelData model,
CancellationToken cancellationToken = default)
{
if (model.JsonSerializerType == JsonSerializerType.NewtonsoftJson)
if (model.Settings.JsonSerializerType == JsonSerializerType.NewtonsoftJson)
{
return string.Empty;
}
Expand Down Expand Up @@ -70,7 +70,7 @@ public static string GenerateEnumNullableJsonConverter(
ModelData model,
CancellationToken cancellationToken = default)
{
if (model.JsonSerializerType == JsonSerializerType.NewtonsoftJson)
if (model.Settings.JsonSerializerType == JsonSerializerType.NewtonsoftJson)
{
return string.Empty;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ public static partial class Sources
public static string GenerateJsonSerializerContextConverters(
EndPoint endPoint)
{
if (!endPoint.GenerateJsonSerializerContextTypes ||
endPoint.JsonSerializerType != JsonSerializerType.SystemTextJson)
if (!endPoint.Settings.GenerateJsonSerializerContextTypes ||
endPoint.Settings.JsonSerializerType != JsonSerializerType.SystemTextJson)
{
return string.Empty;
}
Expand Down
12 changes: 6 additions & 6 deletions src/libs/OpenApiGenerator.Core/Generation/Sources.Methods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public static string GetHttpMethod(string targetFramework, OperationType operati
public static string GenerateMethod(
EndPoint endPoint)
{
var jsonSerializer = endPoint.JsonSerializerType.GetSerializer();
var jsonSerializer = endPoint.Settings.JsonSerializerType.GetSerializer();
var taskType = endPoint.Stream
? string.IsNullOrWhiteSpace(endPoint.ResponseType.CSharpType)
? throw new InvalidOperationException($"Streamed responses must have a response type. OperationId: {endPoint.Id}.")
Expand Down Expand Up @@ -88,7 +88,7 @@ public static string GenerateMethod(
{(string.IsNullOrWhiteSpace(endPoint.RequestType.CSharpType) ? " " : @"
request = request ?? throw new global::System.ArgumentNullException(nameof(request));
")}
{(endPoint.JsonSerializerType == JsonSerializerType.NewtonsoftJson ? endPoint.Properties
{(endPoint.Settings.JsonSerializerType == JsonSerializerType.NewtonsoftJson ? endPoint.Properties
.Where(x => x is { ParameterLocation: not null, Type.EnumValues.Length: > 0 })
.Select(x => $@"
var {x.ArgumentName} = {x.ParameterName} switch
Expand All @@ -99,11 +99,11 @@ public static string GenerateMethod(
_ => throw new global::System.NotImplementedException(""Enum value not implemented.""),
}};").Inject() : " ")}
using var httpRequest = new global::System.Net.Http.HttpRequestMessage(
method: {GetHttpMethod(endPoint.TargetFramework, endPoint.HttpMethod)},
method: {GetHttpMethod(endPoint.Settings.TargetFramework, endPoint.HttpMethod)},
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, endPoint.JsonSerializerContext)},
content: {jsonSerializer.GenerateSerializeCall(endPoint.RequestType, endPoint.Settings.JsonSerializerContext)},
encoding: global::System.Text.Encoding.UTF8,
mediaType: ""application/json"");")}
Expand All @@ -116,7 +116,7 @@ public static string GenerateMethod(
var __content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
return
{jsonSerializer.GenerateDeserializeCall(endPoint.ResponseType, endPoint.JsonSerializerContext)} ??
{jsonSerializer.GenerateDeserializeCall(endPoint.ResponseType, endPoint.Settings.JsonSerializerContext)} ??
throw new global::System.InvalidOperationException($""Response deserialization failed for \""{{__content}}\"" "");")}
{(endPoint.Stream ? $@"
using var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);
Expand All @@ -125,7 +125,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, endPoint.JsonSerializerContext)} ??
var streamedResponse = {jsonSerializer.GenerateDeserializeCall(endPoint.ResponseType, endPoint.Settings.JsonSerializerContext)} ??
throw new global::System.InvalidOperationException($""Response deserialization failed for \""{{__content}}\"" "");
yield return streamedResponse;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public static string GenerateEnumerationModel(
CancellationToken cancellationToken = default)
{
// Only Newtonsoft.Json supports EnumMemberAttribute
if (modelData.JsonSerializerType == JsonSerializerType.NewtonsoftJson)
if (modelData.Settings.JsonSerializerType == JsonSerializerType.NewtonsoftJson)
{
return $@"
{modelData.Summary.ToXmlDocumentationSummary(level: 4)}
Expand Down
6 changes: 3 additions & 3 deletions src/libs/OpenApiGenerator.Core/Generation/Sources.Models.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ private static string GenerateModel(
int level,
CancellationToken cancellationToken = default)
{
if (modelData.NamingConvention == NamingConvention.ConcatNames ||
if (modelData.Settings.NamingConvention == NamingConvention.ConcatNames ||
level == modelData.Parents.AsSpan().Length)
{
return modelData.Style switch
Expand Down Expand Up @@ -71,8 +71,8 @@ public static string GenerateClassModel(
ModelData modelData,
CancellationToken cancellationToken = default)
{
var jsonSerializer = modelData.JsonSerializerType.GetSerializer();
var isRequiredKeywordSupported = IsSupported(modelData.UseRequiredKeyword, modelData.TargetFramework);
var jsonSerializer = modelData.Settings.JsonSerializerType.GetSerializer();
var isRequiredKeywordSupported = IsSupported(modelData.Settings.UseRequiredKeyword, modelData.Settings.TargetFramework);
var requiredKeyword = isRequiredKeywordSupported
? " required"
: string.Empty;
Expand Down
8 changes: 4 additions & 4 deletions src/libs/OpenApiGenerator.Core/Generation/Sources.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public static FileWithName EnumJsonConverter(
CancellationToken cancellationToken = default)
{
if (data.Style != ModelStyle.Enumeration ||
data.JsonSerializerType == JsonSerializerType.NewtonsoftJson)
data.Settings.JsonSerializerType == JsonSerializerType.NewtonsoftJson)
{
return FileWithName.Empty;
}
Expand All @@ -65,7 +65,7 @@ public static FileWithName EnumNullableJsonConverter(
CancellationToken cancellationToken = default)
{
if (data.Style != ModelStyle.Enumeration ||
data.JsonSerializerType == JsonSerializerType.NewtonsoftJson)
data.Settings.JsonSerializerType == JsonSerializerType.NewtonsoftJson)
{
return FileWithName.Empty;
}
Expand Down Expand Up @@ -127,8 +127,8 @@ public static FileWithName JsonSerializerContextConverters(
EndPoint endPoint,
CancellationToken cancellationToken = default)
{
if (!endPoint.GenerateJsonSerializerContextTypes ||
endPoint.JsonSerializerType == JsonSerializerType.NewtonsoftJson)
if (!endPoint.Settings.GenerateJsonSerializerContextTypes ||
endPoint.Settings.JsonSerializerType == JsonSerializerType.NewtonsoftJson)
{
return FileWithName.Empty;
}
Expand Down
12 changes: 0 additions & 12 deletions src/libs/OpenApiGenerator.Core/Models/EndPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ public readonly record struct EndPoint(
string Path,
string AuthorizationScheme,
ImmutableArray<PropertyData> Properties,
string TargetFramework,
JsonSerializerType JsonSerializerType,
string JsonSerializerContext,
bool GenerateJsonSerializerContextTypes,
OperationType HttpMethod,
string Summary,
string BaseUrlSummary,
Expand Down Expand Up @@ -193,10 +189,6 @@ public static EndPoint FromSchema(
Path: preparedPath,
AuthorizationScheme: string.Empty,
Properties: properties.ToImmutableArray(),
TargetFramework: settings.TargetFramework,
JsonSerializerType: settings.JsonSerializerType,
JsonSerializerContext: settings.JsonSerializerContext,
GenerateJsonSerializerContextTypes: settings.GenerateJsonSerializerContextTypes,
HttpMethod: operation.Key,
Summary: operation.Value.GetXmlDocumentationSummary(),
BaseUrlSummary: string.Empty,
Expand Down Expand Up @@ -232,10 +224,6 @@ public static EndPoint FromAuthorization(
Path: string.Empty,
AuthorizationScheme: scheme,
Properties: [],
TargetFramework: settings.TargetFramework,
JsonSerializerType: settings.JsonSerializerType,
JsonSerializerContext: settings.JsonSerializerContext,
GenerateJsonSerializerContextTypes: settings.GenerateJsonSerializerContextTypes,
HttpMethod: default,
Summary: string.Empty,
BaseUrlSummary: string.Empty,
Expand Down
19 changes: 5 additions & 14 deletions src/libs/OpenApiGenerator.Core/Models/ModelData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,9 @@ public readonly record struct ModelData(
string Id,
bool AddTypeName,
ImmutableArray<ModelData> Parents,
string TargetFramework,
string Namespace,
NamingConvention NamingConvention,
Settings Settings,
ModelStyle Style,
JsonSerializerType JsonSerializerType,
SdkFeatureUsage UseRequiredKeyword,
ImmutableArray<PropertyData> Properties,
string Summary,
bool IsDeprecated,
Expand Down Expand Up @@ -48,12 +45,9 @@ public static ModelData FromKey(
Id: key,
AddTypeName: false,
Parents: parents.ToImmutableArray(),
TargetFramework: settings.TargetFramework,
Namespace: settings.Namespace,
NamingConvention: settings.NamingConvention,
JsonSerializerType: settings.JsonSerializerType,
UseRequiredKeyword: settings.UseRequiredKeyword,
Style: settings.ModelStyle,
Settings: settings,
Properties: ImmutableArray<PropertyData>.Empty,
Summary: string.Empty,
IsDeprecated: false,
Expand Down Expand Up @@ -120,12 +114,9 @@ public static ModelData FromSchema(
Id: schema.Key,
AddTypeName: false,
Parents: parents.ToImmutableArray(),
TargetFramework: settings.TargetFramework,
Namespace: settings.Namespace,
NamingConvention: settings.NamingConvention,
JsonSerializerType: settings.JsonSerializerType,
UseRequiredKeyword: settings.UseRequiredKeyword,
Style: settings.ModelStyle,
Settings: settings,
Properties: ImmutableArray<PropertyData>.Empty,
Summary: schema.Value.GetSummary(),
IsDeprecated: schema.Value.Deprecated,
Expand Down Expand Up @@ -206,7 +197,7 @@ public static ModelData FromSchema(
: Schema.Value.Type.ToPropertyName())
;

public string ClassName => NamingConvention switch
public string ClassName => Settings.NamingConvention switch
{
NamingConvention.InnerClasses => Parents.IsEmpty ? Name : $"_{Name}",
NamingConvention.ConcatNames => Parents.IsEmpty ? Name : $"{Parents.Last().ClassName}{Name}",
Expand All @@ -215,7 +206,7 @@ public static ModelData FromSchema(

public string GlobalClassName => $"global::{Namespace}.{ClassName}";

public string ExternalClassName => NamingConvention switch
public string ExternalClassName => Settings.NamingConvention switch
{
NamingConvention.InnerClasses => string.Join(".", Parents.Select(x => x.ClassName).Concat([ClassName])),
NamingConvention.ConcatNames => ClassName,
Expand Down
4 changes: 1 addition & 3 deletions src/tests/OpenApiGenerator.UnitTests/DataTests.Helpers.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Collections.Immutable;
using System.Runtime.CompilerServices;
using OpenApiGenerator.Core.Generation;
using System.Runtime.CompilerServices;
using OpenApiGenerator.Core.Models;
using OpenApiGenerator.Core.Naming.Methods;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,6 @@
ParameterDefaultValue: default
}
],
TargetFramework: netstandard2.0,
JsonSerializerContext: ,
GenerateJsonSerializerContextTypes: false,
HttpMethod: Patch,
Summary:
Update Run
Expand Down Expand Up @@ -853,9 +850,6 @@ Update a run.,
ParameterDefaultValue: default
}
],
TargetFramework: netstandard2.0,
JsonSerializerContext: ,
GenerateJsonSerializerContextTypes: false,
HttpMethod: Post,
Summary:
Create Run
Expand Down Expand Up @@ -1023,9 +1017,6 @@ Create a new run.,
Path: ,
AuthorizationScheme: ,
Properties: null,
TargetFramework: netstandard2.0,
JsonSerializerContext: ,
GenerateJsonSerializerContextTypes: false,
Summary: From https://github.com/langchain-ai/langsmith-sdk/blob/main/openapi/openapi.yaml,
BaseUrlSummary: ,
Settings: {
Expand Down
Loading

0 comments on commit fa434b5

Please sign in to comment.