/// <summary>Validates the given data, throwing an <see cref="OpenApiBadRequestException"/> augmented with <see cref="OpenApiProblemDetailsExtensions"/> detailing the errors.</summary> /// <param name="data">The data.</param> /// <param name="schema">The schema.</param> public void ValidateAndThrow(JToken?data, OpenApiSchema schema) { if (this.logger.IsEnabled(LogLevel.Debug)) { this.logger.LogDebug("Attempting to validate data to [{schema}]", schema.GetLoggingInformation()); } data ??= JValue.CreateNull(); ICollection <ValidationError> errors = this.Validate(data, schema); if (errors.Count > 0) { Exception exception = new OpenApiBadRequestException("Schema Validation Error", "The content does not conform to the required schema.") .AddProblemDetailsExtension("validationErrors", errors); throw exception; } }
/// <summary>Validates the given data, throwing an <see cref="OpenApiBadRequestException"/> augmented with <see cref="OpenApiProblemDetailsExtensions"/> detailing the errors.</summary> /// <param name="data">The data.</param> /// <param name="schema">The schema.</param> public void ValidateAndThrow(string data, OpenApiSchema schema) { if (this.logger.IsEnabled(LogLevel.Debug)) { this.logger.LogDebug("Attempting to validate data to [{schema}]", schema.GetLoggingInformation()); } ICollection <ValidationError> errors = this.Validate(data, schema); if (errors.Count > 0) { Exception exception = new OpenApiBadRequestException("Schema Validation Error", "The content does not conform to the required schema.") .AddProblemDetailsExtension("validationErrors", errors); this.logger.LogError( "Failed to validate data to [{schema}], The content does not conform to the required schema, [{errors}]", schema.GetLoggingInformation(), errors.Select(error => new { error.Kind, error.Property, error.LineNumber, error.LinePosition })); throw exception; } }
private async Task TryAddBody(HttpRequest request, OpenApiOperationPathTemplate operationPathTemplate, Dictionary <string, object> parameters) { if (this.logger.IsEnabled(LogLevel.Debug)) { this.logger.LogDebug( "Trying to add body for request [{path}] [{method}]", request.Path, request.Method); } // Map the body in to our list too if (operationPathTemplate.Operation.RequestBody == null) { if (this.logger.IsEnabled(LogLevel.Debug)) { this.logger.LogDebug( "Skipped adding body for request [{path}] [{method}], operation request body is null but not required", request.Path, request.Method); } // No request body, we're just fine return; } if (request.Body == null) { if (operationPathTemplate.Operation.RequestBody.Required) { this.logger.LogError( "Failed to add body for request [{path}] [{method}], request body is null but it is required", request.Path, request.Method); throw new OpenApiBadRequestException("The request body is missing."); } else { if (this.logger.IsEnabled(LogLevel.Debug)) { this.logger.LogDebug( "Skipped adding body for request [{path}] [{method}], request body is null but not required", request.Path, request.Method); } return; } } string?requestBaseContentType = this.GetBaseContentType(request.ContentType); if (string.IsNullOrEmpty(requestBaseContentType) || !operationPathTemplate.Operation.RequestBody.Content.TryGetValue(requestBaseContentType, out OpenApiMediaType openApiMediaType)) { if (operationPathTemplate.Operation.RequestBody.Required) { string message = $"The request body does not have a supported media type. It is '{request.ContentType}', but should be one of {string.Join(", ", operationPathTemplate.Operation.RequestBody.Content.Keys)}"; var exception = new OpenApiBadRequestException(message, message); this.logger.LogError( exception, "Failed to add body for request [{path}] [{method}], [{exception}]", request.Path, request.Method, exception.Message); throw exception; } else { if (this.logger.IsEnabled(LogLevel.Debug)) { this.logger.LogDebug( "Skipped adding body for request [{path}] [{method}], request body does not have a supported media type. It is '[{requestContentType}]', but should be one of [{requestBody}] and is not required", request.Path, request.Method, request.ContentType, string.Join(", ", operationPathTemplate?.Operation?.RequestBody?.Content?.Keys)); } return; } } parameters.Add("body", await this.ConvertBodyAsync(openApiMediaType.Schema, request.Body).ConfigureAwait(false)); if (this.logger.IsEnabled(LogLevel.Debug)) { this.logger.LogDebug( "Added body for request [{path}] [{method}]", request.Path, request.Method); } }