Skip to content

Commit c018fd1

Browse files
committed
let user override validation level per operation
1 parent 0173e45 commit c018fd1

2 files changed

Lines changed: 10 additions & 4 deletions

File tree

src/OpenAPI.WebApiGenerator/CodeGeneration/OperationGenerator.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ internal partial class Operation
2828
internal const string PathTemplate = "{{pathTemplate}}";
2929
internal const string Method = "{{method.Method}}";
3030
31+
/// <summary>
32+
/// Set validation level for requests and responses
33+
/// </summary>
34+
internal ValidationLevel ValidationLevel { get; init; } = ValidationLevel.Detailed;
35+
3136
/// <summary>
3237
/// Should responses be validated?
3338
/// If the response has already been validated, this can be disabled to avoid redundant validation.
@@ -56,8 +61,7 @@ internal static async Task HandleAsync(
5661
var request = await Request.BindAsync(context, cancellationToken)
5762
.ConfigureAwait(false);
5863
59-
var validationLevel = ValidationLevel.Detailed;
60-
var validationContext = request.Validate(validationLevel);
64+
var validationContext = request.Validate(operation.ValidationLevel);
6165
if (!validationContext.IsValid)
6266
{
6367
operation.HandleRequestValidationError(validationContext.Results.WithLocation(configuration.OpenApiSpecificationUri))
@@ -69,7 +73,7 @@ internal static async Task HandleAsync(
6973
.ConfigureAwait(false);
7074
if (operation.ValidateResponse)
7175
{
72-
validationContext = response.Validate(validationLevel);
76+
validationContext = response.Validate(operation.ValidationLevel);
7377
if (!validationContext.IsValid)
7478
{
7579
var validationResult = validationContext.Results.WithLocation(configuration.OpenApiSpecificationUri);

tests/Example.OpenApi20/Paths/FooFooId/Put/Operation.Handler.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ public Operation()
99
{
1010
HandleRequestValidationError = HandleValidationErrors;
1111
ValidateResponse = false;
12+
ValidationLevel = ValidationLevel.Detailed;
1213
}
1314

1415
private static Response.BadRequest400 HandleValidationErrors(ImmutableList<ValidationResult> validationResults)
@@ -35,7 +36,8 @@ internal partial Task<Response> HandleAsync(Request request, CancellationToken c
3536
Status = 2
3637
}
3738
};
38-
var validationContext = response.Validate(ValidationLevel.Detailed);
39+
40+
var validationContext = response.Validate(ValidationLevel);
3941
return !validationContext.IsValid
4042
? throw new JsonValidationException("Response is not valid", validationContext.Results)
4143
: Task.FromResult<Response>(response);

0 commit comments

Comments
 (0)