Skip to content

Commit 07392fd

Browse files
committed
Update to latest OpenAPI
1 parent f7213f6 commit 07392fd

40 files changed

+537
-353
lines changed

package-versions.props

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,13 @@
3434
<PropertyGroup Condition="'$(TargetFramework)' == 'net10.0'">
3535
<!-- Published dependencies (only update on major version change) -->
3636
<EntityFrameworkCoreFrozenVersion>10.0.*-*</EntityFrameworkCoreFrozenVersion>
37+
<SwashbuckleFrozenVersion>9.0.0-pr.3283.*</SwashbuckleFrozenVersion>
3738

3839
<!-- Non-published dependencies (these are safe to update, won't cause a breaking change) -->
3940
<AspNetCoreVersion>10.0.*-*</AspNetCoreVersion>
41+
<SwashbuckleVersion>9.0.0-pr.3283.*</SwashbuckleVersion>
42+
<MicrosoftOpenApiVersion>2.0.0-preview.21</MicrosoftOpenApiVersion>
43+
<MicrosoftApiServerVersion>10.0.*-*</MicrosoftApiServerVersion>
4044
<EntityFrameworkCoreVersion>10.0.*-*</EntityFrameworkCoreVersion>
4145
<EntityFrameworkCorePomeloVersion>9.0.*-*</EntityFrameworkCorePomeloVersion>
4246
</PropertyGroup>

src/Examples/JsonApiDotNetCoreExample/JsonApiDotNetCoreExample.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<Project Sdk="Microsoft.NET.Sdk.Web">
22
<PropertyGroup>
3-
<TargetFrameworks>net10.0;net9.0;net8.0</TargetFrameworks>
3+
<TargetFrameworks>net10.0</TargetFrameworks>
44
<OpenApiGenerateDocumentsOnBuild>true</OpenApiGenerateDocumentsOnBuild>
55
<OpenApiDocumentsDirectory>GeneratedSwagger</OpenApiDocumentsDirectory>
66
</PropertyGroup>

src/Examples/JsonApiDotNetCoreExample/SetOpenApiServerAtBuildTimeFilter.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
1616
{
1717
if (_httpContextAccessor.HttpContext == null)
1818
{
19+
swaggerDoc.Servers ??= [];
20+
1921
swaggerDoc.Servers.Add(new OpenApiServer
2022
{
2123
Url = "https://localhost:44340"

src/JsonApiDotNetCore.OpenApi.Swashbuckle/ConfigureSwaggerGenOptions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ public void Configure(SwaggerGenOptions options)
7474
options.DocumentFilter<StringEnumOrderingFilter>();
7575
options.DocumentFilter<SetSchemaTypeToObjectDocumentFilter>();
7676
options.DocumentFilter<UnusedComponentSchemaCleaner>();
77+
options.DocumentFilter<SortSchemasFilter>();
78+
options.DocumentFilter<RemoveTagsFilter>();
7779
}
7880

7981
private List<Type> SelectDerivedTypes(Type baseType)

src/JsonApiDotNetCore.OpenApi.Swashbuckle/JsonApiDotNetCore.OpenApi.Swashbuckle.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
3-
<TargetFrameworks>net10.0;net8.0</TargetFrameworks>
3+
<TargetFrameworks>net10.0</TargetFrameworks>
44
<IsPackable>true</IsPackable>
55
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
66
<OpenApiGenerateDocuments>false</OpenApiGenerateDocuments>
@@ -33,6 +33,7 @@
3333
</ItemGroup>
3434

3535
<ItemGroup>
36+
<PackageReference Include="Microsoft.OpenApi" Version="$(MicrosoftOpenApiVersion)" />
3637
<PackageReference Include="SauceControl.InheritDoc" Version="$(InheritDocVersion)" PrivateAssets="All" />
3738
<PackageReference Include="Swashbuckle.AspNetCore" Version="$(SwashbuckleFrozenVersion)" />
3839
</ItemGroup>

src/JsonApiDotNetCore.OpenApi.Swashbuckle/JsonApiSchemaIdSelector.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public string GetSchemaId(Type type)
120120

121121
private string ApplySchemaTemplate(string schemaTemplate, ResourceType? resourceType, string? relationshipName, AtomicOperationCode? operationCode)
122122
{
123-
string schemaId = schemaTemplate;
123+
string? schemaId = schemaTemplate;
124124

125125
schemaId = resourceType != null
126126
? schemaId.Replace("[ResourceName]", resourceType.PublicName.Singularize()).Pascalize()
@@ -136,7 +136,7 @@ private string ApplySchemaTemplate(string schemaTemplate, ResourceType? resource
136136
schemaId = schemaId.Replace("[OperationCode]", operationCode.Value.ToString().Pascalize());
137137
}
138138

139-
string pascalCaseSchemaId = schemaId.Pascalize();
139+
string? pascalCaseSchemaId = schemaId.Pascalize();
140140

141141
JsonNamingPolicy? namingPolicy = _options.SerializerOptions.PropertyNamingPolicy;
142142
return namingPolicy != null ? namingPolicy.ConvertName(pascalCaseSchemaId) : pascalCaseSchemaId;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using Microsoft.OpenApi.Models;
2+
3+
namespace JsonApiDotNetCore.OpenApi.Swashbuckle;
4+
5+
internal static class MicrosoftOpenApiCompatibilityExtensions
6+
{
7+
public static void SetNullable(this OpenApiSchema schema, bool nullable)
8+
{
9+
ArgumentNullException.ThrowIfNull(schema);
10+
11+
if (nullable)
12+
{
13+
schema.Type ??= JsonSchemaType.Null;
14+
schema.Type |= JsonSchemaType.Null;
15+
}
16+
else
17+
{
18+
if (schema.Type != null)
19+
{
20+
schema.Type &= ~JsonSchemaType.Null;
21+
}
22+
}
23+
}
24+
}

src/JsonApiDotNetCore.OpenApi.Swashbuckle/OpenApiDescriptionLinkProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public OpenApiDescriptionLinkProvider(IOptionsMonitor<SwaggerGeneratorOptions> s
3030

3131
if (swaggerGeneratorOptions.SwaggerDocs.Count > 0)
3232
{
33-
string latestVersionDocumentName = swaggerGeneratorOptions.SwaggerDocs.Last().Key;
33+
string? latestVersionDocumentName = swaggerGeneratorOptions.SwaggerDocs.Last().Key;
3434

3535
SwaggerOptions swaggerOptions = _swaggerOptionsMonitor.CurrentValue;
3636
return swaggerOptions.RouteTemplate.Replace("{documentName}", latestVersionDocumentName).Replace("{extension:regex(^(json|ya?ml)$)}", "json");

src/JsonApiDotNetCore.OpenApi.Swashbuckle/OpenApiOperationIdSelector.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ private string ApplyTemplate(string openApiOperationIdTemplate, ResourceType? re
9494
// @formatter:wrap_chained_method_calls chop_always
9595
// @formatter:wrap_before_first_method_call true
9696

97-
string pascalCaseOpenApiOperationId = openApiOperationIdTemplate
97+
string? pascalCaseOpenApiOperationId = openApiOperationIdTemplate
9898
.Replace("[Method]", method)
9999
.Replace("[PrimaryResourceName]", resourceType?.PublicName.Singularize())
100100
.Replace("[RelationshipName]", relationshipName)

src/JsonApiDotNetCore.OpenApi.Swashbuckle/OpenApiSchemaExtensions.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Microsoft.OpenApi.Models;
2+
using Microsoft.OpenApi.Models.Interfaces;
23

34
namespace JsonApiDotNetCore.OpenApi.Swashbuckle;
45

@@ -9,22 +10,23 @@ public static void ReorderProperties(this OpenApiSchema fullSchema, IEnumerable<
910
ArgumentNullException.ThrowIfNull(fullSchema);
1011
ArgumentNullException.ThrowIfNull(propertyNamesInOrder);
1112

12-
var propertiesInOrder = new Dictionary<string, OpenApiSchema>();
13+
var propertiesInOrder = new Dictionary<string, IOpenApiSchema>();
1314

1415
foreach (string propertyName in propertyNamesInOrder)
1516
{
16-
if (fullSchema.Properties.TryGetValue(propertyName, out OpenApiSchema? schema))
17+
if (fullSchema.Properties != null && fullSchema.Properties.TryGetValue(propertyName, out IOpenApiSchema? schema))
1718
{
1819
propertiesInOrder.Add(propertyName, schema);
1920
}
2021
}
2122

23+
ConsistencyGuard.ThrowIf(fullSchema.Properties == null);
2224
ConsistencyGuard.ThrowIf(fullSchema.Properties.Count != propertiesInOrder.Count);
2325

2426
fullSchema.Properties = propertiesInOrder;
2527
}
2628

27-
public static OpenApiSchema WrapInExtendedSchema(this OpenApiSchema source)
29+
public static OpenApiSchema WrapInExtendedSchema(this IOpenApiSchema source)
2830
{
2931
ArgumentNullException.ThrowIfNull(source);
3032

@@ -34,11 +36,11 @@ public static OpenApiSchema WrapInExtendedSchema(this OpenApiSchema source)
3436
};
3537
}
3638

37-
public static OpenApiSchema UnwrapLastExtendedSchema(this OpenApiSchema source)
39+
public static IOpenApiSchema UnwrapLastExtendedSchema(this IOpenApiSchema source)
3840
{
3941
ArgumentNullException.ThrowIfNull(source);
4042

41-
if (source.AllOf is { Count: > 0 })
43+
if (source is OpenApiSchema && source.AllOf is { Count: > 0 })
4244
{
4345
return source.AllOf.Last();
4446
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using Microsoft.OpenApi.Models;
2+
using Swashbuckle.AspNetCore.SwaggerGen;
3+
4+
namespace JsonApiDotNetCore.OpenApi.Swashbuckle;
5+
6+
internal sealed class RemoveTagsFilter : IDocumentFilter
7+
{
8+
public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
9+
{
10+
swaggerDoc.Tags?.Clear();
11+
}
12+
}

src/JsonApiDotNetCore.OpenApi.Swashbuckle/SchemaGenerators/Components/AtomicOperationCodeSchemaGenerator.cs

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using JsonApiDotNetCore.Serialization.Objects;
2-
using Microsoft.OpenApi.Any;
32
using Microsoft.OpenApi.Models;
3+
using Microsoft.OpenApi.Models.Interfaces;
4+
using Microsoft.OpenApi.Models.References;
45
using Swashbuckle.AspNetCore.SwaggerGen;
56

67
namespace JsonApiDotNetCore.OpenApi.Swashbuckle.SchemaGenerators.Components;
@@ -19,22 +20,15 @@ public AtomicOperationCodeSchemaGenerator(SchemaGenerationTracer schemaGeneratio
1920
_schemaIdSelector = schemaIdSelector;
2021
}
2122

22-
public OpenApiSchema GenerateSchema(AtomicOperationCode operationCode, SchemaRepository schemaRepository)
23+
public IOpenApiSchema GenerateSchema(AtomicOperationCode operationCode, SchemaRepository schemaRepository)
2324
{
2425
ArgumentNullException.ThrowIfNull(schemaRepository);
2526

2627
string schemaId = _schemaIdSelector.GetAtomicOperationCodeSchemaId(operationCode);
2728

2829
if (schemaRepository.Schemas.ContainsKey(schemaId))
2930
{
30-
return new OpenApiSchema
31-
{
32-
Reference = new OpenApiReference
33-
{
34-
Id = schemaId,
35-
Type = ReferenceType.Schema
36-
}
37-
};
31+
return new OpenApiSchemaReference(schemaId);
3832
}
3933

4034
using ISchemaGenerationTraceScope traceScope = _schemaGenerationTracer.TraceStart(this, operationCode);
@@ -43,11 +37,11 @@ public OpenApiSchema GenerateSchema(AtomicOperationCode operationCode, SchemaRep
4337

4438
var fullSchema = new OpenApiSchema
4539
{
46-
Type = "string",
47-
Enum = [new OpenApiString(enumValue)]
40+
Type = JsonSchemaType.String,
41+
Enum = [enumValue]
4842
};
4943

50-
OpenApiSchema referenceSchema = schemaRepository.AddDefinition(schemaId, fullSchema);
44+
OpenApiSchemaReference? referenceSchema = schemaRepository.AddDefinition(schemaId, fullSchema);
5145

5246
traceScope.TraceSucceeded(schemaId);
5347
return referenceSchema;

src/JsonApiDotNetCore.OpenApi.Swashbuckle/SchemaGenerators/Components/DataContainerSchemaGenerator.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
using JsonApiDotNetCore.Configuration;
33
using JsonApiDotNetCore.OpenApi.Swashbuckle.JsonApiObjects.ResourceObjects;
44
using JsonApiDotNetCore.OpenApi.Swashbuckle.SwaggerComponents;
5-
using Microsoft.OpenApi.Models;
5+
using Microsoft.OpenApi.Models.Interfaces;
6+
using Microsoft.OpenApi.Models.References;
67
using Swashbuckle.AspNetCore.SwaggerGen;
78

89
namespace JsonApiDotNetCore.OpenApi.Swashbuckle.SchemaGenerators.Components;
@@ -27,21 +28,21 @@ public DataContainerSchemaGenerator(SchemaGenerationTracer schemaGenerationTrace
2728
_resourceGraph = resourceGraph;
2829
}
2930

30-
public OpenApiSchema GenerateSchemaForCommonResourceDataInResponse(SchemaRepository schemaRepository)
31+
public OpenApiSchemaReference GenerateSchemaForCommonResourceDataInResponse(SchemaRepository schemaRepository)
3132
{
3233
ArgumentNullException.ThrowIfNull(schemaRepository);
3334

3435
return _dataSchemaGenerator.GenerateSchemaForCommonData(typeof(ResourceInResponse), schemaRepository);
3536
}
3637

37-
public OpenApiSchema GenerateSchema(Type dataContainerSchemaType, ResourceType resourceType, bool forRequestSchema, bool canIncludeRelated,
38+
public IOpenApiSchema GenerateSchema(Type dataContainerSchemaType, ResourceType resourceType, bool forRequestSchema, bool canIncludeRelated,
3839
SchemaRepository schemaRepository)
3940
{
4041
ArgumentNullException.ThrowIfNull(dataContainerSchemaType);
4142
ArgumentNullException.ThrowIfNull(resourceType);
4243
ArgumentNullException.ThrowIfNull(schemaRepository);
4344

44-
if (schemaRepository.TryLookupByType(dataContainerSchemaType, out OpenApiSchema referenceSchemaForData))
45+
if (schemaRepository.TryLookupByType(dataContainerSchemaType, out OpenApiSchemaReference? referenceSchemaForData))
4546
{
4647
return referenceSchemaForData;
4748
}
@@ -68,7 +69,7 @@ public OpenApiSchema GenerateSchema(Type dataContainerSchemaType, ResourceType r
6869
}
6970

7071
referenceSchemaForData = _dataSchemaGenerator.GenerateSchema(dataConstructedType, forRequestSchema, schemaRepository);
71-
traceScope.TraceSucceeded(referenceSchemaForData.Reference.Id);
72+
traceScope.TraceSucceeded(referenceSchemaForData.Reference.Id!);
7273
return referenceSchemaForData;
7374
}
7475

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy