Skip to content

Commit d8c5ac9

Browse files
Upgrade to Microsoft.OpenApi v2
Upgrade to preview 5 of Microsoft.OpenApi v2. Lots of tests are broken. Also applies some refactorings in files that needed to be touched, but those will be extracted out and merged ahead of time where possible in a future PR.
1 parent bafd6d6 commit d8c5ac9

File tree

77 files changed

+1406
-1292
lines changed

Some content is hidden

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

77 files changed

+1406
-1292
lines changed

Directory.Packages.props

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@
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="2.0.0-preview5" />
25+
<PackageVersion Include="Microsoft.OpenApi.Readers" Version="2.0.0-preview5" />
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" />
2929
<PackageVersion Include="NSwag.MSBuild" Version="14.2.0" />
3030
<PackageVersion Include="ReportGenerator" Version="5.4.1" />
31-
<PackageVersion Include="System.Text.Json" Version="4.6.0" />
31+
<PackageVersion Include="System.Text.Json" Version="8.0.5" />
3232
<PackageVersion Include="Verify.Xunit" Version="28.3.2" />
3333
<PackageVersion Include="xunit" Version="2.9.2" />
3434
<PackageVersion Include="xunit.core" Version="2.9.2" />

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 = JsonSchemaType.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 = JsonSchemaType.String,
124124
},
125125
},
126126
},

src/Swashbuckle.AspNetCore.Annotations/AnnotationsOperationFilter.cs

Lines changed: 12 additions & 14 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-
if (swaggerOperationAttribute.Tags != null)
67-
{
68-
operation.Tags = swaggerOperationAttribute.Tags
69-
.Select(tagName => new OpenApiTag { Name = tagName })
70-
.ToList();
71-
}
66+
// TODO Fix this
67+
////if (swaggerOperationAttribute.Tags != null)
68+
////{
69+
//// operation.Tags = swaggerOperationAttribute.Tags
70+
//// .Select(tagName => new OpenApiTag { Name = tagName })
71+
//// .ToList();
72+
////}
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
{

src/Swashbuckle.AspNetCore.ApiTesting/ApiTestRunnerBase.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,20 @@
33
using System.Net.Http;
44
using System.Threading.Tasks;
55
using Microsoft.OpenApi.Models;
6+
using Microsoft.OpenApi.Reader;
7+
using Microsoft.OpenApi.Readers;
68
using Microsoft.OpenApi.Writers;
79

810
namespace Swashbuckle.AspNetCore.ApiTesting
911
{
1012
public abstract class ApiTestRunnerBase : IDisposable
1113
{
14+
static ApiTestRunnerBase()
15+
{
16+
// TODO Make an assembly fixture
17+
OpenApiReaderRegistry.RegisterReader(OpenApiConstants.Yaml, new OpenApiYamlReader());
18+
}
19+
1220
private readonly ApiTestRunnerOptions _options;
1321
private readonly RequestValidator _requestValidator;
1422
private readonly ResponseValidator _responseValidator;
@@ -85,4 +93,4 @@ public void Dispose()
8593
}
8694
}
8795
}
88-
}
96+
}

src/Swashbuckle.AspNetCore.ApiTesting/ApiTestRunnerOptionsExtensions.cs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,28 @@
1-
using Microsoft.OpenApi.Models;
2-
using Microsoft.OpenApi.Readers;
3-
using System;
1+
using System;
42
using System.IO;
3+
using Microsoft.OpenApi.Models;
54

