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

Set default JSON type resolver if not set #49393

Merged
merged 4 commits into from
Jul 14, 2023

Conversation

captainsafia
Copy link
Member

Closes #49360

@dotnet-issue-labeler dotnet-issue-labeler bot added the needs-area-label Used by the dotnet-issue-labeler to label those issues which couldn't be triaged automatically label Jul 14, 2023
@captainsafia captainsafia added area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc and removed needs-area-label Used by the dotnet-issue-labeler to label those issues which couldn't be triaged automatically labels Jul 14, 2023
@captainsafia captainsafia force-pushed the safia/set-typeinfo-resolver branch from bc3077d to f47b5f4 Compare July 14, 2023 01:01
@eerhardt
Copy link
Member

eerhardt commented Jul 14, 2023

FYI - If you wanted to write some tests for this, you could do them like this:

[ConditionalFact]
[RemoteExecutionSupported]
public void DefaultSerializerOptions_SetsTypeInfoResolverToDefault_WhenJsonIsReflectionEnabledByDefaultTrue()
{
var options = new RemoteInvokeOptions();
options.RuntimeConfigurationOptions.Add("System.Text.Json.JsonSerializer.IsReflectionEnabledByDefault", true.ToString());
using var remoteHandle = RemoteExecutor.Invoke(static () =>
{
// Arrange
var options = new JsonOptions().JsonSerializerOptions;
// Assert
Assert.NotNull(options.TypeInfoResolver);
Assert.IsType<DefaultJsonTypeInfoResolver>(options.TypeInfoResolver);
}, options);

You would just be setting the bool to false.

@@ -18,6 +18,7 @@ public static bool ShouldUseWith(this JsonTypeInfo jsonTypeInfo, [NotNullWhen(fa

public static JsonTypeInfo GetReadOnlyTypeInfo(this JsonSerializerOptions options, Type type)
{
options.TypeInfoResolver ??= new EmptyJsonTypeInfoResolver();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. You can avoid creating a custom resolver if you just use JsonTypeInfoResolver.Combine() with no arguments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates
Projects
None yet
Development

Successfully merging this pull request may close these issues.

MVC apps no longer work with PublishTrimmed
3 participants