Skip to content

Commit d196e12

Browse files
authored
Merge pull request #1663 from json-api-dotnet/openapi-mark-experimental
Mark AddOpenApiForJsonApi with [Experimental]
2 parents d81abed + 55faea6 commit d196e12

22 files changed

+54
-41
lines changed

docs/usage/openapi.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ Exposing an [OpenAPI document](https://swagger.io/specification/) for your JSON:
77
The [JsonApiDotNetCore.OpenApi.Swashbuckle](https://github.com/json-api-dotnet/JsonApiDotNetCore/pkgs/nuget/JsonApiDotNetCore.OpenApi.Swashbuckle) NuGet package
88
provides OpenAPI support for JSON:API by integrating with [Swashbuckle](https://github.com/domaindrivendev/Swashbuckle.AspNetCore).
99

10+
> [!WARNING]
11+
> OpenAPI support for JSON:API is currently experimental. The API and the structure of the OpenAPI document may change in future versions.
12+
1013
## Getting started
1114

1215
1. Install the `JsonApiDotNetCore.OpenApi.Swashbuckle` NuGet package:

src/Examples/JsonApiDotNetCoreExample/Program.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,9 @@ static void ConfigureServices(WebApplicationBuilder builder)
7979

8080
using (CodeTimingSessionManager.Current.Measure("AddOpenApiForJsonApi()"))
8181
{
82+
#pragma warning disable JADNC_OA_001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
8283
builder.Services.AddOpenApiForJsonApi(options => options.DocumentFilter<SetOpenApiServerAtBuildTimeFilter>());
84+
#pragma warning restore JADNC_OA_001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
8385
}
8486
}
8587

src/JsonApiDotNetCore.OpenApi.Client.NSwag/JsonApiClient.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Diagnostics;
12
using System.Linq.Expressions;
23
using System.Reflection;
34
using Newtonsoft.Json;
@@ -175,7 +176,7 @@ public override bool CanConvert(Type objectType)
175176

176177
public override object ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer)
177178
{
178-
throw new UnreachableCodeException();
179+
throw new UnreachableException();
179180
}
180181

181182
public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer)
@@ -236,7 +237,7 @@ public override bool CanConvert(Type objectType)
236237

237238
public override object ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer)
238239
{
239-
throw new UnreachableCodeException();
240+
throw new UnreachableException();
240241
}
241242

242243
public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer)

src/JsonApiDotNetCore.OpenApi.Client.NSwag/UnreachableCodeException.cs

Lines changed: 0 additions & 6 deletions
This file was deleted.

src/JsonApiDotNetCore.OpenApi.Swashbuckle/JsonApiActionDescriptorCollectionProvider.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Diagnostics;
12
using System.Reflection;
23
using JsonApiDotNetCore.Errors;
34
using JsonApiDotNetCore.Middleware;
@@ -125,7 +126,7 @@ private static void UpdateProducesResponseTypeAttribute(ActionDescriptor endpoin
125126
}
126127
}
127128

128-
throw new UnreachableCodeException();
129+
throw new UnreachableException();
129130
}
130131

131132
private static bool ProducesJsonApiResponseDocument(ActionDescriptor endpoint)

src/JsonApiDotNetCore.OpenApi.Swashbuckle/JsonApiMetadata/JsonApiEndpointMetadataProvider.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Diagnostics;
12
using System.Reflection;
23
using JsonApiDotNetCore.Configuration;
34
using JsonApiDotNetCore.Controllers;
@@ -45,7 +46,7 @@ public JsonApiEndpointMetadataContainer Get(MethodInfo controllerAction)
4546

4647
if (primaryResourceType == null)
4748
{
48-
throw new UnreachableCodeException();
49+
throw new UnreachableException();
4950
}
5051

5152
IJsonApiRequestMetadata? requestMetadata = GetRequestMetadata(endpoint, primaryResourceType);

