async Task <object?> IRequestDataAttribute <WebApiController> .GetRequestDataAsync( WebApiController controller, Type type, string parameterName) { var data = await controller.HttpContext.GetRequestFormDataAsync() .ConfigureAwait(false); var fieldName = FieldName ?? parameterName; if (!data.ContainsKey(fieldName) && BadRequestIfMissing) { throw HttpException.BadRequest($"Missing form field {fieldName}."); } object result = null; if (type.IsArray) { var fieldValues = data.GetValues(fieldName) ?? Array.Empty <string>(); if (!FromString.TryConvertTo(type, fieldValues, out result)) { throw HttpException.BadRequest($"Cannot convert field {fieldName} to an array of {type.GetElementType().Name}."); } return(result); } else { var fieldValue = data.GetValues(fieldName)?.LastOrDefault(); if (fieldValue == null) { if (type.IsValueType) { var parameter = controller.CurrentMethod.GetParameters().FirstOrDefault(p => p.Name == parameterName); result = parameter.HasDefaultValue ? parameter.DefaultValue : Activator.CreateInstance(type); } return(Task.FromResult(result)); } if (!FromString.TryConvertTo(type, fieldValue, out result)) { throw HttpException.BadRequest($"Cannot convert field {fieldName} to {type.Name}."); } return(result); } }
Task <object?> IRequestDataAttribute <WebApiController> .GetRequestDataAsync( WebApiController controller, Type type, string parameterName) { var data = controller.HttpContext.GetRequestQueryData(); var fieldName = FieldName ?? parameterName; if (!data.ContainsKey(fieldName) && BadRequestIfMissing) { throw HttpException.BadRequest($"Missing query field {fieldName}."); } if (type.IsArray) { var fieldValues = data.GetValues(fieldName) ?? Array.Empty <string>(); if (!FromString.TryConvertTo(type, fieldValues, out var result)) { throw HttpException.BadRequest($"Cannot convert field {fieldName} to an array of {type.GetElementType().Name}."); } return(Task.FromResult(result)); } else { var fieldValue = data.GetValues(fieldName)?.LastOrDefault(); if (fieldValue == null) { return(Task.FromResult(type.IsValueType ? Activator.CreateInstance(type) : null)); } if (!FromString.TryConvertTo(type, fieldValue, out var result)) { throw HttpException.BadRequest($"Cannot convert field {fieldName} to {type.Name}."); } return(Task.FromResult(result)); } }
public void TryConvertTo_NonGeneric_OnNonConvertibleType_ReturnsFalse() => Assert.IsFalse(FromString.TryConvertTo(typeof(Action), SomeString, out _));
public void TryConvertTo_Generic_OnSuccess_ReturnsTrue() => Assert.IsTrue(FromString.TryConvertTo <int>(StringValue, out _));
public void TryConvertTo_Generic_OnSuccess_YieldsCorrectValue() { FromString.TryConvertTo <int>(StringValue, out var result); Assert.AreEqual(IntValue, result); }
public void TryConvertTo_Generic_OnNonConvertibleType_ReturnsFalse() => Assert.IsFalse(FromString.TryConvertTo <Action>(SomeString, out _));
public void TryConvertTo_Generic_OnInvalidString_ReturnsFalse() => Assert.IsFalse(FromString.TryConvertTo <int>(SomeString, out _));
public void TryConvertTo_NonGeneric_OnSuccess_ReturnsTrue() => Assert.IsTrue(FromString.TryConvertTo(typeof(int), StringValue, out _));
public void TryConvertTo_NonGeneric_OnSuccess_YieldsCorrectValue() { FromString.TryConvertTo(typeof(int), StringValue, out var result); Assert.AreEqual(IntValue, result); }
public void TryConvertTo_ArrayNonGeneric_OnNullType_ThrowsArgumentNullException() => Assert.Throws <ArgumentNullException>(() => FromString.TryConvertTo(null, ValidStringValues, out _));
public void TryConvertTo_NonGeneric_OnInvalidString_ReturnsFalse() => Assert.IsFalse(FromString.TryConvertTo(typeof(int), SomeString, out _));
public void TryConvertTo_ArrayGeneric_OnSuccess_YieldsCorrectValues() { FromString.TryConvertTo <int>(ValidStringValues, out var result); CollectionAssert.AreEqual(ValidIntValues, (int[])result); }
public void TryConvertTo_NonGeneric_OnNullType_ThrowsArgumentNullException() => Assert.Throws <ArgumentNullException>(() => FromString.TryConvertTo(null, SomeString, out _));
public void TryConvertTo_ArrayGeneric_OnNullStrings_ReturnsFalse() => Assert.IsFalse(FromString.TryConvertTo <int>((string[])null, out _));
public void TryConvertTo_ArrayGeneric_OnInvalidStrings_ReturnsFalse() => Assert.IsFalse(FromString.TryConvertTo <int>(InvalidStringValues, out _));
public void TryConvertTo_ArrayGeneric_OnNonConvertibleType_ReturnsFalse() => Assert.IsFalse(FromString.TryConvertTo <Action>(ValidStringValues, out _));
public void TryConvertTo_ArrayNonGeneric_OnNullStrings_ReturnsFalse() => Assert.IsFalse(FromString.TryConvertTo(typeof(int), (string[])null, out _));
public void TryConvertTo_ArrayNonGeneric_OnInvalidStrings_ReturnsFalse() => Assert.IsFalse(FromString.TryConvertTo(typeof(int), InvalidStringValues, out _));