/// <summary>
        /// Gets an <see cref="EnumerableWrapperProvider"/> for the provided context.
        /// </summary>
        /// <param name="context">The <see cref="WrapperProviderContext"/>.</param>
        /// <returns>An instance of <see cref="EnumerableWrapperProvider"/> if the declared type is
        /// an interface and implements <see cref="IEnumerable{T}"/>.</returns>
        public IWrapperProvider?GetProvider(WrapperProviderContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (context.IsSerialization)
            {
                // Example: IEnumerable<SerializableError>
                var declaredType = context.DeclaredType;

                // We only wrap interfaces types(ex: IEnumerable<T>, IQueryable<T>, IList<T> etc.) and not
                // concrete types like List<T>, Collection<T> which implement IEnumerable<T>.
                if (declaredType != null && declaredType.IsInterface && declaredType.IsGenericType)
                {
                    var enumerableOfT = ClosedGenericMatcher.ExtractGenericInterface(
                        declaredType,
                        typeof(IEnumerable <>));
                    if (enumerableOfT != null)
                    {
                        var elementType            = enumerableOfT.GenericTypeArguments[0];
                        var wrapperProviderContext = new WrapperProviderContext(elementType, context.IsSerialization);

                        var elementWrapperProvider =
                            _wrapperProviderFactories.GetWrapperProvider(wrapperProviderContext);

                        return(new EnumerableWrapperProvider(enumerableOfT, elementWrapperProvider));
                    }
                }
            }

            return(null);
        }
示例#2
0
        public IWrapperProvider GetProvider(WrapperProviderContext context)
        {
            if (context.DeclaredType == typeof(ProblemDetails))
            {
                if (_options.AllowRfc7807CompliantProblemDetailsFormat)
                {
                    return(new WrapperProvider(typeof(ProblemDetailsWrapper), p => new ProblemDetailsWrapper((ProblemDetails)p)));
                }
                else
                {
#pragma warning disable CS0618 // Type or member is obsolete
                    return(new WrapperProvider(typeof(ProblemDetails21Wrapper), p => new ProblemDetails21Wrapper((ProblemDetails)p)));

#pragma warning restore CS0618 // Type or member is obsolete
                }
            }

            if (context.DeclaredType == typeof(ValidationProblemDetails))
            {
                if (_options.AllowRfc7807CompliantProblemDetailsFormat)
                {
                    return(new WrapperProvider(typeof(ValidationProblemDetailsWrapper), p => new ValidationProblemDetailsWrapper((ValidationProblemDetails)p)));
                }
                else
                {
#pragma warning disable CS0618 // Type or member is obsolete
                    return(new WrapperProvider(typeof(ValidationProblemDetails21Wrapper), p => new ValidationProblemDetails21Wrapper((ValidationProblemDetails)p)));

#pragma warning restore CS0618 // Type or member is obsolete
                }
            }

            return(null);
        }
        /// <summary>
        /// Gets an instance of <see cref="IWrapperProvider"/> for the supplied
        /// type.
        /// </summary>
        /// <param name="wrapperProviderFactories">A list of <see cref="IWrapperProviderFactory"/>.</param>
        /// <param name="wrapperProviderContext">The <see cref="WrapperProviderContext"/>.</param>
        /// <returns>An instance of <see cref="IWrapperProvider"/> if there is a wrapping provider for the
        /// supplied type, else null.</returns>
        public static IWrapperProvider GetWrapperProvider(
            this IEnumerable <IWrapperProviderFactory> wrapperProviderFactories,
            WrapperProviderContext wrapperProviderContext)
        {
            if (wrapperProviderFactories == null)
            {
                throw new ArgumentNullException(nameof(wrapperProviderFactories));
            }

            if (wrapperProviderContext == null)
            {
                throw new ArgumentNullException(nameof(wrapperProviderContext));
            }

            foreach (var wrapperProviderFactory in wrapperProviderFactories)
            {
                var wrapperProvider = wrapperProviderFactory.GetProvider(wrapperProviderContext);
                if (wrapperProvider != null)
                {
                    return(wrapperProvider);
                }
            }

            return(null);
        }
示例#4
0
        public IWrapperProvider GetProvider(WrapperProviderContext context)
        {
            if (context.DeclaredType == DeclaredType)
            {
                return(new WrapperProvider(this));
            }

            return(null);
        }
示例#5
0
        public void GetProvider_ReturnsNull_IfTypeDoesNotMatch()
        {
            // Arrange
            var providerFactory = new ProblemDetailsWrapperProviderFactory();
            var context         = new WrapperProviderContext(typeof(SerializableError), isSerialization: true);

            // Act
            var provider = providerFactory.GetProvider(context);

            // Assert
            Assert.Null(provider);
        }
示例#6
0
        public void GetProvider_ReturnsNull_ForCustomProblemDetails()
        {
            // Arrange
            var providerFactory = new ProblemDetailsWrapperProviderFactory();
            var instance        = new CustomProblemDetails();
            var context         = new WrapperProviderContext(instance.GetType(), isSerialization: true);

            // Act
            var provider = providerFactory.GetProvider(context);

            // Assert
            Assert.Null(provider);
        }
示例#7
0
        public IWrapperProvider?GetProvider(WrapperProviderContext context)
        {
            if (context.DeclaredType == typeof(ProblemDetails))
            {
                return(new WrapperProvider(typeof(ProblemDetailsWrapper), p => new ProblemDetailsWrapper((ProblemDetails)p !)));
            }

            if (context.DeclaredType == typeof(ValidationProblemDetails))
            {
                return(new WrapperProvider(typeof(ValidationProblemDetailsWrapper), p => new ValidationProblemDetailsWrapper((ValidationProblemDetails)p !)));
            }

            return(null);
        }