src/JsonApiDotNetCore.OpenApi.Swashbuckle/JsonApiRequestFormatMetadataProvider.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Diagnostics;
12
using JsonApiDotNetCore.Middleware;
23
using Microsoft.AspNetCore.Mvc.ApiExplorer;
34
using Microsoft.AspNetCore.Mvc.Formatters;
@@ -18,7 +19,7 @@ public bool CanRead(InputFormatterContext context)
1819
/// <inheritdoc />
1920
public Task<InputFormatterResult> ReadAsync(InputFormatterContext context)
2021
{
21-
throw new UnreachableCodeException();
22+
throw new UnreachableException();
2223
}
2324

2425
/// <inheritdoc />

src/JsonApiDotNetCore.OpenApi.Swashbuckle/OpenApiEndpointConvention.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Diagnostics;
12
using System.Net;
23
using System.Reflection;
34
using JsonApiDotNetCore.Configuration;
@@ -108,7 +109,7 @@ private static bool IsEndpointAvailable(JsonApiEndpoints endpoint, ResourceType
108109
JsonApiEndpoints.PatchRelationship => availableEndpoints.HasFlag(JsonApiEndpoints.PatchRelationship),
109110
JsonApiEndpoints.Delete => availableEndpoints.HasFlag(JsonApiEndpoints.Delete),
110111
JsonApiEndpoints.DeleteRelationship => availableEndpoints.HasFlag(JsonApiEndpoints.DeleteRelationship),
111-
_ => throw new UnreachableCodeException()
112+
_ => throw new UnreachableException()
112113
};
113114
}
114115

@@ -178,7 +179,7 @@ private static HttpStatusCode[] GetSuccessStatusCodesForEndpoint(JsonApiEndpoint
178179
[
179180
HttpStatusCode.NoContent
180181
],
181-
_ => throw new UnreachableCodeException()
182+
_ => throw new UnreachableException()
182183
};
183184
}
184185

@@ -236,7 +237,7 @@ private HttpStatusCode[] GetErrorStatusCodesForEndpoint(JsonApiEndpointWrapper e
236237
HttpStatusCode.NotFound,
237238
HttpStatusCode.Conflict
238239
],
239-
_ => throw new UnreachableCodeException()
240+
_ => throw new UnreachableException()
240241
};
241242
}
242243

src/JsonApiDotNetCore.OpenApi.Swashbuckle/OpenApiOperationIdSelector.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Diagnostics;
12
using System.Reflection;
23
using System.Text.Json;
34
using Humanizer;
@@ -66,7 +67,7 @@ private static string GetTemplate(ApiDescription endpoint)
6667

6768
if (!SchemaOpenTypeToOpenApiOperationIdTemplateMap.TryGetValue(bodyType, out string? template))
6869
{
69-
throw new UnreachableCodeException();
70+
throw new UnreachableException();
7071
}
7172

7273
return template;
@@ -78,7 +79,7 @@ private static Type GetBodyType(ApiDescription endpoint)
7879

7980
if (producesResponseTypeAttribute == null)
8081
{
81-
throw new UnreachableCodeException();
82+
throw new UnreachableException();
8283
}
8384

