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
- Host: GitHub
- URL: https://github.com/gpbultz/apidocandmock
- Owner: gpbultz
- License: other
- Created: 2024-12-20T10:18:47.000Z (10 months ago)
- Default Branch: master
- Last Pushed: 2025-01-08T20:33:51.000Z (9 months ago)
- Last Synced: 2025-02-08T23:35:38.472Z (8 months ago)
- Topics: api, bogus, minimalapi, mocking, net8, openapi, reflection
- Language: C#
- Homepage:
- Size: 193 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE-THIRD-PARTY.md
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)