values swagger

This commit is contained in:
Tizian.Breuch
2025-08-01 15:25:18 +02:00
parent 1317844ce5
commit 60e8ea073e
5 changed files with 214 additions and 1 deletions

View File

@@ -198,12 +198,18 @@ builder.Services.AddSwaggerGen(c =>
} }
}); });
c.SchemaFilter<AddExampleSchemaFilter>();
c.OperationFilter<AuthorizeOperationFilter>(); c.OperationFilter<AuthorizeOperationFilter>();
// Endpunktspezifische Beispiele
c.OperationFilter<LoginExampleOperationFilter>(); c.OperationFilter<LoginExampleOperationFilter>();
c.OperationFilter<PaymentMethodExampleOperationFilter>(); c.OperationFilter<PaymentMethodExampleOperationFilter>();
c.OperationFilter<SupplierExampleOperationFilter>(); c.OperationFilter<SupplierExampleOperationFilter>();
c.OperationFilter<ShippingMethodExampleOperationFilter>(); c.OperationFilter<ShippingMethodExampleOperationFilter>();
c.SchemaFilter<AddExampleSchemaFilter>(); c.OperationFilter<AdminCategoryExampleOperationFilter>();
c.OperationFilter<AdminProductExampleOperationFilter>();
c.OperationFilter<CustomerAddressExampleOperationFilter>();
c.OperationFilter<CustomerOrderExampleOperationFilter>();
}); });

View File

@@ -0,0 +1,59 @@
// src/Webshop.Api/SwaggerFilters/AdminCategoryExampleOperationFilter.cs
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;
using Microsoft.OpenApi.Any;
using System.Net.Mime;
using System.Collections.Generic;
namespace Webshop.Api.SwaggerFilters
{
public class AdminCategoryExampleOperationFilter : IOperationFilter
{
public void Apply(OpenApiOperation operation, OperationFilterContext context)
{
if (context.MethodInfo.DeclaringType == typeof(Controllers.Admin.AdminCategoriesController) &&
context.MethodInfo.Name == "CreateCategory")
{
if (operation.RequestBody == null ||
!operation.RequestBody.Content.TryGetValue(MediaTypeNames.Application.Json, out var mediaType))
{
return;
}
if (mediaType.Examples == null)
{
mediaType.Examples = new Dictionary<string, OpenApiExample>();
}
mediaType.Examples.Clear();
mediaType.Examples["Hauptkategorie"] = new OpenApiExample
{
Summary = "Beispiel: Hauptkategorie",
Value = new OpenApiObject
{
["name"] = new OpenApiString("Elektronik"),
["slug"] = new OpenApiString("elektronik"),
["description"] = new OpenApiString("Alles rund um elektronische Geräte."),
["parentCategoryId"] = new OpenApiNull(),
["isActive"] = new OpenApiBoolean(true),
["displayOrder"] = new OpenApiInteger(1)
}
};
mediaType.Examples["Unterkategorie"] = new OpenApiExample
{
Summary = "Beispiel: Unterkategorie",
Value = new OpenApiObject
{
["name"] = new OpenApiString("Smartphones"),
["slug"] = new OpenApiString("smartphones"),
["description"] = new OpenApiString("Mobile Endgeräte."),
["parentCategoryId"] = new OpenApiString("EXISTING_CATEGORY_ID_HERE"),
["isActive"] = new OpenApiBoolean(true),
["displayOrder"] = new OpenApiInteger(1)
}
};
}
}
}
}

View File

