Example #1
0
        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));
            }
        }
Example #3
0
 public void TryConvertTo_NonGeneric_OnNonConvertibleType_ReturnsFalse()
 => Assert.IsFalse(FromString.TryConvertTo(typeof(Action), SomeString, out _));
Example #4
0
 public void TryConvertTo_Generic_OnSuccess_ReturnsTrue()
 => Assert.IsTrue(FromString.TryConvertTo <int>(StringValue, out _));
Example #5
0
 public void TryConvertTo_Generic_OnSuccess_YieldsCorrectValue()
 {
     FromString.TryConvertTo <int>(StringValue, out var result);
     Assert.AreEqual(IntValue, result);
 }
Example #6
0
 public void TryConvertTo_Generic_OnNonConvertibleType_ReturnsFalse()
 => Assert.IsFalse(FromString.TryConvertTo <Action>(SomeString, out _));
Example #7
0
 public void TryConvertTo_Generic_OnInvalidString_ReturnsFalse()
 => Assert.IsFalse(FromString.TryConvertTo <int>(SomeString, out _));
Example #8
0
 public void TryConvertTo_NonGeneric_OnSuccess_ReturnsTrue()
 => Assert.IsTrue(FromString.TryConvertTo(typeof(int), StringValue, out _));
Example #9
0
 public void TryConvertTo_NonGeneric_OnSuccess_YieldsCorrectValue()
 {
     FromString.TryConvertTo(typeof(int), StringValue, out var result);
     Assert.AreEqual(IntValue, result);
 }
Example #10
0
 public void TryConvertTo_ArrayNonGeneric_OnNullType_ThrowsArgumentNullException()
 => Assert.Throws <ArgumentNullException>(() => FromString.TryConvertTo(null, ValidStringValues, out _));
Example #11
0
 public void TryConvertTo_NonGeneric_OnInvalidString_ReturnsFalse()
 => Assert.IsFalse(FromString.TryConvertTo(typeof(int), SomeString, out _));
Example #12
0
 public void TryConvertTo_ArrayGeneric_OnSuccess_YieldsCorrectValues()
 {
     FromString.TryConvertTo <int>(ValidStringValues, out var result);
     CollectionAssert.AreEqual(ValidIntValues, (int[])result);
 }
Example #13
0
 public void TryConvertTo_NonGeneric_OnNullType_ThrowsArgumentNullException()
 => Assert.Throws <ArgumentNullException>(() => FromString.TryConvertTo(null, SomeString, out _));
Example #14
0
 public void TryConvertTo_ArrayGeneric_OnNullStrings_ReturnsFalse()
 => Assert.IsFalse(FromString.TryConvertTo <int>((string[])null, out _));
Example #15
0
 public void TryConvertTo_ArrayGeneric_OnInvalidStrings_ReturnsFalse()
 => Assert.IsFalse(FromString.TryConvertTo <int>(InvalidStringValues, out _));
Example #16
0
 public void TryConvertTo_ArrayGeneric_OnNonConvertibleType_ReturnsFalse()
 => Assert.IsFalse(FromString.TryConvertTo <Action>(ValidStringValues, out _));
Example #17
0
 public void TryConvertTo_ArrayNonGeneric_OnNullStrings_ReturnsFalse()
 => Assert.IsFalse(FromString.TryConvertTo(typeof(int), (string[])null, out _));
Example #18
0
 public void TryConvertTo_ArrayNonGeneric_OnInvalidStrings_ReturnsFalse()
 => Assert.IsFalse(FromString.TryConvertTo(typeof(int), InvalidStringValues, out _));