ApiDocAndMock 1.0.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package ApiDocAndMock --version 1.0.1
                    
NuGet\Install-Package ApiDocAndMock -Version 1.0.1
                    
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="ApiDocAndMock" Version="1.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ApiDocAndMock" Version="1.0.1" />
                    
Directory.Packages.props
<PackageReference Include="ApiDocAndMock" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add ApiDocAndMock --version 1.0.1
                    
#r "nuget: ApiDocAndMock, 1.0.1"
                    
#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.
#:package ApiDocAndMock@1.0.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=ApiDocAndMock&version=1.0.1
                    
Install as a Cake Addin
#tool nuget:?package=ApiDocAndMock&version=1.0.1
                    
Install as a Cake Tool

ApiMockDataFactory

A comprehensive API mocking utility for .NET projects, leveraging the power of Bogus for seamless data generation.


Installation

Install the package via NuGet:

dotnet add package ApiDocAndMock

Usage

Service Configuration

Integrate the package with your application by adding the required services:

// Main service setup
builder.Services.AddDocAndMock();

// Optional: Add authentication for API mocking
builder.Services.AddMockAuthentication();

// Swagger integration for documentation and mock APIs
builder.Services.AddMockSwagger(includeSecurity: true, includAnnotations: true);

// Optional: Add an in-memory database for simulating CRUD operations
builder.Services.AddMemoryDb();

Application Middleware

Configure the application middleware to enable API documentation and mocking:

var app = builder.Build();

// Enable API documentation and mock utilities
app.UseApiDocAndMock(useAuthentication: true, useMockOutcome: true);

// Enable Swagger for API visualization
app.UseSwagger();
app.UseSwaggerUI();

Configuration

Response Types

Customize default response types or add new ones for specific HTTP status codes:

builder.Services.AddCommonResponseConfigurations(config =>
{
    config.RegisterResponseExample(500, new ProblemDetails
    {
        Title = "Custom Internal Server Error",
        Status = 500,
        Detail = "A custom error occurred. Please contact support."
    });

    config.RegisterResponseExample(403, new ProblemDetails
    {
        Title = "Forbidden",
        Status = 403,
        Detail = "You do not have permission to access this resource."
    });
});

Default Faker Rules

Define default faker rules to override existing rules and generate mock data for specific property names:

builder.Services.AddDefaultFakerRules(rules =>
{
    rules["Phone"] = faker => "+44 " + faker.Phone.PhoneNumber(); 
});

Mocking Configurations

Set up custom mocking configurations to apply specific rules to request and response objects. This ensures that objects adhere to predefined formats, with fallback defaults provided by Bogus.

builder.Services.AddMockingConfigurations(config =>
{
    // Configure Booking object
    config.RegisterConfiguration<Booking>(cfg =>
    {
        cfg
            .ForPropertyObject<Room>("Room")
            .ForPropertyObject<Contact>("PrimaryContact");
    });

    // Configure Hotel object
    config.RegisterConfiguration<Hotel>(cfg =>
    {
        cfg
            .ForProperty("Name", faker => faker.Company.CompanyName())
            .ForPropertyObjectList<Room>("Rooms", 5)
            .ForPropertyObjectList<Booking>("Bookings", 5);
    });
});

Notes on Mocking

  • ForPropertyObject: Applies rules to a specific nested object within a response.
  • ForPropertyObjectList: Configures a list of nested objects with a specified number of items.
  • If a property lacks an explicit rule, a default value is assigned based on its type. Undefined types default to null.

OpenApiExtensions For MinimalApi endpoints
OpenApiMockExtensions For MinimalApi Endpoints
GetMockFromMemoryDb Usage
UpdateMockWithMemoryDb Usage
CreateMockWithMemoryDb Usage
DeleteMockWithMemoryDb Usage

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows 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.2.4 258 1/8/2025
1.2.3 90 1/8/2025
1.2.2 94 1/8/2025
1.2.1 125 1/4/2025
1.2.0 107 1/2/2025
1.1.1 107 12/26/2024
1.1.0 128 12/24/2024
1.0.2 124 12/21/2024
1.0.1 119 12/21/2024
1.0.0 121 12/21/2024