@@ -0,0 +1,51 @@
// src/Webshop.Api/SwaggerFilters/AdminProductExampleOperationFilter.cs
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;
using Microsoft.OpenApi.Any;
using System.Net.Mime;
using System.Collections.Generic;
using System;
namespace Webshop.Api.SwaggerFilters
{
public class AdminProductExampleOperationFilter : IOperationFilter
{
public void Apply(OpenApiOperation operation, OperationFilterContext context)
{
if (context.MethodInfo.DeclaringType == typeof(Controllers.Admin.AdminProductsController) &&
context.MethodInfo.Name == "CreateAdminProduct")
{
if (operation.RequestBody == null ||
!operation.RequestBody.Content.TryGetValue(MediaTypeNames.Application.Json, out var mediaType))
{
return;
}
if (mediaType.Examples == null)
{
mediaType.Examples = new Dictionary<string, OpenApiExample>();
}
mediaType.Examples.Clear();
var uniqueId = Guid.NewGuid().ToString().Substring(0, 8);
mediaType.Examples["Standard"] = new OpenApiExample
{
Summary = "Beispiel: Standardprodukt",
Value = new OpenApiObject
{
["name"] = new OpenApiString($"Neues Produkt {uniqueId}"),
["description"] = new OpenApiString("Detaillierte Beschreibung des neuen Produkts."),
["sku"] = new OpenApiString($"SKU-{uniqueId}"),
["price"] = new OpenApiDouble(99.99),
["isActive"] = new OpenApiBoolean(true),
["isInStock"] = new OpenApiBoolean(true),
["stockQuantity"] = new OpenApiInteger(100),
["slug"] = new OpenApiString($"neues-produkt-{uniqueId}"),
["categoryIds"] = new OpenApiArray { new OpenApiString("EXISTING_CATEGORY_ID_HERE") }
}
};
}
}
}
}

View File

@@ -0,0 +1,46 @@
// src/Webshop.Api/SwaggerFilters/CustomerAddressExampleOperationFilter.cs
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;
using Microsoft.OpenApi.Any;
using System.Net.Mime;
using System.Collections.Generic;
using Webshop.Domain.Enums;
namespace Webshop.Api.SwaggerFilters
{
public class CustomerAddressExampleOperationFilter : IOperationFilter
{
public void Apply(OpenApiOperation operation, OperationFilterContext context)
{
if (context.MethodInfo.DeclaringType == typeof(Controllers.Customer.AddressesController) &&
context.MethodInfo.Name == "CreateAddress")
{
if (operation.RequestBody == null ||
!operation.RequestBody.Content.TryGetValue(MediaTypeNames.Application.Json, out var mediaType))
{
return;
}
if (mediaType.Examples == null)
{
mediaType.Examples = new Dictionary<string, OpenApiExample>();
}
mediaType.Examples.Clear();
mediaType.Examples["Lieferadresse"] = new OpenApiExample
{
Summary = "Beispiel: Lieferadresse",
Value = new OpenApiObject
{
["street"] = new OpenApiString("Musterweg"),
["houseNumber"] = new OpenApiString("1a"),
["city"] = new OpenApiString("Musterstadt"),
["postalCode"] = new OpenApiString("12345"),
["country"] = new OpenApiString("Deutschland"),
["type"] = new OpenApiString(AddressType.Shipping.ToString())
}
};
}
}
}
}

View File

@@ -0,0 +1,51 @@
// src/Webshop.Api/SwaggerFilters/CustomerOrderExampleOperationFilter.cs
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;
using Microsoft.OpenApi.Any;
using System.Net.Mime;
using System.Collections.Generic;
namespace Webshop.Api.SwaggerFilters
{
public class CustomerOrderExampleOperationFilter : IOperationFilter
{
public void Apply(OpenApiOperation operation, OperationFilterContext context)
{
if (context.MethodInfo.DeclaringType == typeof(Controllers.Customer.OrdersController) &&
context.MethodInfo.Name == "CreateOrder")
{
if (operation.RequestBody == null ||
!operation.RequestBody.Content.TryGetValue(MediaTypeNames.Application.Json, out var mediaType))
{
return;
}
if (mediaType.Examples == null)
{
mediaType.Examples = new Dictionary<string, OpenApiExample>();
}
mediaType.Examples.Clear();
mediaType.Examples["Standardbestellung"] = new OpenApiExample
{
Summary = "Beispiel: Standardbestellung",
Value = new OpenApiObject
{
["shippingAddressId"] = new OpenApiString("EXISTING_ADDRESS_ID_HERE"),
["billingAddressId"] = new OpenApiString("EXISTING_ADDRESS_ID_HERE"),
["paymentMethodId"] = new OpenApiString("EXISTING_PAYMENT_METHOD_ID_HERE"),
["shippingMethodId"] = new OpenApiString("EXISTING_SHIPPING_METHOD_ID_HERE"),
["items"] = new OpenApiArray
{
new OpenApiObject
{
["productId"] = new OpenApiString("EXISTING_PRODUCT_ID_HERE"),
["quantity"] = new OpenApiInteger(1)
}
}
}
};
}
}
}
}