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

JsonSerializerOptionsNoTypeInfoResolverSpecified after upgrading from .NET 8 Preview 6 to 7 (release mode only) #90395

Closed
DM-98 opened this issue Aug 11, 2023 · 4 comments

Comments

@DM-98
Copy link

DM-98 commented Aug 11, 2023

Description

App works fine in debug mode for both Preview 6 and 7.
App crashes in release mode for just Preview 7: JsonSerializerOptionsNoTypeInfoResolverSpecified.

My app worked fine in .NET 8 Preview 6:
DebugMode

After upgrading to .NET 8 Preview 7:
ReleaseMode

Seems like a JsonSerializer bug in the .NET Preview 7 install package. I tried these:

  • Keeping same codebase, but changing from 6 to 7 and back to 6 to confirm the issue
  • Changing JsonSerializer to JsonConvert, issue persists
  • Creating my own JsonSerializerContext class using the attributes and applying it as my TypeInfoResolver for the JsonSerializerOptions: [JsonSourceGenerationOptions] & [JsonSerializable(typeof(MySerializableType))], then the error changes to NoMetadataSpecified or something like that
  • Removed JsonSerializerOptions entirely in my code, issue persists
  • Used PostAsync instead of PostAsJsonAsync, issue persists

Steps to Reproduce

  1. Create an API with a HttpPost endpoint that accepts a complex object in body
  2. Create a MAUI app that runs on .NET 8 Preview 7 (dotnet workload install maui)
  3. Call the API endpoint with a Post request using PostAsJsonAsync method
  4. Run the app in Release mode using .NET 8 Preview 7

Link to public reproduction project repository

https://github.com/DM-98/MauiApp1

Version with bug

8.0.0-preview.7.8842

Is this a regression from previous behavior?

Yes, this used to work in .NET MAUI

Last version that worked well

8.0.0-preview.6.8686

Affected platforms

Android, I was not able test on other platforms

Affected platform versions

Android 13, not tested on other.

Did you find any workaround?

Change back to previous SDK: .NET 8 Preview 6.
Edit (workaround by eiriktsarpalis):

Appears to be a duplicate of dotnet/maui#16038. Setting

<JsonSerializerIsReflectionEnabledByDefault>true</JsonSerializerIsReflectionEnabledByDefault>

in your project should get you back the original behavior.

Relevant log output

No response

@mattleibow mattleibow transferred this issue from dotnet/maui Aug 11, 2023
@ghost ghost added the untriaged New issue has not been triaged by the area owner label Aug 11, 2023
@ghost
Copy link

ghost commented Aug 11, 2023

Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis
See info in area-owners.md if you want to be subscribed.

Issue Details

Description

App works fine in debug mode for both Preview 6 and 7.
App crashes in release mode for just Preview 7: JsonSerializerOptionsNoTypeInfoResolverSpecified.

Codebase:

private readonly HttpClient httpClient;
private readonly JsonSerializerOptions jsonSerializerOptions;

public HttpClientDocumentService(HttpClient httpClient)
{
	this.httpClient = httpClient;
	jsonSerializerOptions = new JsonSerializerOptions(JsonSerializerDefaults.Web);
}

public async Task<ResponseDTO<IReadOnlyCollection<DocumentListDTO>>> GetDocumentListAsync(DocumentFilter documentFilter, CancellationToken cancellationToken = default)
{
	try
	{
		HttpResponseMessage response = await httpClient.PostAsJsonAsync("Document/GetDocumentList", documentFilter, jsonSerializerOptions, cancellationToken);

		return await response.Content.ReadFromJsonAsync<ResponseDTO<IReadOnlyCollection<DocumentListDTO>>>(jsonSerializerOptions, cancellationToken) ?? new ResponseDTO<IReadOnlyCollection<DocumentListDTO>>(false, "Der skete en serverfejl, kontakt os hvis problemet fortsætter.");
	}
	catch (OperationCanceledException ex)
	{
		return new ResponseDTO<IReadOnlyCollection<DocumentListDTO>>(false, "Din anmodning blev annulleret.", ErrorType.General_OperationCanceled, ex.Message, ex.InnerException?.Message);
	}
	catch (Exception ex)
	{
		return new ResponseDTO<IReadOnlyCollection<DocumentListDTO>>(false, "Der opstod en fejl ved din behandling - prøv igen eller kontakt os.", ErrorType.General_Unhandled, ex.Message, ex.InnerException?.Message);
	}
}

My app worked fine in .NET 8 Preview 6:
debug

After upgrading to .NET 8 Preview 7:
release

Seems like a JsonSerializer bug in the .NET Preview 7 install package. I tried these:

  • Keeping same codebase, but changing from 6 to 7 and back to 6 to confirm the issue
  • Changing JsonSerializer to JsonConvert, issue persists
  • Creating my own JsonSerializerContext class using the attributes and applying it as my TypeInfoResolver for the JsonSerializerOptions: [JsonSourceGenerationOptions] & [JsonSerializable(typeof(MySerializableType))], then the error changes to NoMetadataSpecified or something like that
  • Removed JsonSerializerOptions entirely in my code, issue persists
  • Used PostAsync instead of PostAsJsonAsync, issue persists

Steps to Reproduce

  1. Create an API with a HttpPost endpoint that accepts a complex object in body
  2. Create a MAUI app that runs on .NET 8 Preview 7 (dotnet workload install maui)
  3. Call the API endpoint with a Post request using PostAsJsonAsync method
  4. Run the app in Release mode using .NET 8 Preview 7

Link to public reproduction project repository

No response

Version with bug

8.0.0-preview.7.8842

Is this a regression from previous behavior?

Yes, this used to work in .NET MAUI

Last version that worked well

8.0.0-preview.6.8686

Affected platforms

Android, I was not able test on other platforms

Affected platform versions

Android 13, not tested on other.

Did you find any workaround?

Change back to previous SDK: .NET 8 Preview 6.

Relevant log output

No response

Author: DM-98
Assignees: -
Labels:

area-System.Text.Json

Milestone: -

@eiriktsarpalis
Copy link
Member

eiriktsarpalis commented Aug 11, 2023

Appears to be a duplicate of dotnet/maui#16038. Setting

<JsonSerializerIsReflectionEnabledByDefault>true</JsonSerializerIsReflectionEnabledByDefault>

in your project should get you back the original behavior.

@DM-98
Copy link
Author

DM-98 commented Aug 11, 2023

@eiriktsarpalis That worked, thanks. When will this be fixed, and after fix can I remove that line? in .NET 8 RC1?

@eiriktsarpalis
Copy link
Member

@DM-98 I would recommend subscribing to dotnet/maui#16038 for updates on the issue.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants