Skip to content

Commit 446c522

Browse files
[WIP] Support .NET 10
Add support for .NET 10 by conditionally compiling various code to cater for differences between Microsoft.OpenApi v1 and v2.
1 parent 48c2982 commit 446c522

153 files changed

Lines changed: 2346 additions & 1339 deletions

File tree

Some content is hidden

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

Directory.Build.props

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
TODO Fix warning from Swashbuckle.AspNetCore with dotnet pack
2525
-->
2626
<NoWarn>$(NoWarn);NU5128</NoWarn>
27+
<!-- TODO Remove once .NET 10 is stable -->
28+
<NoWarn>$(NoWarn);NU5104</NoWarn>
2729
<NuGetAuditMode>direct</NuGetAuditMode>
2830
<!--
2931
TODO Go through the code and add nullable annotations
@@ -58,7 +60,7 @@
5860
<ItemGroup Condition=" '$(PackageReadmeFile)' != '' ">
5961
<None Include="$(MSBuildThisFileDirectory)$(PackageReadmeFile)" Pack="True" PackagePath="" />
6062
</ItemGroup>
61-
<PropertyGroup Condition=" '$(SignAssembly)' == 'true' AND '$(OS)' != 'Windows_NT' ">
63+
<PropertyGroup Condition=" '$(SignAssembly)' == 'true' AND '$(OS)' != !$([MSBuild]::IsOSPlatform('Windows')) ">
6264
<PublicSign>true</PublicSign>
6365
</PropertyGroup>
6466
<PropertyGroup>

Directory.Packages.props

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
<PackageVersion Include="Microsoft.AspNetCore.TestHost" Version="6.0.32" />
2222
<PackageVersion Include="Microsoft.CodeAnalysis.PublicApiAnalyzers" Version="3.3.4" />
2323
<PackageVersion Include="Microsoft.Extensions.FileProviders.Embedded" Version="2.1.0" />
24-
<PackageVersion Include="Microsoft.OpenApi" Version="1.6.22" />
25-
<PackageVersion Include="Microsoft.OpenApi.Readers" Version="1.6.22" />
24+
<PackageVersion Include="Microsoft.OpenApi" Version="1.6.23" />
25+
<PackageVersion Include="Microsoft.OpenApi.Readers" Version="1.6.23" />
2626
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
2727
<PackageVersion Include="Newtonsoft.Json" Version="13.0.3" />
2828
<PackageVersion Include="NSubstitute" Version="5.3.0" />
@@ -34,4 +34,9 @@
3434
<PackageVersion Include="xunit.core" Version="2.9.2" />
3535
<PackageVersion Include="xunit.runner.visualstudio" Version="2.8.2" />
3636
</ItemGroup>
37+
<ItemGroup Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net10.0'))">
38+
<PackageVersion Update="Microsoft.AspNetCore.OpenApi" Version="10.0.0-preview.1.25120.3" />
39+
<PackageVersion Update="Microsoft.OpenApi" Version="2.0.0-preview5" />
40+
<PackageVersion Update="Microsoft.OpenApi.Readers" Version="2.0.0-preview5" />
41+
</ItemGroup>
3742
</Project>

global.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"sdk": {
3-
"version": "9.0.200",
3+
"version": "10.0.100-preview.1.25120.13",
44
"allowPrerelease": false,
55
"rollForward": "latestMajor"
66
}

perf/Swashbuckle.AspNetCore.Benchmarks/Swashbuckle.AspNetCore.Benchmarks.csproj

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22
<PropertyGroup>
33
<IsPackable>false</IsPackable>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>net8.0</TargetFramework>
5+
<TargetFrameworks>net10.0;net8.0</TargetFrameworks>
66
</PropertyGroup>
7+
<ItemGroup>
8+
<Compile Include="..\..\src\Shared\JsonSchemaTypes.cs" Link="JsonSchemaTypes.cs" />
9+
</ItemGroup>
710
<ItemGroup>
811
<ProjectReference Include="..\..\src\Swashbuckle.AspNetCore.Annotations\Swashbuckle.AspNetCore.Annotations.csproj" />
912
<ProjectReference Include="..\..\src\Swashbuckle.AspNetCore.SwaggerGen\Swashbuckle.AspNetCore.SwaggerGen.csproj" />

perf/Swashbuckle.AspNetCore.Benchmarks/XmlCommentsBenchmark.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public void Setup()
101101
{
102102
Schema = new()
103103
{
104-
Type = "string",
104+
Type = JsonSchemaTypes.String,
105105
Description = "schema-level description",
106106
},
107107
};
@@ -120,7 +120,7 @@ public void Setup()
120120
{
121121
Schema = new()
122122
{
123-
Type = "string",
123+
Type = JsonSchemaTypes.String,
124124
},
125125
},
126126
},

src/Shared/JsonExtensions.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#if NET10_0_OR_GREATER
2+
using System.Text.Json;
3+
using System.Text.Json.Nodes;
4+
5+
namespace Swashbuckle.AspNetCore;
6+
7+
internal static class JsonExtensions
8+
{
9+
private static readonly JsonSerializerOptions Options = new()
10+
{
11+
NewLine = "\n",
12+
WriteIndented = true,
13+
};
14+
15+
public static string ToJson(this JsonNode value)
16+
=> value.ToJsonString(Options);
17+
}
18+
#endif

src/Shared/JsonModelFactory.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#if NET10_0_OR_GREATER
2+
using System.Text.Json.Nodes;
3+
#else
4+
using Microsoft.OpenApi.Any;
5+
using Swashbuckle.AspNetCore.SwaggerGen;
6+
#endif
7+
8+
namespace Swashbuckle.AspNetCore;
9+
10+
internal static class JsonModelFactory
11+
{
12+
#if NET10_0_OR_GREATER
13+
public static JsonNode CreateFromJson(string json)
14+
=> JsonNode.Parse(json);
15+
#else
16+
public static IOpenApiAny CreateFromJson(string json)
17+
=> OpenApiAnyFactory.CreateFromJson(json);
18+
#endif
19+
}

src/Shared/JsonSchemaTypes.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#if NET10_0_OR_GREATER
2+
using Microsoft.OpenApi.Models;
3+
#endif
4+
5+
namespace Swashbuckle.AspNetCore;
6+
7+
internal static class JsonSchemaTypes
8+
{
9+
#if NET10_0_OR_GREATER
10+
public static readonly JsonSchemaType Array = JsonSchemaType.Array;
11+
public static readonly JsonSchemaType Boolean = JsonSchemaType.Boolean;
12+
public static readonly JsonSchemaType Integer = JsonSchemaType.Integer;
13+
public static readonly JsonSchemaType Number = JsonSchemaType.Number;
14+
public static readonly JsonSchemaType Null = JsonSchemaType.Null;
15+
public static readonly JsonSchemaType Object = JsonSchemaType.Object;
16+
public static readonly JsonSchemaType String = JsonSchemaType.String;
17+
#else
18+
public const string Array = "array";
19+
public const string Boolean = "boolean";
20+
public const string Integer = "integer";
21+
public const string Number = "number";
22+
public const string Null = "null";
23+
public const string Object = "object";
24+
public const string String = "string";
25+
#endif
26+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System.IO;
2+
using System.Threading.Tasks;
3+
using Microsoft.OpenApi.Models;
4+
#if NET10_0_OR_GREATER
5+
using Microsoft.OpenApi.Reader;
6+
#else
7+
using Microsoft.OpenApi.Readers;
8+
#endif
9+
10+
namespace Swashbuckle.AspNetCore;
11+
12+
internal static class OpenApiDocumentLoader
13+
{
14+
public static async Task<OpenApiDocument> LoadAsync(Stream stream)
15+
{
16+
#if NET10_0_OR_GREATER
17+
var result = await OpenApiDocument.LoadAsync(stream);
18+
return result.Document;
19+
#else
20+
var reader = new OpenApiStreamReader();
21+
var document = reader.Read(stream, out OpenApiDiagnostic diagnostic);
22+
return await Task.FromResult(document);
23+
#endif
24+
}
25+
26+
public static async Task<(OpenApiDocument Document, OpenApiDiagnostic Diagnostic)> LoadWithDiagnosticsAsync(Stream stream)
27+
{
28+
#if NET10_0_OR_GREATER
29+
var result = await OpenApiDocument.LoadAsync(stream);
30+
return (result.Document, result.Diagnostic);
31+
#else
32+
var reader = new OpenApiStreamReader();
33+
var document = reader.Read(stream, out OpenApiDiagnostic diagnostic);
34+
return await Task.FromResult((document, diagnostic));
35+
#endif
36+
}
37+
}

src/Swashbuckle.AspNetCore.Annotations/AnnotationsOperationFilter.cs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ public class AnnotationsOperationFilter : IOperationFilter
1010
{
1111
public void Apply(OpenApiOperation operation, OperationFilterContext context)
1212
{
13-
IEnumerable<object> controllerAttributes = Array.Empty<object>();
14-
IEnumerable<object> actionAttributes = Array.Empty<object>();
15-
IEnumerable<object> metadataAttributes = Array.Empty<object>();
13+
IEnumerable<object> controllerAttributes = [];
14+
IEnumerable<object> actionAttributes = [];
15+
IEnumerable<object> metadataAttributes = [];
1616

1717
if (context.MethodInfo != null)
1818
{
@@ -63,12 +63,13 @@ private static void ApplySwaggerOperationAttribute(
6363
if (swaggerOperationAttribute.OperationId != null)
6464
operation.OperationId = swaggerOperationAttribute.OperationId;
6565

66+
// TODO Fix this
67+
#if !NET10_0_OR_GREATER
6668
if (swaggerOperationAttribute.Tags != null)
6769
{
68-
operation.Tags = swaggerOperationAttribute.Tags
69-
.Select(tagName => new OpenApiTag { Name = tagName })
70-
.ToList();
70+
operation.Tags = [.. swaggerOperationAttribute.Tags.Select(tagName => new OpenApiTag { Name = tagName })];
7171
}
72+
#endif
7273
}
7374

7475
public static void ApplySwaggerOperationFilterAttributes(
@@ -86,7 +87,7 @@ public static void ApplySwaggerOperationFilterAttributes(
8687
}
8788
}
8889

89-
private void ApplySwaggerResponseAttributes(
90+
private static void ApplySwaggerResponseAttributes(
9091
OpenApiOperation operation,
9192
OperationFilterContext context,
9293
IEnumerable<object> controllerAndActionAttributes)
@@ -97,10 +98,7 @@ private void ApplySwaggerResponseAttributes(
9798
{
9899
var statusCode = swaggerResponseAttribute.StatusCode.ToString();
99100

100-
if (operation.Responses == null)
101-
{
102-
operation.Responses = new OpenApiResponses();
103-
}
101+
operation.Responses ??= [];
104102

105103
if (!operation.Responses.TryGetValue(statusCode, out OpenApiResponse response))
106104
{

0 commit comments

Comments
 (0)