Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit test projects with JsonSerializerIsReflectionEnabledByDefault=false cannot use WebApplicationFactory #55586

Open
1 task done
dougclutter opened this issue May 7, 2024 · 0 comments · May be fixed by #55595
Open
1 task done
Labels
area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions

Comments

@dougclutter
Copy link

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

If an xunit Test Project includes the following in the csproj file:

  <PropertyGroup>
    <JsonSerializerIsReflectionEnabledByDefault>false</JsonSerializerIsReflectionEnabledByDefault>
  </PropertyGroup>

then unit tests which use a WebApplicationFactory will fail with an InvalidOperationException with a stack similar to this:

  Message: 
System.InvalidOperationException : Reflection-based serialization has been disabled for this application. Either use the source generator APIs or explicitly configure the 'JsonSerializerOptions.TypeInfoResolver' property.

  Stack Trace: 
ThrowHelper.ThrowInvalidOperationException_JsonSerializerIsReflectionDisabled()
JsonSerializerOptions.ConfigureForJsonSerializer()
JsonSerializer.GetTypeInfo(JsonSerializerOptions options, Type inputType)
JsonSerializer.GetTypeInfo[T](JsonSerializerOptions options)
JsonSerializer.Deserialize[TValue](ReadOnlySpan`1 utf8Json, JsonSerializerOptions options)
WebApplicationFactory`1.GetContentRootFromFile(String file)
WebApplicationFactory`1.SetContentRoot(IWebHostBuilder builder)
WebApplicationFactory`1.<ConfigureHostBuilder>b__22_0(IWebHostBuilder webHostBuilder)
GenericHostWebHostBuilderExtensions.ConfigureWebHost(IHostBuilder builder, Func`3 createWebHostBuilder, Action`1 configure, Action`1 configureWebHostBuilder)
WebApplicationFactory`1.ConfigureHostBuilder(IHostBuilder hostBuilder)
WebApplicationFactory`1.EnsureServer()
WebApplicationFactory`1.CreateDefaultClient(DelegatingHandler[] handlers)
WebApplicationFactory`1.CreateDefaultClient(Uri baseAddress, DelegatingHandler[] handlers)
WebApplicationFactory`1.CreateClient(WebApplicationFactoryClientOptions options)
WebApplicationFactory`1.CreateClient()
ProgramTests.RequestForJwksWorks() line 10
<>c.<ThrowAsync>b__128_0(Object state)

I believe the root cause is this line in the WebApplicationFactory.GetContentRootFromFile:

var data = JsonSerializer.Deserialize<IDictionary<string, string>>(File.ReadAllBytes(file))!;

Expected Behavior

WebApplicationFactory should work.

Steps To Reproduce

A complete repo is available at https://github.com/dougclutter/WebApplicationFactoryJsonSerialization

Exceptions (if any)

InvalidOperationException

.NET Version

8.0.204

Anything else?

No response

martincostello added a commit to martincostello/aspnetcore that referenced this issue May 8, 2024
Use JSON source generator to deserialize dictionary.
Resolves dotnet#55586.
@mkArtakMSFT mkArtakMSFT added area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions and removed area-web-frameworks labels May 8, 2024
martincostello added a commit to martincostello/aspnetcore that referenced this issue May 16, 2024
Use JSON source generator to deserialize dictionary.
Resolves dotnet#55586.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants