Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: ChilliCream/graphql-platform
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 11.3.6
Choose a base ref
...
head repository: ChilliCream/graphql-platform
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 11.3.7
Choose a head ref
  • 1 commit
  • 2 files changed
  • 1 contributor

Commits on Sep 6, 2021

  1. Verified

    This commit was signed with the committer’s verified signature. The key has expired.
    Mic92 Jörg Thalheim
    Copy the full SHA
    ced1a64 View commit details
Original file line number Diff line number Diff line change
@@ -55,7 +55,7 @@ private static bool Any(Type enumerableType, object enumerable)

MethodInfo info = _enumerableTypeInfo
.DeclaredMethods
.First(m => m.Name == _methodNameAny && m.IsStatic)
.First(m => m.Name == _methodNameAny && m.IsStatic && m.GetParameters().Length == 1)
.MakeGenericMethod(genericArgumentType);

return (bool)info.Invoke(null, new[] { enumerable })!;
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Microsoft.Extensions.DependencyInjection;
using Xunit;

namespace HotChocolate.Utilities
{
public class CombinedServiceProviderTests
{
[Fact]
public void GetServiceWithoutError()
{
/***
Note
==========
This code is adapted from `HotChocolate.SchemaBuilder.Setup.InitializeInterceptors<T>`,
which was the next relevant call down "down the stack" in the error traces which
motivate changes to the subject-under-test (i.e. CombinedServiceProvider).
***/
IServiceProvider stringServices = new DictionaryServiceProvider(
(typeof(IEnumerable<string>), new List<string>(new[] { "one", "two" }))
);

IServiceProvider numberServices = new DictionaryServiceProvider(
(typeof(IEnumerable<int>), new List<int>(new[] { 1, 2, 3, 4, 5 }))
);

IServiceProvider services = new CombinedServiceProvider(stringServices, numberServices);

switch (services.GetService<IEnumerable<int>>())
{
case null:
throw new Exception("Could not locate service!");

case var target:
Assert.Equal(15, target.Sum());
break;
}
}

[Fact(Skip = "For demonstration purposed only")]
public void ThrowRefelctionError()
{
var actual = AnyFailure(typeof(IEnumerable<int>), Array.Empty<int>());
Assert.False(actual);
}

/// This method simulates a rare -- but possible -- edge case in the
/// implementation of `CombinedServiceProvider.Any()`. Specifically,
/// there are occasions wherein the _wrong_ overload of `Enumerable.Any()`
/// is selected, which results in run-time exception. This method will
/// fail when run. It is intended for instructional purposes only.
private bool AnyFailure(Type enumerableType, object enumerable)
{
Type genericArgumentType = enumerableType
.GetTypeInfo()
.GenericTypeArguments[0];

MethodInfo info = typeof(Enumerable)
.GetTypeInfo()
.DeclaredMethods
.First(m =>
m.IsStatic &&
m.Name == nameof(Enumerable.Any) &&
m.GetParameters().Length > 1)
/***
Note
==========
There are two overloads of `Any` (arity 1 and arity 2).
We only want the one which takes a single argument (arity 1).
Explicitly selecting an overload with different arity will
cause a TargetParameterCountException error when invoked (below).
***/
.MakeGenericMethod(genericArgumentType);

return (bool)info.Invoke(null, new[] { enumerable });
}
}
}