An open API service indexing awesome lists of open source software.

https://github.com/gpbultz/apidocandmock

🔧OpenApi extensions and Mocking objects using Bogus
https://github.com/gpbultz/apidocandmock

api bogus minimalapi mocking net8 openapi reflection

Last synced: about 2 months ago
JSON representation

🔧OpenApi extensions and Mocking objects using Bogus

Awesome Lists containing this project

README

          

# ApiMockDataFactory

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

---

## Installation

Install the package via NuGet:

```bash
dotnet add package ApiDocAndMock
```

---

## Usage

### Service Configuration
Integrate the package with your application by adding the required services:

```csharp
// 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:

```csharp
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:

```csharp
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:

```csharp
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**.

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

// Configure Hotel object
config.RegisterConfiguration(cfg =>
{
cfg
.ForProperty("Name", faker => faker.Company.CompanyName())
.ForPropertyObjectList("Rooms", 5)
.ForPropertyObjectList("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](./OpenApiExtensions%20For%20MinimalApi%20endpoints.md)
[OpenApiMockExtensions For MinimalApi Endpoints](./OpenApiMockExtensions%20For%20MinimalApi%20Endpoints.md)
[GetMockFromMemoryDb Usage](./GetMockFromMemoryDb%20Usage.md)
[UpdateMockWithMemoryDb Usage](./UpdateMockWIthMemoryDb%20Usage.md)
[CreateMockWithMemoryDb Usage](./CreateMockWithMemoryDb%20Usage.md)
[DeleteMockWithMemoryDb Usage](./DeleteMockWithMemoryDb%20Usage.md)