private object ConvertValue(OpenApiSchema schema, string value) { if (this.logger.IsEnabled(LogLevel.Debug)) { this.logger.LogDebug("Converting value to match [{schema}]", schema.GetLoggingInformation()); } foreach (IOpenApiConverter converter in this.converters) { if (converter.CanConvert(schema)) { if (this.logger.IsEnabled(LogLevel.Debug)) { this.logger.LogDebug( "Matched converter [{converter}] to the [{schema}]", converter.GetType(), schema.GetLoggingInformation()); } return(converter.ConvertFrom(value, schema)); } } if (this.logger.IsEnabled(LogLevel.Debug)) { this.logger.LogDebug( "Failed to convert value with [{schema}], falling back to just type", schema.GetLoggingInformation()); } // We didn't hit anything directly, so let's fall back to just the type alone foreach (IOpenApiConverter converter in this.converters) { if (converter.CanConvert(schema, true)) { if (this.logger.IsEnabled(LogLevel.Debug)) { this.logger.LogDebug( "Matched converter [{converter}] to the [{schema}] for value, ignoring format", converter.GetType(), schema.GetLoggingInformation()); } return(converter.ConvertFrom(value, schema)); } } this.logger.LogError( "Failed to convert value with [{schema}]", schema.GetLoggingInformation()); throw new OpenApiServiceMismatchException($"Unable to convert value to match [{schema.GetLoggingInformation()}]"); }
/// <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; } }
/// <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; } }