Skip to content

Mark AddOpenApiForJsonApi with [Experimental] #1663

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/usage/openapi.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ Exposing an [OpenAPI document](https://swagger.io/specification/) for your JSON:
The [JsonApiDotNetCore.OpenApi.Swashbuckle](https://github.com/json-api-dotnet/JsonApiDotNetCore/pkgs/nuget/JsonApiDotNetCore.OpenApi.Swashbuckle) NuGet package
provides OpenAPI support for JSON:API by integrating with [Swashbuckle](https://github.com/domaindrivendev/Swashbuckle.AspNetCore).

> [!WARNING]
> OpenAPI support for JSON:API is currently experimental. The API and the structure of the OpenAPI document may change in future versions.

## Getting started

1. Install the `JsonApiDotNetCore.OpenApi.Swashbuckle` NuGet package:
Expand Down
2 changes: 2 additions & 0 deletions src/Examples/JsonApiDotNetCoreExample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ static void ConfigureServices(WebApplicationBuilder builder)

using (CodeTimingSessionManager.Current.Measure("AddOpenApiForJsonApi()"))
{
#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.
builder.Services.AddOpenApiForJsonApi(options => options.DocumentFilter<SetOpenApiServerAtBuildTimeFilter>());
#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.
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/JsonApiDotNetCore.OpenApi.Client.NSwag/JsonApiClient.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Diagnostics;
using System.Linq.Expressions;
using System.Reflection;
using Newtonsoft.Json;
Expand Down Expand Up @@ -175,7 +176,7 @@ public override bool CanConvert(Type objectType)

public override object ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer)
{
throw new UnreachableCodeException();
throw new UnreachableException();
}

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

public override object ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer)
{
throw new UnreachableCodeException();
throw new UnreachableException();
}

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

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Diagnostics;
using System.Reflection;
using JsonApiDotNetCore.Errors;
using JsonApiDotNetCore.Middleware;
Expand Down Expand Up @@ -125,7 +126,7 @@ private static void UpdateProducesResponseTypeAttribute(ActionDescriptor endpoin
}
}

throw new UnreachableCodeException();
throw new UnreachableException();
}

private static bool ProducesJsonApiResponseDocument(ActionDescriptor endpoint)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Diagnostics;
using System.Reflection;
using JsonApiDotNetCore.Configuration;
using JsonApiDotNetCore.Controllers;
Expand Down Expand Up @@ -45,7 +46,7 @@ public JsonApiEndpointMetadataContainer Get(MethodInfo controllerAction)

if (primaryResourceType == null)
{
throw new UnreachableCodeException();
throw new UnreachableException();
}

IJsonApiRequestMetadata? requestMetadata = GetRequestMetadata(endpoint, primaryResourceType);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Diagnostics;
using JsonApiDotNetCore.Middleware;
using Microsoft.AspNetCore.Mvc.ApiExplorer;
using Microsoft.AspNetCore.Mvc.Formatters;
Expand All @@ -18,7 +19,7 @@ public bool CanRead(InputFormatterContext context)
/// <inheritdoc />
public Task<InputFormatterResult> ReadAsync(InputFormatterContext context)
{
throw new UnreachableCodeException();
throw new UnreachableException();
}

/// <inheritdoc />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Diagnostics;
using System.Net;
using System.Reflection;
using JsonApiDotNetCore.Configuration;
Expand Down Expand Up @@ -108,7 +109,7 @@ private static bool IsEndpointAvailable(JsonApiEndpoints endpoint, ResourceType
JsonApiEndpoints.PatchRelationship => availableEndpoints.HasFlag(JsonApiEndpoints.PatchRelationship),
JsonApiEndpoints.Delete => availableEndpoints.HasFlag(JsonApiEndpoints.Delete),
JsonApiEndpoints.DeleteRelationship => availableEndpoints.HasFlag(JsonApiEndpoints.DeleteRelationship),
_ => throw new UnreachableCodeException()
_ => throw new UnreachableException()
};
}

Expand Down Expand Up @@ -178,7 +179,7 @@ private static HttpStatusCode[] GetSuccessStatusCodesForEndpoint(JsonApiEndpoint
[
HttpStatusCode.NoContent
],
_ => throw new UnreachableCodeException()
_ => throw new UnreachableException()
};
}

Expand Down Expand Up @@ -236,7 +237,7 @@ private HttpStatusCode[] GetErrorStatusCodesForEndpoint(JsonApiEndpointWrapper e
HttpStatusCode.NotFound,
HttpStatusCode.Conflict
],
_ => throw new UnreachableCodeException()
_ => throw new UnreachableException()
};
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Diagnostics;
using System.Reflection;
using System.Text.Json;
using Humanizer;
Expand Down Expand Up @@ -66,7 +67,7 @@ private static string GetTemplate(ApiDescription endpoint)

if (!SchemaOpenTypeToOpenApiOperationIdTemplateMap.TryGetValue(bodyType, out string? template))
{
throw new UnreachableCodeException();
throw new UnreachableException();
}

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

if (producesResponseTypeAttribute == null)
{
throw new UnreachableCodeException();
throw new UnreachableException();
}

