OpenApiRequestExample 1.0.5

dotnet add package OpenApiRequestExample --version 1.0.5                
NuGet\Install-Package OpenApiRequestExample -Version 1.0.5                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="OpenApiRequestExample" Version="1.0.5" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add OpenApiRequestExample --version 1.0.5                
#r "nuget: OpenApiRequestExample, 1.0.5"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install OpenApiRequestExample as a Cake Addin
#addin nuget:?package=OpenApiRequestExample&version=1.0.5

// Install OpenApiRequestExample as a Cake Tool
#tool nuget:?package=OpenApiRequestExample&version=1.0.5                

OpenAPI Request Example Generator

OpenAPI Request Example Generator is a NuGet package designed for .NET 9 and above to simplify adding request examples to your API documentation in Swagger UI, ReDoc UI or Scalar UI. By using simple attributes on your API controller actions, the package automatically generates an OpenAPI transform document, enriching your API docs with meaningful examples. It is currently limited to standard API controllers and does not support Minimal APIs

Features

  • Seamless Integration with Swagger UI, ReDoc and Scalar UI.
  • Simple Attribute-Based Configuration � just add attributes to your API actions.
  • Automatic OpenAPI Transform Document Generation.
  • Minimal Setup � install, annotate, and configure your OpenAPI services.

Installation

Install the NuGet package via the .NET CLI:

dotnet add package OpenApiRequestExample

Or via the Package Manager Console:

Install-Package OpenApiRequestExample

Getting Started

1. Annotate Your API Actions

Add the [RequestExample] attribute to your API controller actions to specify request examples.


[ApiController]
[Route("api/[controller]")]
public class SampleController : ControllerBase
{
    [HttpPost]
    [RequestExample(typeof(WeatherForecastExample))]
    public IActionResult CreateItem(WeatherForecastDto weatherForecastDto)
    {
        return Ok();
    }
}

2. Providing Example Data via Static Class

To define reusable examples, create a static class with a dictionary of named examples:

public static class WeatherForecastExample
{
    public static IDictionary<string, Microsoft.OpenApi.Models.OpenApiExample> Example => new Dictionary<string, Microsoft.OpenApi.Models.OpenApiExample>
    {
        ["SunnyExample"] = new Microsoft.OpenApi.Models.OpenApiExample
        {
            Summary = "Sunny Weather",
            Value = new Microsoft.OpenApi.Any.OpenApiObject
            {
                ["Date"] = new Microsoft.OpenApi.Any.OpenApiString("2021-07-01"),
                ["Summary"] = new Microsoft.OpenApi.Any.OpenApiString("Sunny"),
            }
        },
        ["RainyExample"] = new Microsoft.OpenApi.Models.OpenApiExample
        {
            Summary = "Rainy Weather",
            Value = new Microsoft.OpenApi.Any.OpenApiObject
            {
                ["Date"] = new Microsoft.OpenApi.Any.OpenApiString("2021-07-02"),
                ["Summary"] = new Microsoft.OpenApi.Any.OpenApiString("Rainy"),
            }
        }
    };
}

3. Attribute Options

The [RequestExample] attribute provides additional options for customization:

public RequestExampleAttribute(
    Type exampleProviderType,
    string exampleProviderProperty = "Example",
    string name = "Default",
    bool overwriteExisting = false)
{
    OverwriteExisting = overwriteExisting;
    ExampleProviderType = exampleProviderType;
    ExampleProviderProperty = exampleProviderProperty;
    Name = name;
}
  • exampleProviderType (Required): Specifies the type of the class that provides the example.
  • exampleProviderProperty (Default: "Example"): The property name within the provider class that holds the example value.
  • name (Default: "Default"): A custom name for the example, useful for differentiating multiple examples.
  • overwriteExisting (Default: false): Determines if existing examples should be overwritten.

To specify an example with a custom name:

[RequestExample(typeof(WeatherForecastExample), name: "MyExample")]

To force an override of an existing example:

[RequestExample(typeof(WeatherForecastExample), overwriteExisting: true)]

4. Configure OpenAPI Services

Add the transform document generation to your OpenAPI configuration in Program.cs.

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

3. Run Your Application

Start your application and navigate to the Swagger UI or Scalar UI. You'll see the request examples displayed for your API endpoints.

Example Output

After adding the attribute and configuring the services, your Swagger UI or Scalar UI will display request examples like this:

{
  "Date": "2021-07-01",
  "Summary": "Sunny"
}
{
  "Date": "2021-07-02",
  "Summary": "Rainy"
}

Customization

You can customize the request examples by:

  • Using different models with the [RequestExample] attribute.
  • Specifying complex JSON structures for advanced request bodies.

License

This project is licensed under the MIT License.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
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.0.5 43 3/29/2025
1.0.4 97 2/8/2025
1.0.4-preview.20250208210719 60 2/8/2025