This repository has been archived by the owner on Sep 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
144 additions
and
59 deletions.
There are no files selected for viewing
9 changes: 9 additions & 0 deletions
9
VirtoCommerce.Storefront/Infrastructure/Swagger/ApiSchemaOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
namespace VirtoCommerce.Storefront.Infrastructure.Swagger | ||
{ | ||
public class ApiSchemaOptions | ||
{ | ||
public bool NameAndOrderRequestBody { get; set; } | ||
|
||
public OpenApiSpecificationVersion OpenApiSpecificationVersion { get; set; } | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
VirtoCommerce.Storefront/Infrastructure/Swagger/ApiSchemaVersion.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace VirtoCommerce.Storefront.Infrastructure.Swagger | ||
{ | ||
public enum ApiSchemaVersion | ||
{ | ||
V1, | ||
V2 | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
VirtoCommerce.Storefront/Infrastructure/Swagger/NameAndOrderRequestBodyFilter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using Microsoft.Extensions.Options; | ||
using Microsoft.OpenApi.Any; | ||
using Microsoft.OpenApi.Models; | ||
using Swashbuckle.AspNetCore.SwaggerGen; | ||
|
||
namespace VirtoCommerce.Storefront.Infrastructure.Swagger | ||
{ | ||
public class NameAndOrderRequestBodyFilter: IRequestBodyFilter | ||
{ | ||
private readonly SwaggerOptions _swaggerOptions; | ||
|
||
public NameAndOrderRequestBodyFilter(IOptions<SwaggerOptions> swaggerOptions) | ||
{ | ||
_swaggerOptions = swaggerOptions.Value; | ||
} | ||
|
||
public void Apply(OpenApiRequestBody requestBody, RequestBodyFilterContext context) | ||
{ | ||
// Even if it's v2 or request body shouldn't be named | ||
// we want name it because of bugs and different approaches in generation tools | ||
var bodyName = _swaggerOptions.Schema.NameAndOrderRequestBody | ||
? context.BodyParameterDescription.Name | ||
: "body"; | ||
requestBody.Extensions.Add("x-name", new OpenApiString(bodyName)); | ||
requestBody.Extensions.Add("x-codegen-request-body-name", new OpenApiString(bodyName)); | ||
requestBody.Extensions.Add("x-ms-requestBody-name", new OpenApiString(bodyName)); | ||
|
||
if (_swaggerOptions.Schema.NameAndOrderRequestBody) | ||
{ | ||
var bodyPosition = context.BodyParameterDescription.ParameterInfo().Position; | ||
requestBody.Extensions.Add("x-position", new OpenApiInteger(bodyPosition)); | ||
requestBody.Extensions.Add("x-ms-requestBody-index", new OpenApiInteger(bodyPosition)); | ||
} | ||
} | ||
} | ||
} |
24 changes: 0 additions & 24 deletions
24
VirtoCommerce.Storefront/Infrastructure/Swagger/NewtonsoftJsonIgnoreFilter.cs
This file was deleted.
Oops, something went wrong.
8 changes: 8 additions & 0 deletions
8
VirtoCommerce.Storefront/Infrastructure/Swagger/OpenApiSpecificationVersion.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace VirtoCommerce.Storefront.Infrastructure.Swagger | ||
{ | ||
public enum OpenApiSpecificationVersion | ||
{ | ||
V2, | ||
V3 | ||
} | ||
} |
31 changes: 0 additions & 31 deletions
31
VirtoCommerce.Storefront/Infrastructure/Swagger/OptionalParametersFilter.cs
This file was deleted.
Oops, something went wrong.
42 changes: 42 additions & 0 deletions
42
VirtoCommerce.Storefront/Infrastructure/Swagger/ParameterOrderFilter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using Microsoft.Extensions.Options; | ||
using Microsoft.OpenApi.Any; | ||
using Microsoft.OpenApi.Models; | ||
using Swashbuckle.AspNetCore.SwaggerGen; | ||
|
||
namespace VirtoCommerce.Storefront.Infrastructure.Swagger | ||
{ | ||
public class ParameterOrderFilter: IParameterFilter | ||
{ | ||
private readonly SwaggerOptions _swaggerOptions; | ||
|
||
public ParameterOrderFilter(IOptions<SwaggerOptions> swaggerOptions) | ||
{ | ||
_swaggerOptions = swaggerOptions.Value; | ||
} | ||
|
||
public void Apply(OpenApiParameter parameter, ParameterFilterContext context) | ||
{ | ||
if (_swaggerOptions.Schema.NameAndOrderRequestBody) | ||
{ | ||
// Explicitly specify parameters position | ||
// Position for store and language is unknown and they should be last, so use int.MaxValue relative values | ||
if (context.ParameterInfo != null) | ||
{ | ||
parameter.Extensions.Add("x-position", new OpenApiInteger(context.ParameterInfo.Position)); | ||
} | ||
else | ||
{ | ||
switch (parameter.Name) | ||
{ | ||
case "store": | ||
parameter.Extensions.Add("x-position", new OpenApiInteger(int.MaxValue - 1)); | ||
break; | ||
case "language": | ||
parameter.Extensions.Add("x-position", new OpenApiInteger(int.MaxValue)); | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
VirtoCommerce.Storefront/Infrastructure/Swagger/RequireRequestBodyFilter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using Microsoft.OpenApi.Models; | ||
using Swashbuckle.AspNetCore.SwaggerGen; | ||
|
||
namespace VirtoCommerce.Storefront.Infrastructure.Swagger | ||
{ | ||
public class RequireRequestBodyFilter: IRequestBodyFilter | ||
{ | ||
public void Apply(OpenApiRequestBody requestBody, RequestBodyFilterContext context) | ||
{ | ||
requestBody.Required = true; | ||
} | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
VirtoCommerce.Storefront/Infrastructure/Swagger/SwaggerOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace VirtoCommerce.Storefront.Infrastructure.Swagger | ||
{ | ||
public class SwaggerOptions | ||
{ | ||
public ApiSchemaOptions Schema { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters