|
| 1 | +// Copyright (c) Autofac Project. All rights reserved. |
| 2 | +// Licensed under the MIT License. See LICENSE in the project root for license information. |
| 3 | + |
| 4 | +namespace Autofac.Test.Features.OpenGenerics; |
| 5 | + |
| 6 | +/// <summary> |
| 7 | +/// Regression tests for issue #1464: OpenGenericServiceBinder.TryBindOpenGenericTypedService |
| 8 | +/// should not let a non-mappable interface appearing first in the interface list suppress a |
| 9 | +/// valid later interface that CAN be mapped. |
| 10 | +/// </summary> |
| 11 | +public class OpenGenericMultipleInterfaceOrderTests |
| 12 | +{ |
| 13 | + private interface IHandler<in T> |
| 14 | + { |
| 15 | + } |
| 16 | + |
| 17 | + private interface IRequest |
| 18 | + { |
| 19 | + } |
| 20 | + |
| 21 | + private interface IRequest<T> |
| 22 | + { |
| 23 | + } |
| 24 | + |
| 25 | + /// <summary> |
| 26 | + /// Handler whose first interface IS the mappable one (IHandler<IRequest<TParam>>). |
| 27 | + /// This worked before the fix and must continue to work. |
| 28 | + /// </summary> |
| 29 | + private class HandlerMappableFirst<TParam> |
| 30 | + : IHandler<IRequest<TParam>>, IHandler<IRequest> |
| 31 | + { |
| 32 | + } |
| 33 | + |
| 34 | + /// <summary> |
| 35 | + /// Handler whose first interface is NOT mappable for the requested service |
| 36 | + /// (IHandler<IRequest> — closed, no TParam) and the mappable one comes second. |
| 37 | + /// This is the ordering bug reported in #1464. |
| 38 | + /// </summary> |
| 39 | + private class HandlerNonMappableFirst<TParam> |
| 40 | + : IHandler<IRequest>, IHandler<IRequest<TParam>> |
| 41 | + { |
| 42 | + } |
| 43 | + |
| 44 | + /// <summary> |
| 45 | + /// Handler whose class generic parameter is NOT used in the interface it implements. |
| 46 | + /// Assignability rules say this CANNOT be bound as an open-generic IHandler<> because |
| 47 | + /// TParam cannot be inferred from the service arguments. Autofac must not bind this |
| 48 | + /// and must not throw; it must cleanly return no registration. |
| 49 | + /// </summary> |
| 50 | + private class HandlerWithUnrelatedTypeParam<TParam> |
| 51 | + : IHandler<int> |
| 52 | + { |
| 53 | + } |
| 54 | + |
| 55 | + [Fact] |
| 56 | + public void MappableFirstHandlerIsResolvable() |
| 57 | + { |
| 58 | + var builder = new ContainerBuilder(); |
| 59 | + builder |
| 60 | + .RegisterGeneric(typeof(HandlerMappableFirst<>)) |
| 61 | + .As(typeof(IHandler<>).MakeGenericType(typeof(IRequest<>))); |
| 62 | + |
| 63 | + var container = builder.Build(); |
| 64 | + |
| 65 | + var handlers = container.Resolve<IEnumerable<IHandler<IRequest<int>>>>(); |
| 66 | + Assert.Single(handlers); |
| 67 | + Assert.IsType<HandlerMappableFirst<int>>(handlers.Single()); |
| 68 | + } |
| 69 | + |
| 70 | + [Fact] |
| 71 | + public void NonMappableFirstHandlerIsResolvable() |
| 72 | + { |
| 73 | + // Regression for #1464: when the first interface (IHandler<IRequest>) cannot |
| 74 | + // be mapped to the type parameter TParam, the binder must not give up — it |
| 75 | + // must continue to the second interface (IHandler<IRequest<TParam>>) which CAN |
| 76 | + // be mapped. |
| 77 | + var builder = new ContainerBuilder(); |
| 78 | + builder |
| 79 | + .RegisterGeneric(typeof(HandlerNonMappableFirst<>)) |
| 80 | + .As(typeof(IHandler<>).MakeGenericType(typeof(IRequest<>))); |
| 81 | + |
| 82 | + var container = builder.Build(); |
| 83 | + |
| 84 | + var handlers = container.Resolve<IEnumerable<IHandler<IRequest<int>>>>(); |
| 85 | + Assert.Single(handlers); |
| 86 | + Assert.IsType<HandlerNonMappableFirst<int>>(handlers.Single()); |
| 87 | + } |
| 88 | + |
| 89 | + [Fact] |
| 90 | + public void BothHandlerOrderVariantsAreResolvedFromEnumerable() |
| 91 | + { |
| 92 | + // The full scenario from the issue: both handlers registered for the same open |
| 93 | + // generic service — both must appear in the resolved enumerable regardless of |
| 94 | + // which one has the non-mappable interface first. |
| 95 | + var builder = new ContainerBuilder(); |
| 96 | + builder |
| 97 | + .RegisterGeneric(typeof(HandlerMappableFirst<>)) |
| 98 | + .As(typeof(IHandler<>).MakeGenericType(typeof(IRequest<>))); |
| 99 | + builder |
| 100 | + .RegisterGeneric(typeof(HandlerNonMappableFirst<>)) |
| 101 | + .As(typeof(IHandler<>).MakeGenericType(typeof(IRequest<>))); |
| 102 | + |
| 103 | + var container = builder.Build(); |
| 104 | + |
| 105 | + var handlers = container.Resolve<IEnumerable<IHandler<IRequest<int>>>>().ToList(); |
| 106 | + Assert.Equal(2, handlers.Count); |
| 107 | + Assert.Contains(handlers, h => h is HandlerMappableFirst<int>); |
| 108 | + Assert.Contains(handlers, h => h is HandlerNonMappableFirst<int>); |
| 109 | + } |
| 110 | + |
| 111 | + [Fact] |
| 112 | + public void HandlerWithUnrelatedTypeParamIsNotResolved() |
| 113 | + { |
| 114 | + // Second case from #1464: GenericMismatchWithInterface<TParam> : IHandler<int> |
| 115 | + // registered as IHandler<>. The type parameter TParam cannot be inferred from the |
| 116 | + // service arguments (int), so Autofac must not bind this and the enumerable must |
| 117 | + // be empty. No exception should be thrown. |
| 118 | + var builder = new ContainerBuilder(); |
| 119 | + builder |
| 120 | + .RegisterGeneric(typeof(HandlerWithUnrelatedTypeParam<>)) |
| 121 | + .As(typeof(IHandler<>)); |
| 122 | + |
| 123 | + var container = builder.Build(); |
| 124 | + |
| 125 | + // Must not throw; must return no registrations. |
| 126 | + var handlers = container.Resolve<IEnumerable<IHandler<int>>>(); |
| 127 | + Assert.Empty(handlers); |
| 128 | + } |
| 129 | +} |
0 commit comments