65
namespace Swashbuckle.AspNetCore.ApiTesting
76
{
87
public static class ApiTestRunnerOptionsExtensions
98
{
109
public static void AddOpenApiFile(this ApiTestRunnerOptions options, string documentName, string filePath)
1110
{
12-
using (var fileStream = File.OpenRead(filePath))
13-
{
14-
var openApiDocument = new OpenApiStreamReader().Read(fileStream, out OpenApiDiagnostic diagnostic);
15-
options.OpenApiDocs.Add(documentName, openApiDocument);
16-
}
11+
using var fileStream = File.OpenRead(filePath);
12+
using var memoryStream = new MemoryStream();
13+
14+
fileStream.CopyTo(memoryStream);
15+
16+
var result = OpenApiDocument.Load(memoryStream);
17+
options.OpenApiDocs.Add(documentName, result.Document);
1718
}
1819

1920
public static OpenApiDocument GetOpenApiDocument(this ApiTestRunnerOptions options, string documentName)
2021
{
2122
if (!options.OpenApiDocs.TryGetValue(documentName, out OpenApiDocument document))
23+
{
2224
throw new InvalidOperationException($"Document with name '{documentName}' not found");
25+
}
2326

2427
return document;
2528
}

src/Swashbuckle.AspNetCore.ApiTesting/JsonContentValidator.cs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,24 @@
66

77
namespace Swashbuckle.AspNetCore.ApiTesting
88
{
9-
public class JsonContentValidator : IContentValidator
9+
public sealed class JsonContentValidator : IContentValidator
1010
{
11-
private readonly JsonValidator _jsonValidator;
11+
private readonly JsonValidator _jsonValidator = new();
1212

13-
public JsonContentValidator()
14-
{
15-
_jsonValidator = new JsonValidator();
16-
}
17-
18-
public bool CanValidate(string mediaType)
19-
{
20-
return mediaType.Contains("json");
21-
}
13+
public bool CanValidate(string mediaType) => mediaType.Contains("json");
2214

2315
public void Validate(OpenApiMediaType mediaTypeSpec, OpenApiDocument openApiDocument, HttpContent content)
2416
{
25-
if (mediaTypeSpec?.Schema == null) return;
17+
if (mediaTypeSpec?.Schema == null)
18+
{
19+
return;
20+
}
2621

2722
var instance = JToken.Parse(content.ReadAsStringAsync().Result);
2823
if (!_jsonValidator.Validate(mediaTypeSpec.Schema, openApiDocument, instance, out IEnumerable<string> errorMessages))
24+
{
2925
throw new ContentDoesNotMatchSpecException(string.Join(Environment.NewLine, errorMessages));
26+
}
3027
}
3128
}
3229
}

src/Swashbuckle.AspNetCore.ApiTesting/JsonValidation/JsonAllOfValidator.cs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,9 @@
55

66
namespace Swashbuckle.AspNetCore.ApiTesting
77
{
8-
public class JsonAllOfValidator : IJsonValidator
8+
public sealed class JsonAllOfValidator(JsonValidator jsonValidator) : IJsonValidator
99
{
10-
private JsonValidator _jsonValidator;
11-
12-
public JsonAllOfValidator(JsonValidator jsonValidator)
13-
{
14-
_jsonValidator = jsonValidator;
15-
}
10+
private JsonValidator _jsonValidator = jsonValidator;
1611

1712
public bool CanValidate(OpenApiSchema schema) => schema.AllOf != null && schema.AllOf.Any();
1813

@@ -26,14 +21,16 @@ public bool Validate(
2621

2722
var allOfArray = schema.AllOf.ToArray();
2823

29-
for (int i=0;i<allOfArray.Length;i++)
24+
for (int i = 0; i < allOfArray.Length; i++)
3025
{
3126
if (!_jsonValidator.Validate(allOfArray[i], openApiDocument, instance, out IEnumerable<string> subErrorMessages))
27+
{
3228
errorMessagesList.AddRange(subErrorMessages.Select(msg => $"{msg} (allOf[{i}])"));
29+
}
3330
}
3431

3532
errorMessages = errorMessagesList;
3633
return !errorMessages.Any();
3734
}
3835
}
39-
}
36+
}
Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
1-
using System.Linq;
21
using System.Collections.Generic;
2+
using System.Linq;
33
using Microsoft.OpenApi.Models;
44
using Newtonsoft.Json.Linq;
55

66
namespace Swashbuckle.AspNetCore.ApiTesting
77
{
8-
public class JsonAnyOfValidator : IJsonValidator
8+
public sealed class JsonAnyOfValidator(JsonValidator jsonValidator) : IJsonValidator
99
{
10-
private JsonValidator _jsonValidator;
11-
12-
public JsonAnyOfValidator(JsonValidator jsonValidator)
13-
{
14-
_jsonValidator = jsonValidator;
15-
}
10+
private JsonValidator _jsonValidator = jsonValidator;
1611

1712
public bool CanValidate(OpenApiSchema schema) => schema.AnyOf != null && schema.AnyOf.Any();
1813

@@ -26,11 +21,11 @@ public bool Validate(
2621

2722
var anyOfArray = schema.AnyOf.ToArray();
2823

29-
for (int i=0;i<anyOfArray.Length;i++)
24+
for (int i = 0; i < anyOfArray.Length; i++)
3025
{
3126
if (_jsonValidator.Validate(anyOfArray[i], openApiDocument, instance, out IEnumerable<string> subErrorMessages))
3227
{
33-
errorMessages = Enumerable.Empty<string>();
28+
errorMessages = [];
3429
return true;
3530
}
3631

@@ -41,4 +36,4 @@ public bool Validate(
4136
return !errorMessages.Any();
4237
}
4338
}
44-
}
39+
}
Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,15 @@
1-
using System;
2-
using System.Linq;
31
using System.Collections.Generic;
2+
using System.Linq;
43
using Microsoft.OpenApi.Models;
54
using Newtonsoft.Json.Linq;
65

76
namespace Swashbuckle.AspNetCore.ApiTesting
87
{
9-
public class JsonArrayValidator : IJsonValidator
8+
public class JsonArrayValidator(IJsonValidator jsonValidator) : IJsonValidator
109
{
11-
private readonly IJsonValidator _jsonValidator;
12-
13-
public JsonArrayValidator(IJsonValidator jsonValidator)
14-
{
15-
_jsonValidator = jsonValidator;
16-
}
10+
private readonly IJsonValidator _jsonValidator = jsonValidator;
1711

18-
public bool CanValidate(OpenApiSchema schema) => schema.Type == "array";
12+
public bool CanValidate(OpenApiSchema schema) => schema.Type is JsonSchemaType.Array;
1913

2014
public bool Validate(
2115
OpenApiSchema schema,
@@ -25,7 +19,7 @@ public bool Validate(
2519
{
2620
if (instance.Type != JTokenType.Array)
2721
{
28-
errorMessages = new[] { $"Path: {instance.Path}. Instance is not of type 'array'" };
22+
errorMessages = [$"Path: {instance.Path}. Instance is not of type 'array'"];
2923
return false;
3024
}
3125

@@ -38,24 +32,32 @@ public bool Validate(
3832
foreach (var itemInstance in arrayInstance)
3933
{
4034
if (!_jsonValidator.Validate(schema.Items, openApiDocument, itemInstance, out IEnumerable<string> itemErrorMessages))
35+
{
4136
errorMessagesList.AddRange(itemErrorMessages);
37+
}
4238
}
4339
}
4440

4541
// maxItems
46-
if (schema.MaxItems.HasValue && (arrayInstance.Count() > schema.MaxItems.Value))
42+
if (schema.MaxItems.HasValue && (arrayInstance.Count > schema.MaxItems.Value))
43+
{
4744
errorMessagesList.Add($"Path: {instance.Path}. Array size is greater than maxItems");
45+
}
4846

4947
// minItems
50-
if (schema.MinItems.HasValue && (arrayInstance.Count() < schema.MinItems.Value))
48+
if (schema.MinItems.HasValue && (arrayInstance.Count < schema.MinItems.Value))
49+
{
5150
errorMessagesList.Add($"Path: {instance.Path}. Array size is less than minItems");
51+
}
5252

5353
// uniqueItems
54-
if (schema.UniqueItems.HasValue && (arrayInstance.Count() != arrayInstance.Distinct().Count()))
54+
if (schema.UniqueItems.HasValue && (arrayInstance.Count != arrayInstance.Distinct().Count()))
55+
{
5556
errorMessagesList.Add($"Path: {instance.Path}. Array does not contain uniqueItems");
57+
}
5658

5759
errorMessages = errorMessagesList;
5860
return !errorMessages.Any();
5961
}
6062
}
61-
}
63+
}
Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
using System.Collections.Generic;
2-
using System.Linq;
32
using Microsoft.OpenApi.Models;
43
using Newtonsoft.Json.Linq;
54

65
namespace Swashbuckle.AspNetCore.ApiTesting
76
{
8-
public class JsonBooleanValidator : IJsonValidator
7+
public sealed class JsonBooleanValidator : IJsonValidator
98
{
10-
public bool CanValidate(OpenApiSchema schema) => schema.Type == "boolean";
9+
public bool CanValidate(OpenApiSchema schema) => schema.Type is JsonSchemaType.Boolean;
1110

1211
public bool Validate(
1312
OpenApiSchema schema,
@@ -17,12 +16,12 @@ public bool Validate(
1716
{
1817
if (instance.Type != JTokenType.Boolean)
1918
{
20-
errorMessages = new[] { $"Path: {instance.Path}. Instance is not of type 'boolean'" };
19+
errorMessages = [$"Path: {instance.Path}. Instance is not of type 'boolean'"];
2120
return false;
2221
}
2322

24-
errorMessages = Enumerable.Empty<string>();
23+
errorMessages = [];
2524
return true;
2625
}
2726
}
28-
}
27+
}

0 commit comments

Comments
 (0)