GitXplorerGitXplorer
m

aspnet-transformer-gallery

public
3 stars
0 forks
0 issues

Commits

List of commits on branch main.
Unverified
242cab9108a5eb6bdbc1648cb3a988b52b2f4532

Add CanonicalDocument Transformer

mmikekistler committed 4 days ago
Unverified
0b2c92d6abc21a9ebdde90ba6e95bf96f4c2b413

Add ProblemResponse Transformer

mmikekistler committed 22 days ago
Unverified
7bf8c1d3eaf45ca8b612808977f777eb405dee8f

Add Spectral VSCode extension and ruleset

mmikekistler committed a month ago
Unverified
2e6fb2663e0569d9433c13469c68806587939692

Remove default: null in NullableTransformer

mmikekistler committed a month ago
Unverified
4affcfdbb2a00447a98596f0c42aa0a07dd35f06

Improved nullable transformer

mmikekistler committed a month ago
Unverified
58bec7434d8f5cdf254800e3d8c3276f162e8a77

Add NullableTransformer

mmikekistler committed a month 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);
});