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

Emit diagnostics & exceptions for sourcegen handling init-only properties & JsonInclude attributes #58993

Merged

Conversation

eiriktsarpalis
Copy link
Member

  • Emit diagnostic warnings when sourcegen is handling init-only properties or inacessible JsonInclude members.
  • Throw a runtime exception instead of ignoring the above members.
  • Add localizations for runtime exception messages.

Contributes to #58770.
Fixes #58292.

Should be backported to release/6.0

@ghost
Copy link

ghost commented Sep 11, 2021

Tagging subscribers to this area: @eiriktsarpalis, @layomia
See info in area-owners.md if you want to be subscribed.

Issue Details
  • Emit diagnostic warnings when sourcegen is handling init-only properties or inacessible JsonInclude members.
  • Throw a runtime exception instead of ignoring the above members.
  • Add localizations for runtime exception messages.

Contributes to #58770.
Fixes #58292.

Should be backported to release/6.0

Author: eiriktsarpalis
Assignees: -
Labels:

area-System.Text.Json

Milestone: 6.0.0

Copy link
Contributor

@layomia layomia left a comment

Choose a reason for hiding this comment

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

LGTM


// ConverterForInt32 throws this exception.
await Assert.ThrowsAsync<NotImplementedException>(async () => await JsonSerializerWrapperForString.SerializeWrapper(obj, options));
// JsonInclude on private getters not supported.
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we test that there are no exceptions (silent ignore) when [JsonInclude] isn't on the property?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah I think we do. Here's an example:

public async Task NonPublicMembersAreNotIncluded()

@eiriktsarpalis
Copy link
Member Author

Test failures related to #58927

@eiriktsarpalis eiriktsarpalis merged commit e62c4fe into dotnet:main Sep 13, 2021
@eiriktsarpalis eiriktsarpalis deleted the sourcegen-warnon-initonlyprops branch September 13, 2021 12:28
@eiriktsarpalis
Copy link
Member Author

/backport to release/6.0

@github-actions
Copy link
Contributor

@@ -324,7 +324,7 @@ private string GenerateForTypeWithUnknownConverter(TypeGenerationSpec typeMetada
}}
else
{{
throw new {InvalidOperationExceptionTypeRef}($""The converter '{{converter.GetType()}}' is not compatible with the type '{{typeToConvert}}'."");
Copy link
Member

Choose a reason for hiding this comment

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

With this, we are localizing run-time messages in the generated code, even though we don't localize other run-time exceptions thrown from the runtime AFAIK. Do other generators do this?

My previous thoughts on this we that we shouldn't localize run-time exceptions since we don't do this elsewhere; we should only localize messages shown within VS as errors\warnings. If \ when we decide to localize messages in the runtime, then I think that would be the best time to do this for generated code. Localizing runtime messages may not always be desired -- it does make it harder to search for the message, and increases download size.

Also we generate the message with the current VS locale. Is that what developers want? They may want the message of the current locale that is running and not the locale that it was developed with.

@ghost ghost locked as resolved and limited conversation to collaborators Nov 3, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

System.Text.Json source gen containing non-localized error messages
3 participants