8485
ControllerParameterDescriptor? requestBodyDescriptor = endpoint.ActionDescriptor.GetBodyParameterDescriptor();
@@ -96,7 +97,7 @@ private string ApplyTemplate(string openApiOperationIdTemplate, ResourceType? re
9697
{
9798
if (endpoint.RelativePath == null || endpoint.HttpMethod == null)
9899
{
99-
throw new UnreachableCodeException();
100+
throw new UnreachableException();
100101
}
101102

102103
string method = endpoint.HttpMethod.ToLowerInvariant();

src/JsonApiDotNetCore.OpenApi.Swashbuckle/OpenApiSchemaExtensions.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Diagnostics;
12
using Microsoft.OpenApi.Models;
23

34
namespace JsonApiDotNetCore.OpenApi.Swashbuckle;
@@ -21,7 +22,7 @@ public static void ReorderProperties(this OpenApiSchema fullSchema, IEnumerable<
2122

2223
if (fullSchema.Properties.Count != propertiesInOrder.Count)
2324
{
24-
throw new UnreachableCodeException();
25+
throw new UnreachableException();
2526
}
2627

2728
fullSchema.Properties = propertiesInOrder;

src/JsonApiDotNetCore.OpenApi.Swashbuckle/SchemaGenerators/Bodies/AtomicOperationsBodySchemaGenerator.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Diagnostics;
12
using JsonApiDotNetCore.AtomicOperations;
23
using JsonApiDotNetCore.Configuration;
34
using JsonApiDotNetCore.Middleware;
@@ -121,7 +122,7 @@ private void GenerateSchemaForResourceOperation(Type operationOpenType, Resource
121122
AtomicOperationCode.Add => WriteOperationKind.CreateResource,
122123
AtomicOperationCode.Update => WriteOperationKind.UpdateResource,
123124
AtomicOperationCode.Remove => WriteOperationKind.DeleteResource,
124-
_ => throw new UnreachableCodeException()
125+
_ => throw new UnreachableException()
125126
};
126127

127128
if (!_atomicOperationFilter.IsEnabled(resourceType, writeOperation))
@@ -158,7 +159,7 @@ private void GenerateSchemaForRelationshipOperation(Type operationOpenType, Rela
158159
AtomicOperationCode.Add => WriteOperationKind.AddToRelationship,
159160
AtomicOperationCode.Update => WriteOperationKind.SetRelationship,
160161
AtomicOperationCode.Remove => WriteOperationKind.RemoveFromRelationship,
161-
_ => throw new UnreachableCodeException()
162+
_ => throw new UnreachableException()
162163
};
163164

164165
if (!_atomicOperationFilter.IsEnabled(relationship.LeftType, writeOperation))
@@ -211,7 +212,7 @@ private static bool IsToOneRelationshipEnabled(HasOneAttribute relationship, Wri
211212
return writeOperation switch
212213
{
213214
WriteOperationKind.SetRelationship => relationship.Capabilities.HasFlag(HasOneCapabilities.AllowSet),
214-
_ => throw new UnreachableCodeException()
215+
_ => throw new UnreachableException()
215216
};
216217
}
217218

@@ -222,7 +223,7 @@ private static bool IsToManyRelationshipEnabled(HasManyAttribute relationship, W
222223
WriteOperationKind.SetRelationship => relationship.Capabilities.HasFlag(HasManyCapabilities.AllowSet),
223224
WriteOperationKind.AddToRelationship => relationship.Capabilities.HasFlag(HasManyCapabilities.AllowAdd),
224225
WriteOperationKind.RemoveFromRelationship => relationship.Capabilities.HasFlag(HasManyCapabilities.AllowRemove),
225-
_ => throw new UnreachableCodeException()
226+
_ => throw new UnreachableException()
226227
};
227228
}
228229

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Diagnostics;
12
using JsonApiDotNetCore.OpenApi.Swashbuckle.JsonApiObjects.AtomicOperations;
23
using Microsoft.OpenApi.Any;
34
using Microsoft.OpenApi.Models;
@@ -74,7 +75,7 @@ public void MapDiscriminator(OpenApiSchema referenceSchemaForOperation, string d
7475

7576
if (!schemaRepository.TryLookupByType(AtomicOperationAbstractType, out OpenApiSchema? referenceSchemaForAbstractOperation))
7677
{
77-
throw new UnreachableCodeException();
78+
throw new UnreachableException();
7879
}
7980

8081
OpenApiSchema fullSchemaForAbstractOperation = schemaRepository.Schemas[referenceSchemaForAbstractOperation.Reference.Id];

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Diagnostics;
12
using JsonApiDotNetCore.Configuration;
23
using JsonApiDotNetCore.OpenApi.Swashbuckle.JsonApiObjects.ResourceObjects;
34
using JsonApiDotNetCore.OpenApi.Swashbuckle.SwaggerComponents;
@@ -97,7 +98,7 @@ public void MapDiscriminator(Type resourceDataConstructedType, OpenApiSchema ref
9798
{
9899
if (!schemaRepository.TryLookupByType(ResourceDataAbstractType, out OpenApiSchema? referenceSchemaForAbstractResourceData))
99100
{
100-
throw new UnreachableCodeException();
101+
throw new UnreachableException();
101102
}
102103

103104
OpenApiSchema fullSchemaForAbstractResourceData = schemaRepository.Schemas[referenceSchemaForAbstractResourceData.Reference.Id];

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Diagnostics;
12
using System.Reflection;
23
using JsonApiDotNetCore.Configuration;
34
using JsonApiDotNetCore.OpenApi.Swashbuckle.JsonApiObjects.ResourceObjects;
@@ -75,7 +76,7 @@ private static Type GetElementTypeOfDataProperty(Type dataContainerConstructedTy
7576

7677
if (dataProperty == null)
7778
{
78-
throw new UnreachableCodeException();
79+
throw new UnreachableException();
7980
}
8081

8182
Type innerPropertyType = dataProperty.PropertyType.ConstructedToOpenType().IsAssignableTo(typeof(ICollection<>))
@@ -89,7 +90,7 @@ private static Type GetElementTypeOfDataProperty(Type dataContainerConstructedTy
8990

9091
if (!innerPropertyType.IsGenericType)
9192
{
92-
throw new UnreachableCodeException();
93+
throw new UnreachableException();
9394
}
9495

9596
return innerPropertyType;

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Diagnostics;
12
using JsonApiDotNetCore.Configuration;
23
using JsonApiDotNetCore.OpenApi.Swashbuckle.JsonApiObjects.ResourceObjects;
34
using JsonApiDotNetCore.Resources.Annotations;
@@ -54,7 +55,7 @@ public OpenApiSchema GenerateSchema(RelationshipAttribute relationship, SchemaRe
5455

5556
if (schemaRepository.TryLookupByType(relationshipIdentifierConstructedType, out _))
5657
{
57-
throw new UnreachableCodeException();
58+
throw new UnreachableException();
5859
}
5960

6061
OpenApiSchema referenceSchemaForIdentifier = _defaultSchemaGenerator.GenerateSchema(relationshipIdentifierConstructedType, schemaRepository);

src/JsonApiDotNetCore.OpenApi.Swashbuckle/SchemaGenerators/JsonApiSchemaGenerator.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Diagnostics;
12
using System.Reflection;
23
using JsonApiDotNetCore.Controllers;
34
using JsonApiDotNetCore.OpenApi.Swashbuckle.SchemaGenerators.Bodies;
@@ -61,6 +62,6 @@ private BodySchemaGenerator GetBodySchemaGenerator(Type modelType)
6162
}
6263
}
6364

64-
throw new UnreachableCodeException();
65+
throw new UnreachableException();
6566
}
6667
}

src/JsonApiDotNetCore.OpenApi.Swashbuckle/ServiceCollectionExtensions.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Diagnostics.CodeAnalysis;
12
using JsonApiDotNetCore.OpenApi.Swashbuckle.JsonApiMetadata;
23
using JsonApiDotNetCore.OpenApi.Swashbuckle.SchemaGenerators;
34
using JsonApiDotNetCore.OpenApi.Swashbuckle.SchemaGenerators.Bodies;
@@ -18,17 +19,18 @@ public static class ServiceCollectionExtensions
1819
/// <summary>
1920
/// Configures OpenAPI for JsonApiDotNetCore using Swashbuckle.
2021
/// </summary>
21-
public static void AddOpenApiForJsonApi(this IServiceCollection services, Action<SwaggerGenOptions>? setupSwaggerGenAction = null)
22+
[Experimental("JADNC_OA_001", UrlFormat = "https://github.com/json-api-dotnet/JsonApiDotNetCore/blob/openapi/docs/usage/openapi.md")]
23+
public static void AddOpenApiForJsonApi(this IServiceCollection services, Action<SwaggerGenOptions>? configureSwaggerGenOptions = null)
2224
{
2325
ArgumentNullException.ThrowIfNull(services);
2426

2527
AddCustomApiExplorer(services);
2628
AddCustomSwaggerComponents(services);
2729
AddSwaggerGenerator(services);
2830

29-
if (setupSwaggerGenAction != null)
31+
if (configureSwaggerGenOptions != null)
3032
{
31-
services.Configure(setupSwaggerGenAction);
33+
services.Configure(configureSwaggerGenOptions);
3234
}
3335
}
3436

src/JsonApiDotNetCore.OpenApi.Swashbuckle/SwaggerComponents/DocumentationOpenApiOperationFilter.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Diagnostics;
12
using System.Net;
23
using System.Reflection;
34
using Humanizer;
@@ -437,7 +438,7 @@ private static RelationshipAttribute GetRelationshipFromRoute(ApiDescription api
437438
{
438439
if (apiDescription.RelativePath == null)
439440
{
440-
throw new UnreachableCodeException();
441+
throw new UnreachableException();
441442
}
442443

443444
string relationshipName = apiDescription.RelativePath.Split('/').Last();

src/JsonApiDotNetCore.OpenApi.Swashbuckle/SwaggerComponents/ResourceFieldSchemaBuilder.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Diagnostics;
12
using System.Reflection;
23
using JsonApiDotNetCore.OpenApi.Swashbuckle.JsonApiMetadata;
34
using JsonApiDotNetCore.OpenApi.Swashbuckle.JsonApiObjects.ResourceObjects;
@@ -109,7 +110,7 @@ private static AttrCapabilities GetRequiredCapabilityForAttributes(Type resource
109110
{
110111
return resourceDataOpenType == typeof(ResourceDataInResponse<>) ? AttrCapabilities.AllowView :
111112
resourceDataOpenType == typeof(DataInCreateResourceRequest<>) ? AttrCapabilities.AllowCreate :
112-
resourceDataOpenType == typeof(DataInUpdateResourceRequest<>) ? AttrCapabilities.AllowChange : throw new UnreachableCodeException();
113+
resourceDataOpenType == typeof(DataInUpdateResourceRequest<>) ? AttrCapabilities.AllowChange : throw new UnreachableException();
113114
}
114115

115116
private void EnsureAttributeSchemaIsExposed(OpenApiSchema referenceSchemaForAttribute, AttrAttribute attribute, SchemaRepository schemaRepository)
@@ -219,7 +220,7 @@ private static void AssertHasNoProperties(OpenApiSchema fullSchema)
219220
{
220221
if (fullSchema.Properties.Count > 0)
221222
{
222-
throw new UnreachableCodeException();
223+
throw new UnreachableException();
223224
}
224225
}
225226
}

src/JsonApiDotNetCore.OpenApi.Swashbuckle/SwaggerComponents/StringEnumOrderingFilter.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Diagnostics;
12
using JetBrains.Annotations;
23
using Microsoft.OpenApi.Any;
34
using Microsoft.OpenApi.Interfaces;
@@ -52,7 +53,7 @@ private static void OrderEnumMembers(OpenApiSchema schema)
5253

5354
if (ordered.Count != schema.Enum.Count)
5455
{
55-
throw new UnreachableCodeException();
56+
throw new UnreachableException();
5657
}
5758

5859
schema.Enum = ordered;

src/JsonApiDotNetCore.OpenApi.Swashbuckle/UnreachableCodeException.cs

Lines changed: 0 additions & 6 deletions
This file was deleted.

test/OpenApiTests/OpenApiStartup.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ public override void ConfigureServices(IServiceCollection services)
1515
{
1616
base.ConfigureServices(services);
1717

18+
#pragma warning disable JADNC_OA_001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
1819
services.AddOpenApiForJsonApi(SetupSwaggerGenAction);
20+
#pragma warning restore JADNC_OA_001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
1921
}
2022

2123
protected override void SetJsonApiOptions(JsonApiOptions options)

0 commit comments

Comments
 (0)