graphql-platform icon indicating copy to clipboard operation
graphql-platform copied to clipboard

ScalarSerializer is not found by ISerialzerResolver

Open con-soa opened this issue 3 years ago • 3 comments

Is there an existing issue for this?

  • [X] I have searched the existing issues

Describe the bug

Custom ScalarSerializer not found when when initializing client.

Steps to reproduce

  1. Given a schema like this:
type Outage {
  id: String!
  fasoId: [String!]!
  name: String
  category: String
  product: String
  startTime: AWSDateTime
  endTime: AWSDateTime
  expectedEndTime: AWSDateTime
}
scalar AWSDateTime
  1. Add the following to schema.extensions.graphql:
extend scalar AWSDateTime
	@serializationType(name: "global::System.String")
	@runtimeType(name: "global::System.DateTime")
  1. Add a ScalarSerializer:
public class StrawberryShakeDateTimeSerializer : ScalarSerializer<string, DateTime>
    {
        public StrawberryShakeDateTimeSerializer() : base("AWSDateTime")
        {
        }

        public override DateTime Parse(string serializedValue)
        {
            throw new NotImplementedException();
        }

        protected override string Format(DateTime runtimeValue)
        {
            throw new NotImplementedException();
        }
    }
  1. Check one of the relevant Initalize() methods using AWSDateTime. It should look something like:
        public void Initialize(global::StrawberryShake.Serialization.ISerializerResolver serializerResolver)
        {
            _stringFormatter = serializerResolver.GetInputValueFormatter("String");
            _aWSDateTimeFormatter = serializerResolver.GetInputValueFormatter("AWSDateTime");
        }
  1. Run the project, and try to initialize the client. You will now get the following exception:
My.Test.Agent.Microcore.TestIntegration.Controller.TestIntegrationController: Error: TestMethod Exception: System.ArgumentException: There is no formatter registered the specified type.
   at StrawberryShake.Serialization.SerializerResolver.GetInputValueFormatter(String typeName)
   at My.Test.Agent.Microcore.TestIntegration.Client.GetTicketInputInputValueFormatter.Initialize(ISerializerResolver serializerResolver) in C:\Users\username\projects\test-integration-ms\TestIntegrationSvc\StrawberryShake.CodeGeneration.CSharp.Analyzers\StrawberryShake.CodeGeneration.CSharp.Analyzers.CSharpClientGenerator\ChipperClient.StrawberryShake.cs:line 14474
   at StrawberryShake.Serialization.SerializerResolver..ctor(IEnumerable`1 serializers)
   at Microsoft.Extensions.DependencyInjection.ChipperClientServiceCollectionExtensions.<>c__DisplayClass1_0.<ConfigureClientDefault>b__2(IServiceProvider sp) in C:\Users\username\projects\test-integration-ms\TestIntegrationSvc\StrawberryShake.CodeGeneration.CSharp.Analyzers\StrawberryShake.CodeGeneration.CSharp.Analyzers.CSharpClientGenerator\ChipperClient.StrawberryShake.cs:line 100
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitRootCache(ServiceCallSite callSite, RuntimeResolverContext context)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, RuntimeResolverContext context)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitRootCache(ServiceCallSite callSite, RuntimeResolverContext context)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.Resolve(ServiceCallSite callSite, ServiceProviderEngineScope scope)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.CreateServiceAccessor(Type serviceType)
   at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType, ServiceProviderEngineScope serviceProviderEngineScope)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.GetService(Type serviceType)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ChipperClientServiceCollectionExtensions.<>c.<ConfigureClientDefault>b__1_10(IServiceProvider sp) in C:\Users\username\projects\test-integration-ms\TestIntegrationSvc\StrawberryShake.CodeGeneration.CSharp.Analyzers\StrawberryShake.CodeGeneration.CSharp.Analyzers.CSharpClientGenerator\ChipperClient.StrawberryShake.cs:line 114
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitRootCache(ServiceCallSite callSite, RuntimeResolverContext context)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, RuntimeResolverContext context)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitRootCache(ServiceCallSite callSite, RuntimeResolverContext context)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.Resolve(ServiceCallSite callSite, ServiceProviderEngineScope scope)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.CreateServiceAccessor(Type serviceType)
   at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType, ServiceProviderEngineScope serviceProviderEngineScope)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.GetService(Type serviceType)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ChipperClientServiceCollectionExtensions.<>c.<ConfigureClientDefault>b__1_19(IServiceProvider sp) in C:\Users\username\projects\test-integration-ms\TestIntegrationSvc\StrawberryShake.CodeGeneration.CSharp.Analyzers\StrawberryShake.CodeGeneration.CSharp.Analyzers.CSharpClientGenerator\ChipperClient.StrawberryShake.cs:line 131
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitRootCache(ServiceCallSite callSite, RuntimeResolverContext context)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.Resolve(ServiceCallSite callSite, ServiceProviderEngineScope scope)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.CreateServiceAccessor(Type serviceType)
   at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType, ServiceProviderEngineScope serviceProviderEngineScope)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType)
   at Microsoft.Extensions.DependencyInjection.ChipperClientServiceCollectionExtensions.ClientServiceProvider.GetService(Type serviceType) in C:\Users\username\projects\test-integration-ms\TestIntegrationSvc\StrawberryShake.CodeGeneration.CSharp.Analyzers\StrawberryShake.CodeGeneration.CSharp.Analyzers.CSharpClientGenerator\ChipperClient.StrawberryShake.cs:line 145
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ChipperClientServiceCollectionExtensions.<>c.<AddChipperClient>b__0_7(IServiceProvider sp) in C:\Users\username\projects\test-integration-ms\TestIntegrationSvc\StrawberryShake.CodeGeneration.CSharp.Analyzers\StrawberryShake.CodeGeneration.CSharp.Analyzers.CSharpClientGenerator\ChipperClient.StrawberryShake.cs:line 23
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitRootCache(ServiceCallSite callSite, RuntimeResolverContext context)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.Resolve(ServiceCallSite callSite, ServiceProviderEngineScope scope)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.CreateServiceAccessor(Type serviceType)
   at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType, ServiceProviderEngineScope serviceProviderEngineScope)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)
   at My.Test.Agent.Microcore.TestIntegration.ClientFactory.RefreshClient(JwtSecurityToken jwtSecurityToken) in C:\Users\username\projects\test-integration-ms\TestIntegrationSvc\TestIntegration\ClientFactory.cs:line 122
   at My.Test.Agent.Microcore.TestIntegration.ClientFactory.Client() in C:\Users\username\projects\test-integration-ms\TestIntegrationSvc\TestIntegration\ClientFactory.cs:line 43
   at My.Test.Agent.Microcore.TestIntegration.Facade.TestIntegrationFacade.TestMethod_GraphQL(String id, List`1 status) in C:\Users\username\projects\test-integration-ms\TestIntegrationSvc\TestIntegration\Facade\TestIntegrationFacade.cs:line 135
   at My.Test.Agent.Microcore.TestIntegration.Controller.TestIntegrationController.TestMethod_GraphQL(String incidentId, List`1 status) in C:\Users\username\projects\test-integration-ms\TestIntegrationSvc\TestIntegration\Controller\TestIntegrationController.cs:line 131

Debugging confirms that the ScalarSerializer is not part of the list of Serializers.

Relevant log output

No response

Additional Context?

No response

Product

Strawberry Shake

Version

12.11.1

con-soa avatar Jul 05 '22 09:07 con-soa

Did you register the serializer with AddSerializer< StrawberryShakeDateTimeSerializer >()?

PascalSenn avatar Jul 05 '22 18:07 PascalSenn

Hi @PascalSenn ,

I must have missed that, sorry - I simply missed that part of the Scalar page, as it is only mentioned under "Advanced Example" and not referenced at all before.

Sorry - will try that, and close this.

con-soa avatar Jul 06 '22 07:07 con-soa

Reopening, as adding the serializer in Startup.cs didn't work.

I introduced the DateTime serializer in Startup.cs:ConfigureServices:

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddSerializer<StrawberryShakeDateTimeSerializer>();
            ...
        }

My serializer:

    public class StrawberryShakeDateTimeSerializer : ScalarSerializer<string, DateTime>
    {
        public StrawberryShakeDateTimeSerializer() : base("AWSDateTime")
        {
        }

        public override DateTime Parse(string serializedValue)
        {
            throw new NotImplementedException();
        }

        protected override string Format(DateTime runtimeValue)
        {
            throw new NotImplementedException();
        }
    }

Still gives "There is no formatter registered the specified type.".

Looking at the debugger, the ISerializerResolver has about 50 serializers, but none of them are for AWSDateTime.

Any ideas what I am missing, @PascalSenn ?

con-soa avatar Jul 06 '22 10:07 con-soa