OpenAPI2JsonSchema 1.1.4

.NET Core 3.1
Install-Package OpenAPI2JsonSchema -Version 1.1.4
dotnet add package OpenAPI2JsonSchema --version 1.1.4
<PackageReference Include="OpenAPI2JsonSchema" Version="1.1.4" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add OpenAPI2JsonSchema --version 1.1.4
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: OpenAPI2JsonSchema, 1.1.4"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install OpenAPI2JsonSchema as a Cake Addin
#addin nuget:?package=OpenAPI2JsonSchema&version=1.1.4

// Install OpenAPI2JsonSchema as a Cake Tool
#tool nuget:?package=OpenAPI2JsonSchema&version=1.1.4
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

OpenApi2JsonSchema

Getting Started

Create OpenAPI-specifiation

First you will need to create an OpenAPI-specification, which you'll be translating to Json-Schema later.

Example

If you use Swashbuckle this might look something like this:

ConfigureServices()-method

services.AddSwaggerGen(c =>
      {
        c.SwaggerDoc("v1", new OpenApiInfo {Title = "MyApplication", Version = "v1"});
        var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
        var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
        c.IncludeXmlComments(xmlPath);
        c.AddFluentValidationRules();
      });

In this example, an OpenAPI-specification is created. In order to keep Data Annotations out of its model, this examples uses FluentValidationRules, that can be added easily using the last line in the example above from Swashbuckle.FluentValidation.

Configure()-method

app.UseSwagger();

if (env.IsDevelopment())
{
    app.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", "MyApplication v1"); });
}

Installation

Install the library using NuGet-Package Manager:

Install-Package OpenAPI2JsonSchema

Dependency Injection

First import the following Package in Startup.cs:

using OpenApi2JsonSchema.DependencyInjection;
using OpenApi2JsonSchema.Configuration;

Now, in order to be able to use the library, simply add the following line to the ConfigureServices()-method in Startup.cs:

services.AddJsonSchemaGenerator(new JsonSchemaGeneratorConfiguration
            {
                OpenApiFileDownloadPath = "swagger-file.json",
                OpenApiUrl = "http://localhost:61306/swagger/v1/swagger.json"
            });

The JsonSchemaGeneratorConfiguration allows to specify the following optional properties:

  • OpenApiFileDownloadPath → Path where to save the downloaded file (ignored if caching is disabled)
  • OpenApiUrl → URL to the OpenAPI-specification
  • CachingDisabled → Specify wheter or not caching should be disabled. Default is false.

Note that if you don't specify an URL for the OpenAPI-specification here in ConfigureServices()-method, you will need to specify it whenever you use the JsonSchemaGenerator-instance.

Usage

You can generate a JsonSchema for any type using:

var schema = JsonSchemaGenerator.GetSchemaWithOpenApi<T>("<url-to-openapi-file>");

If not specified in Startup.cs, just replace <url-to-openapi-file> with the URL that points to your OpenAPI-JSON-file, otherwise pass no parameter.

Configuration

Delete OpenAPI-file on Startup

Since the library downloads the specified OpenAPI-file and caches it for further usage, you can optionally delete the downloaded file on Startup by adding the following lines to the Configure()-method in Startup.cs:

app.UseOpenApi2JsonSchemaGenerator(new JsonSchemaGeneratorConfiguration
{
    OpenApiFileDownloadPath = "swagger-file.json"
});

Note that the if you did not set the DownloadPath to the OpenAPI-file in ConfigureServices()-method, you don't need to pass a parameter.

Product Versions
.NET net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows
.NET Core netcoreapp3.1
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.1.4 394 6/8/2020
1.1.3 319 6/3/2020
1.1.2 298 5/25/2020
1.1.1 313 3/25/2020
1.1.0 311 3/24/2020
1.0.0 313 3/20/2020