示例#8
0
        /// <summary>
        /// Creates an instance of <see cref="SerializableErrorWrapperProvider"/> if the provided
        /// <paramref name="context"/>'s <see cref="WrapperProviderContext.DeclaredType"/> is
        /// <see cref="Microsoft.AspNetCore.Mvc.SerializableError"/>.
        /// </summary>
        /// <param name="context">The <see cref="WrapperProviderContext"/>.</param>
        /// <returns>
        /// An instance of <see cref="SerializableErrorWrapperProvider"/> if the provided <paramref name="context"/>'s
        /// <see cref="WrapperProviderContext.DeclaredType"/> is
        /// <see cref="Microsoft.AspNetCore.Mvc.SerializableError"/>; otherwise <c>null</c>.
        /// </returns>
        public IWrapperProvider?GetProvider(WrapperProviderContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (context.DeclaredType == typeof(SerializableError))
            {
                return(new SerializableErrorWrapperProvider());
            }

            return(null);
        }
        public void GetProvider_ReturnsNull_IfTypeIsSubtype()
        {
            // Arrange
            var provider = new WrapperProviderFactory(
                typeof(ProblemDetails),
                typeof(ProblemDetailsWrapper),
                _ => null);
            var context = new WrapperProviderContext(typeof(ValidationProblemDetails), isSerialization: true);

            // Act
            var result = provider.GetProvider(context);

            // Assert
            Assert.Null(result);
        }
        public void DoesNot_CreateWrapperProvider_ForConcrete_EnumerableOfTImplementations(
            Type declaredType,
            object objectToBeWrapped)
        {
            // Arrange
            var wrapperProviderFactories         = GetWrapperProviderFactories();
            var enumerableWrapperProviderFactory = new EnumerableWrapperProviderFactory(wrapperProviderFactories);
            var wrapperProviderContext           = new WrapperProviderContext(declaredType, isSerialization: true);

            // Act
            var wrapperProvider = enumerableWrapperProviderFactory.GetProvider(wrapperProviderContext);

            // Assert
            Assert.Null(wrapperProvider);
        }
示例#11
0
        public void GetProvider_ReturnsWrapper_ForValidationProblemDetails()
        {
            // Arrange
            var providerFactory = new ProblemDetailsWrapperProviderFactory();
            var instance        = new ValidationProblemDetails();
            var context         = new WrapperProviderContext(instance.GetType(), isSerialization: true);

            // Act
            var provider = providerFactory.GetProvider(context);

            // Assert
            var result  = provider.Wrap(instance);
            var wrapper = Assert.IsType <ValidationProblemDetailsWrapper>(result);

            Assert.Same(instance, wrapper.ProblemDetails);
        }
        public void GetProvider_ReturnsValue_IfTypeMatches()
        {
            // Arrange
            var expected        = new object();
            var providerFactory = new WrapperProviderFactory(
                typeof(ProblemDetails),
                typeof(ProblemDetailsWrapper),
                _ => expected);
            var context = new WrapperProviderContext(typeof(ProblemDetails), isSerialization: true);

            // Act
            var provider = providerFactory.GetProvider(context);
            var result   = provider.Wrap(new ProblemDetails());

            // Assert
            Assert.Same(expected, result);
        }
        public void Creates_WrapperProvider_EnumerableOfTInterface(
            Type declaredType,
            object objectToBeWrapped,
            Type expectedWrappingType)
        {
            // Arrange
            var wrapperProviderFactories         = GetWrapperProviderFactories();
            var enumerableWrapperProviderFactory = new EnumerableWrapperProviderFactory(wrapperProviderFactories);
            var wrapperProviderContext           = new WrapperProviderContext(declaredType, isSerialization: true);

            // Act
            var wrapperProvider = enumerableWrapperProviderFactory.GetProvider(wrapperProviderContext);

            // Assert
            Assert.NotNull(wrapperProvider);
            Assert.Equal(expectedWrappingType, wrapperProvider.WrappingType);
        }
示例#14
0
        public void GetProvider_Returns21CompatibleWrapper_ForValidationProblemDetails()
        {
            // Arrange
            var xmlOptions      = new MvcXmlOptions();
            var providerFactory = new ProblemDetailsWrapperProviderFactory(xmlOptions);
            var instance        = new ValidationProblemDetails();
            var context         = new WrapperProviderContext(instance.GetType(), isSerialization: true);

            // Act
            var provider = providerFactory.GetProvider(context);

            // Assert
            var result = provider.Wrap(instance);

#pragma warning disable CS0618 // Type or member is obsolete
            var wrapper = Assert.IsType <ValidationProblemDetails21Wrapper>(result);
#pragma warning restore CS0618 // Type or member is obsolete
            Assert.Same(instance, wrapper.ProblemDetails);
        }
示例#15
0
        public void GetProvider_ReturnsWrapper_ForProblemDetails()
        {
            // Arrange
            var xmlOptions = new MvcXmlOptions {
                AllowRfc7807CompliantProblemDetailsFormat = true
            };
            var providerFactory = new ProblemDetailsWrapperProviderFactory(xmlOptions);
            var instance        = new ProblemDetails();
            var context         = new WrapperProviderContext(instance.GetType(), isSerialization: true);

            // Act
            var provider = providerFactory.GetProvider(context);

            // Assert
            var result  = provider.Wrap(instance);
            var wrapper = Assert.IsType <ProblemDetailsWrapper>(result);

            Assert.Same(instance, wrapper.ProblemDetails);
        }