Skip to content

Commit

Permalink
feat: Added trimming support.
Browse files Browse the repository at this point in the history
  • Loading branch information
HavenDV committed May 19, 2024
1 parent a3d4f12 commit 7274674
Show file tree
Hide file tree
Showing 23 changed files with 1,364 additions and 16 deletions.
7 changes: 7 additions & 0 deletions OpenApiGenerator.sln
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenApiGenerator.UnitTests"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenApiGenerator.Cli", "src\libs\OpenApiGenerator.Cli\OpenApiGenerator.Cli.csproj", "{CF8CA4E0-E90D-44FB-A9D5-6296A036BB24}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenApiGenerator.IntegrationTests.Data", "src\tests\OpenApiGenerator.IntegrationTests.Data\OpenApiGenerator.IntegrationTests.Data.csproj", "{522FA7E8-2FF4-4381-83E9-ADB1851AC26E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -68,6 +70,10 @@ Global
{CF8CA4E0-E90D-44FB-A9D5-6296A036BB24}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CF8CA4E0-E90D-44FB-A9D5-6296A036BB24}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CF8CA4E0-E90D-44FB-A9D5-6296A036BB24}.Release|Any CPU.Build.0 = Release|Any CPU
{522FA7E8-2FF4-4381-83E9-ADB1851AC26E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{522FA7E8-2FF4-4381-83E9-ADB1851AC26E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{522FA7E8-2FF4-4381-83E9-ADB1851AC26E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{522FA7E8-2FF4-4381-83E9-ADB1851AC26E}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -80,6 +86,7 @@ Global
{3A3F5EE3-0076-4822-B3BA-2955EC802E25} = {6E5BF389-3D3F-4D74-9DD0-3B199CB529C5}
{88BE4AC5-6E7B-46FA-B656-5CCE8BF9F18E} = {9CAA231D-7BE1-46C9-ACD6-EB2E569CEBEA}
{CF8CA4E0-E90D-44FB-A9D5-6296A036BB24} = {6E5BF389-3D3F-4D74-9DD0-3B199CB529C5}
{522FA7E8-2FF4-4381-83E9-ADB1851AC26E} = {9CAA231D-7BE1-46C9-ACD6-EB2E569CEBEA}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {1493AEE4-9211-46E9-BFE6-8F629EAC5693}
Expand Down
2 changes: 1 addition & 1 deletion src/libs/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
</ItemGroup>

<PropertyGroup Label="Versioning">
<Version>0.4.0</Version>
<Version>0.5.0</Version>
<MinVerMinimumMajorMinor>0.1</MinVerMinimumMajorMinor>
<MinVerTagPrefix>v</MinVerTagPrefix>
<MinVerDefaultPreReleaseIdentifiers>dev</MinVerDefaultPreReleaseIdentifiers>
Expand Down
1 change: 1 addition & 0 deletions src/libs/OpenApiGenerator.Cli/Commands/GenerateCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ private static async Task HandleAsync(
GenerateMethodsUsingSystemNetHttpJson: false,
IncludeOperationIds: [],
ExcludeOperationIds: [],
JsonSerializerContext: string.Empty,
GenerateModels: false,
ModelStyle: default,
IncludeModels: [],
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 @@ -116,7 +116,7 @@ public static string GenerateMethod(
requestUri: {endPoint.Path});
{(string.IsNullOrWhiteSpace(endPoint.RequestType) ? " " : $@"
httpRequest.Content = new global::System.Net.Http.StringContent(
content: {jsonSerializer.GenerateSerializeCall(endPoint.RequestType)}(request),
content: {jsonSerializer.GenerateSerializeCall(endPoint.RequestType, endPoint.JsonSerializerContext)},
encoding: global::System.Text.Encoding.UTF8,
mediaType: ""application/json"");")}
Expand All @@ -129,7 +129,7 @@ public static string GenerateMethod(
var content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
return
{jsonSerializer.GenerateDeserializeCall(endPoint.ResponseType)}(content) ??
{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 @@ -138,7 +138,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)}(content) ??
var streamedResponse = {jsonSerializer.GenerateDeserializeCall(endPoint.ResponseType, endPoint.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 @@ -40,6 +40,7 @@ public static ImmutableArray<EndPoint> PrepareData(
Path: string.Empty,
Properties: ImmutableArray<PropertyData>.Empty,
JsonSerializerType: settings.JsonSerializerType,
JsonSerializerContext: settings.JsonSerializerContext,
HttpMethod: OperationType.Get,
Summary: string.Empty,
RequestType: string.Empty,
Expand Down
4 changes: 2 additions & 2 deletions src/libs/OpenApiGenerator.Core/Json/IJsonSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ public interface IJsonSerializer
public string GeneratePropertyAttribute(string id, bool isRequired);
string GenerateExtensionDataAttribute();
string GenerateRequiredAttribute();
string GenerateDeserializeCall(string type);
string GenerateSerializeCall(string type);
string GenerateDeserializeCall(string type, string jsonSerializerContext);
string GenerateSerializeCall(string type, string jsonSerializerContext);
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ public string GenerateRequiredAttribute()
return string.Empty;
}

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

public string GenerateDeserializeCall(string type)
public string GenerateDeserializeCall(string type, string jsonSerializerContext)
{
return $"global::Newtonsoft.Json.JsonConvert.DeserializeObject<{type}>";
return $"global::Newtonsoft.Json.JsonConvert.DeserializeObject<{type}>(content)";
}
}
12 changes: 8 additions & 4 deletions src/libs/OpenApiGenerator.Core/Json/SystemTextJsonSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,17 @@ public string GenerateRequiredAttribute()
return "[global::System.Text.Json.Serialization.JsonRequired]";
}

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

public string GenerateDeserializeCall(string type)
public string GenerateDeserializeCall(string type, string jsonSerializerContext)
{
return $"global::System.Text.Json.JsonSerializer.Deserialize<{type}>";
return string.IsNullOrWhiteSpace(jsonSerializerContext)
? $"global::System.Text.Json.JsonSerializer.Deserialize<{type}>(content)"
: $"global::System.Text.Json.JsonSerializer.Deserialize(content, global::{jsonSerializerContext}.Default.{type})";
}
}
2 changes: 2 additions & 0 deletions src/libs/OpenApiGenerator.Core/Models/EndPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public readonly record struct EndPoint(
string Path,
ImmutableArray<PropertyData> Properties,
JsonSerializerType JsonSerializerType,
string JsonSerializerContext,
OperationType HttpMethod,
string Summary,
string RequestType,
Expand Down Expand Up @@ -62,6 +63,7 @@ public static EndPoint FromSchema(
Path: preparedPath,
Properties: [..parameters.Concat(requestModel.Properties)],
JsonSerializerType: settings.JsonSerializerType,
JsonSerializerContext: settings.JsonSerializerContext,
HttpMethod: operation.Key,
Summary: operation.Value.Summary?.Replace("\n", string.Empty) ?? string.Empty,
RequestType: requestSchema?.Reference?.Id ?? string.Empty,
Expand Down
1 change: 1 addition & 0 deletions src/libs/OpenApiGenerator.Core/Models/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public readonly record struct Settings(
bool GenerateMethodsUsingSystemNetHttpJson,
ImmutableArray<string> IncludeOperationIds,
ImmutableArray<string> ExcludeOperationIds,
string JsonSerializerContext,

bool GenerateModels,
ModelStyle ModelStyle,
Expand Down
2 changes: 2 additions & 0 deletions src/libs/OpenApiGenerator/OpenApiGenerator.props
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
<CompilerVisibleProperty Include="OpenApiGenerator_IncludeOperationIds"/>
<!-- List of ids separated by ;. Default: Empty -->
<CompilerVisibleProperty Include="OpenApiGenerator_ExcludeOperationIds"/>
<!-- Full name with namespace of JsonSerializerContext to enable trimming support for System.Text.Json. Default: Empty -->
<CompilerVisibleProperty Include="OpenApiGenerator_JsonSerializerContext"/>

<!-- Models generation -->
<!-- true/false. Default: false -->
Expand Down
1 change: 1 addition & 0 deletions src/libs/OpenApiGenerator/OptionsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public static Settings GetSettings(
[]).ToImmutableArray(),
ExcludeOperationIds: (options.GetGlobalOption(nameof(Settings.ExcludeOperationIds), prefix)?.Split(';') ??
[]).ToImmutableArray(),
JsonSerializerContext: options.GetGlobalOption(nameof(Settings.JsonSerializerContext), prefix) ?? string.Empty,

GenerateModels: options.GetBoolGlobalOption(nameof(Settings.GenerateModels), prefix),
ModelStyle: options.GetEnumGlobalOption<ModelStyle>(nameof(Settings.ModelStyle), prefix),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>

<Import Project="../../libs/OpenApiGenerator/OpenApiGenerator.props" />

<PropertyGroup>
<OpenApiGenerator_Namespace>OpenApiGenerator.IntegrationTests</OpenApiGenerator_Namespace>
<OpenApiGenerator_GenerateSdk>false</OpenApiGenerator_GenerateSdk>
<OpenApiGenerator_GenerateModels>true</OpenApiGenerator_GenerateModels>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.8.0" />
<PackageReference Include="Microsoft.OpenApi" Version="1.6.14" />
<PackageReference Include="Microsoft.OpenApi.Readers" Version="1.6.14" />
<PackageReference Include="SharpYaml" Version="2.1.1" />
<PackageReference Include="System.Collections.Immutable" Version="8.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\libs\OpenApiGenerator.Core\OpenApiGenerator.Core.csproj" OutputItemType="Analyzer" />
<ProjectReference Include="..\..\libs\OpenApiGenerator\OpenApiGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>

</Project>
Loading

0 comments on commit 7274674

Please sign in to comment.