GitXplorerGitXplorer
m

aspnet-transformer-gallery

public
3 stars
0 forks
0 issues

Commits

List of commits on branch main.
Unverified
fcba43e63bfc7e21a659223ba7e7515407075b70

Fix build warnings

mmikekistler committed 2 months ago
Unverified
504b89456b73f87a5db243cb31df6b78afbbe029

Add SecuritySchemeTransformer

mmikekistler committed 2 months ago
Unverified
77244493ba59f8f7f4577a25d3da7991b738be0b

Add TypeTransformer to the gallery

mmikekistler committed 2 months ago
Unverified
943bc1335041a7e7b9be747f54486ba9fc8f0b2b

Add InfoContactTransformer to the gallery

mmikekistler committed 2 months ago
Unverified
a8281f1b3742fd9f5e554309e51ce1079768d1ae

Add build-time OpenAPI doc generation

mmikekistler committed 2 months ago
Unverified
27f0948601eca22071e34ddf50757a5b4454c9b1

Add sln file

mmikekistler committed 2 months ago

README

The README file for this repository.

ASP.NET Transformer Gallery

A collection of OpenAPI transformers for ASP.NET applications.

CanonicalDocumentTransformer

Currently, the content of the OpenAPI document generated by ASP.NET does not have a canonical order. The content order is deterministic but it depends on obscure details of the application structure. So any change in the application could potentially change the order of the content in the OpenAPI document. This is problematic in situations where the OpenAPI document is generated at build time and committed to source control, because a small change in the app could generate a diff in the OpenAPI document that is simply a reordering of elements.

The CanonicalDocumentTransformer transformer sorts the content of the OpenAPI document into a canonical order.

Usage

Copy the CanonicalDocumentTransformer.cs file to your project and then configure the transformer in the configureOptions delegate of the AddOpenApi extension method as shown below:

builder.Services.AddOpenApi(options =>
{
    options.AddDocumentTransformer<CanonicalDocumentTransformer>();
});

InfoContactTransformer

Adds contact information to the OpenAPI document. In the example transformer, the contact information is hard-coded, but your implementation can retrieve the information from a database, a configuration file, or any other source.

Usage

Copy the InfoContactTransformer.cs file to your project and then configure the transformer in the configureOptions delegate of the AddOpenApi extension method as shown below:

builder.Services.AddOpenApi(options =>
{
    options.AddDocumentTransformer(new InfoContactTransformer());
});

NullableTransformer

ASP.NET may generate two different schemas for a class or struct that may be nullable. In many cases the "nullability" of a property is already expressed in the schema by the omission of the property from the required keyword. Properties that are not required can be omitted from the body entirely and the Json deserializer will produce an object with a null value for that property. In this case, dropping the nullable: true keyword from the schema allows the same schema to be used for both nullable and non-nullable cases. In addition, the null value is removed from the enum values if present so that nullable and non-nullable enums can share the same schema,

The NullableTransformer transformer removes nullable: true from any properties that are not listed in the required keyword of the schema.

Additional work must be done for nullable value types, because the default schema reference ID for nullable value types will have a prefix of "NullableOf", which will differ from the schema reference ID for the underlying value type, causing two schemas to be generated. To address this issue, a custom CreateSchemaReferenceId method is added to the OpenApiOptions to remove the prefix from the schema reference for nullable value types.

Usage

Copy the NullableTransformer.cs file to your project and then configure the transformer in the configureOptions delegate of the AddOpenApi extension method as shown below:

builder.Services.AddOpenApi(options =>
{
    options.AddNullableTransformer();
});

ProblemResponseTransformer

The TypedResults.Problem method in ASP.NET Core returns a problem details response, but this method does not implement IEndpointMetadataProvider, so the response is not documented in the OpenAPI document. This is because the status code is not known at compile time. But this situation can be described in OpenAPI 3.0.x using the 4XX response code. The ProblemResponseTransformer transformer adds a 4XX response for the problem details response to the OpenAPI document.

Usage

Copy the ProblemResponseTransformer.cs file to your project and then configure the transformer in the configureOptions delegate of the AddOpenApi extension method as shown below:

builder.Services.AddOpenApi(options =>
{
    options.AddProblemResponseTransformer();
});

SecuritySchemeTransformer

Currently ASP.NET does not collect information about the security schemes used in the application. The SecuritySchemeTransformer adds a JWT Bearer token security scheme to the OpenAPI document, and then adds this as the security requirement for all operations that require authorization.

Usage

Copy the SecuritySchemeTransformer.cs file to your project and then configure the transformer in the configureOptions delegate of the AddOpenApi extension method as shown below:

builder.Services.AddOpenApi(options =>
{
    options.AddSecuritySchemeTransformer();
});

TypeTransformer

Swashbuckle provides the MapType method on the AddSwaggerGen options to customize the type and format for a C# type. The TypeTransformer that does more or less the same thing.

Usage

Copy the TypeTransformer.cs file to your project and then configure the transformer in the configureOptions delegate of the AddOpenApi extension method as shown below:

builder.Services.AddOpenApi(options =>
{
    TypeTransformer.MapType<decimal>(new OpenApiSchema { Type = "number", Format = "decimal" });
    options.AddSchemaTransformer(TypeTransformer.TransformAsync);
});