ControllerParameterDescriptor? requestBodyDescriptor = endpoint.ActionDescriptor.GetBodyParameterDescriptor();
Expand All @@ -96,7 +97,7 @@ private string ApplyTemplate(string openApiOperationIdTemplate, ResourceType? re
{
if (endpoint.RelativePath == null || endpoint.HttpMethod == null)
{
throw new UnreachableCodeException();
throw new UnreachableException();
}

string method = endpoint.HttpMethod.ToLowerInvariant();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Diagnostics;
using Microsoft.OpenApi.Models;

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

if (fullSchema.Properties.Count != propertiesInOrder.Count)
{
throw new UnreachableCodeException();
throw new UnreachableException();
}

fullSchema.Properties = propertiesInOrder;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Diagnostics;
using JsonApiDotNetCore.AtomicOperations;
using JsonApiDotNetCore.Configuration;
using JsonApiDotNetCore.Middleware;
Expand Down Expand Up @@ -121,7 +122,7 @@ private void GenerateSchemaForResourceOperation(Type operationOpenType, Resource
AtomicOperationCode.Add => WriteOperationKind.CreateResource,
AtomicOperationCode.Update => WriteOperationKind.UpdateResource,
AtomicOperationCode.Remove => WriteOperationKind.DeleteResource,
_ => throw new UnreachableCodeException()
_ => throw new UnreachableException()
};

if (!_atomicOperationFilter.IsEnabled(resourceType, writeOperation))
Expand Down Expand Up @@ -158,7 +159,7 @@ private void GenerateSchemaForRelationshipOperation(Type operationOpenType, Rela
AtomicOperationCode.Add => WriteOperationKind.AddToRelationship,
AtomicOperationCode.Update => WriteOperationKind.SetRelationship,
AtomicOperationCode.Remove => WriteOperationKind.RemoveFromRelationship,
_ => throw new UnreachableCodeException()
_ => throw new UnreachableException()
};

if (!_atomicOperationFilter.IsEnabled(relationship.LeftType, writeOperation))
Expand Down Expand Up @@ -211,7 +212,7 @@ private static bool IsToOneRelationshipEnabled(HasOneAttribute relationship, Wri
return writeOperation switch
{
WriteOperationKind.SetRelationship => relationship.Capabilities.HasFlag(HasOneCapabilities.AllowSet),
_ => throw new UnreachableCodeException()
_ => throw new UnreachableException()
};
}

Expand All @@ -222,7 +223,7 @@ private static bool IsToManyRelationshipEnabled(HasManyAttribute relationship, W
WriteOperationKind.SetRelationship => relationship.Capabilities.HasFlag(HasManyCapabilities.AllowSet),
WriteOperationKind.AddToRelationship => relationship.Capabilities.HasFlag(HasManyCapabilities.AllowAdd),
WriteOperationKind.RemoveFromRelationship => relationship.Capabilities.HasFlag(HasManyCapabilities.AllowRemove),
_ => throw new UnreachableCodeException()
_ => throw new UnreachableException()
};
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Diagnostics;
using JsonApiDotNetCore.OpenApi.Swashbuckle.JsonApiObjects.AtomicOperations;
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Models;
Expand Down Expand Up @@ -74,7 +75,7 @@ public void MapDiscriminator(OpenApiSchema referenceSchemaForOperation, string d

if (!schemaRepository.TryLookupByType(AtomicOperationAbstractType, out OpenApiSchema? referenceSchemaForAbstractOperation))
{
throw new UnreachableCodeException();
throw new UnreachableException();
}

OpenApiSchema fullSchemaForAbstractOperation = schemaRepository.Schemas[referenceSchemaForAbstractOperation.Reference.Id];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Diagnostics;
using JsonApiDotNetCore.Configuration;
using JsonApiDotNetCore.OpenApi.Swashbuckle.JsonApiObjects.ResourceObjects;
using JsonApiDotNetCore.OpenApi.Swashbuckle.SwaggerComponents;
Expand Down Expand Up @@ -97,7 +98,7 @@ public void MapDiscriminator(Type resourceDataConstructedType, OpenApiSchema ref
{
if (!schemaRepository.TryLookupByType(ResourceDataAbstractType, out OpenApiSchema? referenceSchemaForAbstractResourceData))
{
throw new UnreachableCodeException();
throw new UnreachableException();
}

OpenApiSchema fullSchemaForAbstractResourceData = schemaRepository.Schemas[referenceSchemaForAbstractResourceData.Reference.Id];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Diagnostics;
using System.Reflection;
using JsonApiDotNetCore.Configuration;
using JsonApiDotNetCore.OpenApi.Swashbuckle.JsonApiObjects.ResourceObjects;
Expand Down Expand Up @@ -75,7 +76,7 @@ private static Type GetElementTypeOfDataProperty(Type dataContainerConstructedTy

if (dataProperty == null)
{
throw new UnreachableCodeException();
throw new UnreachableException();
}

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

if (!innerPropertyType.IsGenericType)
{
throw new UnreachableCodeException();
throw new UnreachableException();
}

return innerPropertyType;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Diagnostics;
using JsonApiDotNetCore.Configuration;
using JsonApiDotNetCore.OpenApi.Swashbuckle.JsonApiObjects.ResourceObjects;
using JsonApiDotNetCore.Resources.Annotations;
Expand Down Expand Up @@ -54,7 +55,7 @@ public OpenApiSchema GenerateSchema(RelationshipAttribute relationship, SchemaRe

if (schemaRepository.TryLookupByType(relationshipIdentifierConstructedType, out _))
{
throw new UnreachableCodeException();
throw new UnreachableException();
}

OpenApiSchema referenceSchemaForIdentifier = _defaultSchemaGenerator.GenerateSchema(relationshipIdentifierConstructedType, schemaRepository);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Diagnostics;
using System.Reflection;
using JsonApiDotNetCore.Controllers;
using JsonApiDotNetCore.OpenApi.Swashbuckle.SchemaGenerators.Bodies;
Expand Down Expand Up @@ -61,6 +62,6 @@ private BodySchemaGenerator GetBodySchemaGenerator(Type modelType)
}
}

throw new UnreachableCodeException();
throw new UnreachableException();
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Diagnostics.CodeAnalysis;
using JsonApiDotNetCore.OpenApi.Swashbuckle.JsonApiMetadata;
using JsonApiDotNetCore.OpenApi.Swashbuckle.SchemaGenerators;
using JsonApiDotNetCore.OpenApi.Swashbuckle.SchemaGenerators.Bodies;
Expand All @@ -18,17 +19,18 @@ public static class ServiceCollectionExtensions
/// <summary>
/// Configures OpenAPI for JsonApiDotNetCore using Swashbuckle.
/// </summary>
public static void AddOpenApiForJsonApi(this IServiceCollection services, Action<SwaggerGenOptions>? setupSwaggerGenAction = null)
[Experimental("JADNC_OA_001", UrlFormat = "https://github.com/json-api-dotnet/JsonApiDotNetCore/blob/openapi/docs/usage/openapi.md")]
public static void AddOpenApiForJsonApi(this IServiceCollection services, Action<SwaggerGenOptions>? configureSwaggerGenOptions = null)
{
ArgumentNullException.ThrowIfNull(services);

AddCustomApiExplorer(services);
AddCustomSwaggerComponents(services);
AddSwaggerGenerator(services);

if (setupSwaggerGenAction != null)
if (configureSwaggerGenOptions != null)
{
services.Configure(setupSwaggerGenAction);
services.Configure(configureSwaggerGenOptions);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Diagnostics;
using System.Net;
using System.Reflection;
using Humanizer;
Expand Down Expand Up @@ -437,7 +438,7 @@ private static RelationshipAttribute GetRelationshipFromRoute(ApiDescription api
{
if (apiDescription.RelativePath == null)
{
throw new UnreachableCodeException();
throw new UnreachableException();
}

string relationshipName = apiDescription.RelativePath.Split('/').Last();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Diagnostics;
using System.Reflection;
using JsonApiDotNetCore.OpenApi.Swashbuckle.JsonApiMetadata;
using JsonApiDotNetCore.OpenApi.Swashbuckle.JsonApiObjects.ResourceObjects;
Expand Down Expand Up @@ -109,7 +110,7 @@ private static AttrCapabilities GetRequiredCapabilityForAttributes(Type resource
{
return resourceDataOpenType == typeof(ResourceDataInResponse<>) ? AttrCapabilities.AllowView :
resourceDataOpenType == typeof(DataInCreateResourceRequest<>) ? AttrCapabilities.AllowCreate :
resourceDataOpenType == typeof(DataInUpdateResourceRequest<>) ? AttrCapabilities.AllowChange : throw new UnreachableCodeException();
resourceDataOpenType == typeof(DataInUpdateResourceRequest<>) ? AttrCapabilities.AllowChange : throw new UnreachableException();
}

private void EnsureAttributeSchemaIsExposed(OpenApiSchema referenceSchemaForAttribute, AttrAttribute attribute, SchemaRepository schemaRepository)
Expand Down Expand Up @@ -219,7 +220,7 @@ private static void AssertHasNoProperties(OpenApiSchema fullSchema)
{
if (fullSchema.Properties.Count > 0)
{
throw new UnreachableCodeException();
throw new UnreachableException();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Diagnostics;
using JetBrains.Annotations;
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Interfaces;
Expand Down Expand Up @@ -52,7 +53,7 @@ private static void OrderEnumMembers(OpenApiSchema schema)

if (ordered.Count != schema.Enum.Count)
{
throw new UnreachableCodeException();
throw new UnreachableException();
}

schema.Enum = ordered;
Expand Down

This file was deleted.

2 changes: 2 additions & 0 deletions test/OpenApiTests/OpenApiStartup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ public override void ConfigureServices(IServiceCollection services)
{
base.ConfigureServices(services);

#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.
services.AddOpenApiForJsonApi(SetupSwaggerGenAction);
#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.
}

protected override void SetJsonApiOptions(JsonApiOptions options)
Expand Down
Loading