Skip to content

Commit 41a6579

Browse files
authored
Merge pull request #34 from IgniteUI/dgdimitrov/openapi-schema-validations
OpenAPI schema validations
2 parents d6cb19c + e7b7453 commit 41a6579

25 files changed

+180
-57
lines changed

NorthwindCRUD/Controllers/CategoriesController.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
namespace NorthwindCRUD.Controllers
22
{
3+
using System.ComponentModel.DataAnnotations;
34
using AutoMapper;
45
using Microsoft.AspNetCore.Authorization;
56
using Microsoft.AspNetCore.Mvc;
@@ -51,8 +52,8 @@ public ActionResult<CategoryDto[]> GetAll()
5152
/// <returns>A PagedResultDto object containing the fetched T and the total record count.</returns>
5253
[HttpGet("GetCategoriesWithSkip")]
5354
public ActionResult<PagedResultDto<CategoryDto>> GetCategoriesWithSkip(
54-
[FromQuery][Attributes.SwaggerSkipParameter] int? skip,
55-
[FromQuery][Attributes.SwaggerTopParameter] int? top,
55+
[FromQuery][Attributes.SwaggerSkipParameter][Range(0, int.MaxValue)] int? skip,
56+
[FromQuery][Attributes.SwaggerTopParameter][Range(0, int.MaxValue)] int? top,
5657
[FromQuery][Attributes.SwaggerOrderByParameter] string? orderBy)
5758
{
5859
try

NorthwindCRUD/Controllers/CustomersController.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
namespace NorthwindCRUD.Controllers
22
{
3+
using System.ComponentModel.DataAnnotations;
34
using AutoMapper;
45
using Microsoft.AspNetCore.Authorization;
56
using Microsoft.AspNetCore.Mvc;
67
using NorthwindCRUD.Models.DbModels;
78
using NorthwindCRUD.Models.Dtos;
89
using NorthwindCRUD.Services;
9-
using Swashbuckle.AspNetCore.Annotations;
1010

1111
[ApiController]
1212
[Route("[controller]")]
@@ -51,8 +51,8 @@ public ActionResult<CustomerDto[]> GetAll()
5151
/// <returns>A PagedResultDto object containing the fetched T and the total record count.</returns>
5252
[HttpGet("GetCustomersWithSkip")]
5353
public ActionResult<PagedResultDto<CustomerDto>> GetCustomersWithSkip(
54-
[FromQuery][Attributes.SwaggerSkipParameter] int? skip,
55-
[FromQuery][Attributes.SwaggerTopParameter] int? top,
54+
[FromQuery][Attributes.SwaggerSkipParameter][Range(0, int.MaxValue)] int? skip,
55+
[FromQuery][Attributes.SwaggerTopParameter][Range(0, int.MaxValue)] int? top,
5656
[FromQuery][Attributes.SwaggerOrderByParameter] string? orderBy)
5757
{
5858
try

NorthwindCRUD/Controllers/EmployeesController.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
namespace NorthwindCRUD.Controllers
22
{
3+
using System.ComponentModel.DataAnnotations;
34
using AutoMapper;
45
using Microsoft.AspNetCore.Authorization;
56
using Microsoft.AspNetCore.Mvc;
67
using NorthwindCRUD.Models.DbModels;
78
using NorthwindCRUD.Models.Dtos;
89
using NorthwindCRUD.Services;
9-
using Swashbuckle.AspNetCore.Annotations;
1010

1111
[ApiController]
1212
[Route("[controller]")]
@@ -53,8 +53,8 @@ public ActionResult<EmployeeDto[]> GetAll()
5353
/// <returns>A PagedResultDto object containing the fetched T and the total record count.</returns>
5454
[HttpGet("GetEmployeesWithSkip")]
5555
public ActionResult<PagedResultDto<EmployeeDto>> GetPagedEmployees(
56-
[FromQuery][Attributes.SwaggerSkipParameter] int? skip,
57-
[FromQuery][Attributes.SwaggerTopParameter] int? top,
56+
[FromQuery][Attributes.SwaggerSkipParameter][Range(0, int.MaxValue)] int? skip,
57+
[FromQuery][Attributes.SwaggerTopParameter][Range(0, int.MaxValue)] int? top,
5858
[FromQuery][Attributes.SwaggerOrderByParameter] string? orderBy)
5959
{
6060
try

NorthwindCRUD/Controllers/OrdersController.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
namespace NorthwindCRUD.Controllers
22
{
3+
using System.ComponentModel.DataAnnotations;
34
using AutoMapper;
45
using Microsoft.AspNetCore.Authorization;
56
using Microsoft.AspNetCore.Mvc;
@@ -56,8 +57,8 @@ public ActionResult<OrderDto[]> GetAll()
5657
/// <returns>A PagedResultDto object containing the fetched T and the total record count.</returns>
5758
[HttpGet("GetPagedOrders")]
5859
public ActionResult<PagedResultDto<OrderDto>> GetAllOrders(
59-
[FromQuery][Attributes.SwaggerSkipParameter] int? skip,
60-
[FromQuery][Attributes.SwaggerTopParameter] int? top,
60+
[FromQuery][Attributes.SwaggerSkipParameter][Range(0, int.MaxValue)] int? skip,
61+
[FromQuery][Attributes.SwaggerTopParameter][Range(0, int.MaxValue)] int? top,
6162
[FromQuery][Attributes.SwaggerOrderByParameter] string? orderBy)
6263
{
6364
try

NorthwindCRUD/Controllers/ProductsController.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
namespace NorthwindCRUD.Controllers
22
{
3-
using System.Globalization;
4-
using System.Reflection;
3+
using System.ComponentModel.DataAnnotations;
54
using AutoMapper;
65
using Microsoft.AspNetCore.Authorization;
76
using Microsoft.AspNetCore.Mvc;
87
using NorthwindCRUD.Models.DbModels;
98
using NorthwindCRUD.Models.Dtos;
109
using NorthwindCRUD.Services;
11-
using Swashbuckle.AspNetCore.Annotations;
1210

1311
[ApiController]
1412
[Route("[controller]")]
@@ -57,8 +55,8 @@ public ActionResult<ProductDto[]> GetAll()
5755
/// <returns>A PagedResultDto object containing the fetched T and the total record count.</returns>
5856
[HttpGet("GetPagedProducts")]
5957
public ActionResult<PagedResultDto<ProductDto>> GetAllProducts(
60-
[FromQuery][Attributes.SwaggerSkipParameter] int? skip,
61-
[FromQuery][Attributes.SwaggerTopParameter] int? top,
58+
[FromQuery][Attributes.SwaggerSkipParameter][Range(0, int.MaxValue)] int? skip,
59+
[FromQuery][Attributes.SwaggerTopParameter][Range(0, int.MaxValue)] int? top,
6260
[FromQuery][Attributes.SwaggerOrderByParameter] string? orderBy)
6361
{
6462
try

NorthwindCRUD/Controllers/RegionsController.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
namespace NorthwindCRUD.Controllers
22
{
3+
using System.ComponentModel.DataAnnotations;
34
using AutoMapper;
45
using Microsoft.AspNetCore.Authorization;
56
using Microsoft.AspNetCore.Mvc;
67
using NorthwindCRUD.Models.DbModels;
78
using NorthwindCRUD.Models.Dtos;
8-
using NorthwindCRUD.Models.InputModels;
99
using NorthwindCRUD.Services;
10-
using Swashbuckle.AspNetCore.Annotations;
1110

1211
[ApiController]
1312
[Route("[controller]")]
@@ -52,8 +51,8 @@ public ActionResult<RegionDto[]> GetAll()
5251
/// <returns>A PagedResultDto object containing the fetched T and the total record count.</returns>
5352
[HttpGet("GetPagedRegions")]
5453
public ActionResult<PagedResultDto<RegionDto>> GetAllRegions(
55-
[FromQuery][Attributes.SwaggerSkipParameter] int? skip,
56-
[FromQuery][Attributes.SwaggerTopParameter] int? top,
54+
[FromQuery][Attributes.SwaggerSkipParameter][Range(0, int.MaxValue)] int? skip,
55+
[FromQuery][Attributes.SwaggerTopParameter][Range(0, int.MaxValue)] int? top,
5756
[FromQuery][Attributes.SwaggerOrderByParameter] string? orderBy)
5857
{
5958
try

NorthwindCRUD/Controllers/ShippersController.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
namespace NorthwindCRUD.Controllers
22
{
3+
using System.ComponentModel.DataAnnotations;
34
using AutoMapper;
45
using Microsoft.AspNetCore.Authorization;
56
using Microsoft.AspNetCore.Mvc;
67
using NorthwindCRUD.Models.DbModels;
78
using NorthwindCRUD.Models.Dtos;
8-
using NorthwindCRUD.Models.InputModels;
99
using NorthwindCRUD.Services;
10-
using Swashbuckle.AspNetCore.Annotations;
1110

1211
[ApiController]
1312
[Route("[controller]")]
@@ -52,8 +51,8 @@ public ActionResult<ShipperDto[]> GetAll()
5251
/// <returns>A PagedResultDto object containing the fetched T and the total record count.</returns>
5352
[HttpGet("GetPagedShippersWithSkip")]
5453
public ActionResult<PagedResultDto<ShipperDto>> GetPagedShippersWithSkip(
55-
[FromQuery][Attributes.SwaggerSkipParameter] int? skip,
56-
[FromQuery][Attributes.SwaggerTopParameter] int? top,
54+
[FromQuery][Attributes.SwaggerSkipParameter][Range(0, int.MaxValue)] int? skip,
55+
[FromQuery][Attributes.SwaggerTopParameter][Range(0, int.MaxValue)] int? top,
5756
[FromQuery][Attributes.SwaggerOrderByParameter] string? orderBy)
5857
{
5958
try

NorthwindCRUD/Controllers/SuppliersController.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
namespace NorthwindCRUD.Controllers
22
{
3+
using System.ComponentModel.DataAnnotations;
34
using AutoMapper;
45
using Microsoft.AspNetCore.Authorization;
56
using Microsoft.AspNetCore.Mvc;
67
using NorthwindCRUD.Models.DbModels;
78
using NorthwindCRUD.Models.Dtos;
8-
using NorthwindCRUD.Models.InputModels;
99
using NorthwindCRUD.Services;
10-
using Swashbuckle.AspNetCore.Annotations;
1110

1211
[ApiController]
1312
[Route("[controller]")]
@@ -52,8 +51,8 @@ public ActionResult<SupplierDto[]> GetAll()
5251
/// <returns>A PagedResultDto object containing the fetched T and the total record count.</returns>
5352
[HttpGet("GetPagedSuppliers")]
5453
public ActionResult<PagedResultDto<SupplierDto>> GetAllSuppliers(
55-
[FromQuery][Attributes.SwaggerSkipParameter] int? skip,
56-
[FromQuery][Attributes.SwaggerTopParameter] int? top,
54+
[FromQuery][Attributes.SwaggerSkipParameter][Range(0, int.MaxValue)] int? skip,
55+
[FromQuery][Attributes.SwaggerTopParameter][Range(0, int.MaxValue)] int? top,
5756
[FromQuery][Attributes.SwaggerOrderByParameter] string? orderBy)
5857
{
5958
try

NorthwindCRUD/Controllers/TerritoriesController.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
namespace NorthwindCRUD.Controllers
22
{
3+
using System.ComponentModel.DataAnnotations;
34
using AutoMapper;
45
using Microsoft.AspNetCore.Authorization;
56
using Microsoft.AspNetCore.Mvc;
67
using NorthwindCRUD.Models.DbModels;
78
using NorthwindCRUD.Models.Dtos;
8-
using NorthwindCRUD.Models.InputModels;
99
using NorthwindCRUD.Services;
10-
using Swashbuckle.AspNetCore.Annotations;
1110

1211
[ApiController]
1312
[Route("[controller]")]
@@ -54,8 +53,8 @@ public ActionResult<TerritoryDto[]> GetAll()
5453
/// <returns>A PagedResultDto object containing the fetched T and the total record count.</returns>
5554
[HttpGet("GetPagedTerritories")]
5655
public ActionResult<PagedResultDto<TerritoryDto>> GetAllTerritories(
57-
[FromQuery][Attributes.SwaggerSkipParameter] int? skip,
58-
[FromQuery][Attributes.SwaggerTopParameter] int? top,
56+
[FromQuery][Attributes.SwaggerSkipParameter][Range(0, int.MaxValue)] int? skip,
57+
[FromQuery][Attributes.SwaggerTopParameter][Range(0, int.MaxValue)] int? top,
5958
[FromQuery][Attributes.SwaggerOrderByParameter] string? orderBy)
6059
{
6160
try
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using Microsoft.OpenApi.Any;
2+
using Microsoft.OpenApi.Models;
3+
using Swashbuckle.AspNetCore.SwaggerGen;
4+
5+
namespace NorthwindCRUD.Filters
6+
{
7+
public class EnumSchemaFilter : ISchemaFilter
8+
{
9+
public void Apply(OpenApiSchema schema, SchemaFilterContext context)
10+
{
11+
if (context.Type.IsEnum)
12+
{
13+
var enumValues = Enum.GetValues(context.Type)
14+
.Cast<object>()
15+
.Select(e => new OpenApiString(e.ToString()))
16+
.ToList<IOpenApiAny>();
17+
18+
schema.Enum = enumValues;
19+
}
20+
}
21+
}
22+
}

0 commit comments

Comments
 (0)