Skip to content

Commit

Permalink
feat: Added Trimming support for CLI generated code.
Browse files Browse the repository at this point in the history
  • Loading branch information
HavenDV committed Jun 16, 2024
1 parent e244e25 commit 3af0761
Show file tree
Hide file tree
Showing 11 changed files with 3,004 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/libs/OpenApiGenerator.Cli/Commands/GenerateCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ private static async Task HandleAsync(
ExcludeOperationIds: [],
IncludeTags: [],
ExcludeTags: [],
JsonSerializerContext: string.Empty,
GenerateJsonSerializerContextTypes: false,
JsonSerializerContext: $"{@namespace}.SourceGenerationContext",
GenerateJsonSerializerContextTypes: true,
GenerateModels: false,
ModelStyle: default,
IncludeModels: [],
Expand All @@ -100,8 +100,8 @@ private static async Task HandleAsync(
.Select(x => Sources.Method(x)))
.Concat(data.AnyOfs
.SelectMany(x => new [] { Sources.AnyOf(x), Sources.AnyOfJsonConverter(x), Sources.AnyOfJsonConverterFactory(x) }))
.Concat([Sources.JsonSerializerContext(data.Converters, data.Types)])
.Concat([Sources.JsonSerializerContextTypes(data.Types)])
.Concat([Sources.JsonSerializerContextConverters(data.Converters)])
.Where(x => !x.IsEmpty)
.ToArray();

Expand Down
3 changes: 3 additions & 0 deletions src/libs/OpenApiGenerator.Core/Generation/Data.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ .. includedTags.Select(x => PropertyData.Default with
HttpMethod: OperationType.Get,
Summary: openApiDocument.Info?.Description?.ClearForXml() ?? string.Empty,
BaseUrlSummary: openApiDocument.Servers.FirstOrDefault()?.Description?.ClearForXml() ?? string.Empty,
Settings: settings,
IsDeprecated: false,
RequestType: TypeData.Default,
ResponseType: TypeData.Default,
Expand All @@ -174,6 +175,7 @@ .. includedTags.Select(x => PropertyData.Default with
HttpMethod: OperationType.Get,
Summary: x.Description?.ClearForXml() ?? string.Empty,
BaseUrlSummary: openApiDocument.Servers.FirstOrDefault()?.Description?.ClearForXml() ?? string.Empty,
Settings: settings,
IsDeprecated: false,
RequestType: TypeData.Default,
ResponseType: TypeData.Default,
Expand Down Expand Up @@ -344,6 +346,7 @@ .. includedTags.Select(x => PropertyData.Default with
HttpMethod: OperationType.Get,
Summary: string.Empty,
BaseUrlSummary: string.Empty,
Settings: settings,
IsDeprecated: false,
RequestType: TypeData.Default,
ResponseType: TypeData.Default,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using OpenApiGenerator.Core.Extensions;
using OpenApiGenerator.Core.Json;
using OpenApiGenerator.Core.Models;

namespace OpenApiGenerator.Core.Generation;

public static partial class Sources
{
public static string GenerateJsonSerializerContext(
EndPoint endPoint,
EquatableArray<TypeData> types,
CancellationToken cancellationToken = default)
{
if (!endPoint.Settings.FromCli ||
!endPoint.Settings.GenerateJsonSerializerContextTypes ||
endPoint.Settings.JsonSerializerType != JsonSerializerType.SystemTextJson)
{
return string.Empty;
}

return $@"
#nullable enable
#pragma warning disable CS0618 // Type or member is obsolete
namespace {endPoint.Namespace}
{{
[global::System.Text.Json.Serialization.JsonSourceGenerationOptions(
DefaultIgnoreCondition = global::System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull,
Converters = new global::System.Type[]
{{
{endPoint.Converters.Select(x => $@"
typeof({x}),
").Inject()}
}})]
[global::System.Text.Json.Serialization.JsonSerializable(typeof(global::{endPoint.Namespace}.JsonSerializerContextTypes))]
internal sealed partial class SourceGenerationContext : global::System.Text.Json.Serialization.JsonSerializerContext
{{
}}
}}".RemoveBlankLinesWhereOnlyWhitespaces();
}
}
17 changes: 17 additions & 0 deletions src/libs/OpenApiGenerator.Core/Generation/Sources.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,23 @@ public static FileWithName JsonSerializerContextTypes(
Text: GenerateJsonSerializerContextTypes(types, cancellationToken: cancellationToken));
}

public static FileWithName JsonSerializerContext(
EndPoint endPoint,
EquatableArray<TypeData> types,
CancellationToken cancellationToken = default)
{
if (!endPoint.Settings.FromCli ||
!endPoint.Settings.GenerateJsonSerializerContextTypes ||
endPoint.Settings.JsonSerializerType == JsonSerializerType.NewtonsoftJson)
{
return FileWithName.Empty;
}

return new FileWithName(
Name: "JsonSerializerContext.g.cs",
Text: GenerateJsonSerializerContext(endPoint, types, cancellationToken: cancellationToken));
}

public static FileWithName JsonSerializerContextConverters(
EndPoint endPoint,
CancellationToken cancellationToken = default)
Expand Down
3 changes: 3 additions & 0 deletions src/libs/OpenApiGenerator.Core/Models/EndPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public readonly record struct EndPoint(
OperationType HttpMethod,
string Summary,
string BaseUrlSummary,
Settings Settings,
bool IsDeprecated,
TypeData RequestType,
TypeData ResponseType,
Expand Down Expand Up @@ -199,6 +200,7 @@ public static EndPoint FromSchema(
HttpMethod: operation.Key,
Summary: operation.Value.GetXmlDocumentationSummary(),
BaseUrlSummary: string.Empty,
Settings: settings,
IsDeprecated: operation.Value.Deprecated,
RequestType: requestType ?? TypeData.Default,
ResponseType: responseType ?? TypeData.Default,
Expand Down Expand Up @@ -237,6 +239,7 @@ public static EndPoint FromAuthorization(
HttpMethod: default,
Summary: string.Empty,
BaseUrlSummary: string.Empty,
Settings: settings,
IsDeprecated: false,
RequestType: TypeData.Default,
ResponseType: TypeData.Default,
Expand Down
3 changes: 3 additions & 0 deletions src/tests/OpenApiGenerator.IntegrationTests.Cli/CliTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ await File.WriteAllTextAsync(Path.Combine(tempDirectory, "Oag.csproj"), @"<Proje
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<LangVersion>preview</LangVersion>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<PropertyGroup Label=""Analyzers"">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,28 @@
Update Run
Update a run.,
BaseUrlSummary: ,
Settings: {
TargetFramework: netstandard2.0,
Namespace: G,
ClassName: Api,
GenerateConstructors: false,
GroupByTags: false,
GenerateMethods: false,
MethodNamingConventionFallback: MethodAndPath,
GenerateMethodsAsHttpClientExtensions: false,
GenerateMethodsUsingSystemNetHttpJson: false,
IncludeOperationIds: null,
ExcludeOperationIds: null,
IncludeTags: null,
ExcludeTags: null,
JsonSerializerContext: ,
GenerateJsonSerializerContextTypes: false,
GenerateModels: false,
IncludeModels: null,
ExcludeModels: null,
GenerateSdk: true,
FromCli: false
},
IsDeprecated: false,
RequestType: {
CSharpType: global::G.RunUpdateSchemaExtended,
Expand Down Expand Up @@ -839,6 +861,28 @@ Update a run.,
Create Run
Create a new run.,
BaseUrlSummary: ,
Settings: {
TargetFramework: netstandard2.0,
Namespace: G,
ClassName: Api,
GenerateConstructors: false,
GroupByTags: false,
GenerateMethods: false,
MethodNamingConventionFallback: MethodAndPath,
GenerateMethodsAsHttpClientExtensions: false,
GenerateMethodsUsingSystemNetHttpJson: false,
IncludeOperationIds: null,
ExcludeOperationIds: null,
IncludeTags: null,
ExcludeTags: null,
JsonSerializerContext: ,
GenerateJsonSerializerContextTypes: false,
GenerateModels: false,
IncludeModels: null,
ExcludeModels: null,
GenerateSdk: true,
FromCli: false
},
IsDeprecated: false,
RequestType: {
CSharpType: global::G.RunCreateSchemaExtended,
Expand Down Expand Up @@ -984,6 +1028,28 @@ Create a new run.,
GenerateJsonSerializerContextTypes: false,
Summary: From https://github.com/langchain-ai/langsmith-sdk/blob/main/openapi/openapi.yaml,
BaseUrlSummary: ,
Settings: {
TargetFramework: netstandard2.0,
Namespace: G,
ClassName: Api,
GenerateConstructors: false,
GroupByTags: false,
GenerateMethods: false,
MethodNamingConventionFallback: MethodAndPath,
GenerateMethodsAsHttpClientExtensions: false,
GenerateMethodsUsingSystemNetHttpJson: false,
IncludeOperationIds: null,
ExcludeOperationIds: null,
IncludeTags: null,
ExcludeTags: null,
JsonSerializerContext: ,
GenerateJsonSerializerContextTypes: false,
GenerateModels: false,
IncludeModels: null,
ExcludeModels: null,
GenerateSdk: true,
FromCli: false
},
IsDeprecated: false,
RequestType: {
CSharpType: ,
Expand Down
Loading

0 comments on commit 3af0761

Please sign in to comment.