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

Extend JsonSourceGenerationOptionsAttribute to have feature parity with JsonSerializerOptions. #88753

Merged

Conversation

eiriktsarpalis
Copy link
Member

@eiriktsarpalis eiriktsarpalis commented Jul 12, 2023

Fix #57321.

@dotnet-issue-labeler
Copy link

Note regarding the new-api-needs-documentation label:

This serves as a reminder for when your PR is modifying a ref *.cs file and adding/modifying public APIs, please make sure the API implementation in the src *.cs file is documented with triple slash comments, so the PR reviewers can sign off that change.

@eiriktsarpalis eiriktsarpalis modified the milestones: 7.0.x, 8.0.0 Jul 12, 2023
@ghost
Copy link

ghost commented Jul 12, 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

Fix #57321. Pending API approval.

Author: eiriktsarpalis
Assignees: eiriktsarpalis
Labels:

area-System.Text.Json, new-api-needs-documentation

Milestone: -

@eiriktsarpalis eiriktsarpalis added the NO-MERGE The PR is not ready for merge yet (see discussion for detailed reasons) label Jul 12, 2023
@eiriktsarpalis eiriktsarpalis force-pushed the extend-jsonsourcegenoptions branch from a93f460 to 2eea328 Compare July 13, 2023 16:55
@eiriktsarpalis eiriktsarpalis requested a review from layomia July 13, 2023 16:55
@eiriktsarpalis eiriktsarpalis removed the NO-MERGE The PR is not ready for merge yet (see discussion for detailed reasons) label Jul 13, 2023
@eiriktsarpalis
Copy link
Member Author

eiriktsarpalis commented Jul 13, 2023

API has been approved as proposed and this PR is ready for review.

};
""");

return policyName != null
Copy link
Member

@tarekgh tarekgh Jul 14, 2023

Choose a reason for hiding this comment

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

policyName != null

nit: is not null?

Copy link
Member Author

Choose a reason for hiding this comment

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

is not null is usually better but we haven't been consistent in its use. In this case it's fine to use != since the particular type doesn't define any custom operators.

{
var converterType = (ITypeSymbol?)element.Value;
TypeRef? typeRef = GetConverterTypeFromAttribute(contextType, converterType, contextType, attributeData);
if (typeRef != null)
Copy link
Member

Choose a reason for hiding this comment

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

!= null

ditto

break;

case nameof(JsonSourceGenerationOptionsAttribute.MaxDepth):
maxDepth = (int)namedArg.Value.Value!;
Copy link
Member

@tarekgh tarekgh Jul 14, 2023

Choose a reason for hiding this comment

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

(int)n

what happen if for any reason such cast or any code throw inside this method? just asking about, do we catch it and produce diagnostics?

Copy link
Member Author

Choose a reason for hiding this comment

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

Unhandled exceptions will get caught by the compiler and surfaced as a warning along the lines of "the source generator threw an exception and will not generate any code".

@eiriktsarpalis
Copy link
Member Author

Build failures not related to the change.

@eiriktsarpalis eiriktsarpalis merged commit acedeb2 into dotnet:main Jul 14, 2023
@eiriktsarpalis eiriktsarpalis deleted the extend-jsonsourcegenoptions branch July 14, 2023 16:39
Comment on lines +55 to +58
/// <summary>
/// The default buffer size in bytes used when creating temporary buffers.
/// </summary>
public int DefaultBufferSize { get; set; }
Copy link
Member

Choose a reason for hiding this comment

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

By default this is going to be 0. Doesn't that differ from JsonSerializerOptions.DefaultBufferSize, which defaults to a positive value and throws an exception if it's set to 0?

Copy link
Member Author

Choose a reason for hiding this comment

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

Because the attribute is only being read by Roslyn, the value of the property is only being consulted if explicitly set by the user in the attribute declaration. I briefly considered making this and other properties nullable, however that would be a departure from convention already used in properties shipped in this attribute, so went with consistency instead. The other alternative would be to explicitly set the properties to equal the JsonSerializerOptions default, however that would necessitate a bit of duplication or refactoring without providing any clear benefit.

Copy link
Member

Choose a reason for hiding this comment

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

Then shouldn't the XML comment call out what a value of 0 means, as is done below for MaxDepth?

Copy link
Member Author

Choose a reason for hiding this comment

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

I would probably opt for removing that reference in MaxDepth, making all of them deliberately ambiguous and just have them point to the corresponding JsonSerializerOptions properties.

Copy link
Member

Choose a reason for hiding this comment

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

We need to call out somewhere in the docs what it means to not provide a value for these.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'll follow up with a PR once we conclude P7 work.

public Type[]? Converters { get; set; }

/// <summary>
/// The default buffer size in bytes used when creating temporary buffers.
Copy link
Member

Choose a reason for hiding this comment

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

These comments on properties inconsistently start or don't start with "Gets or sets..."

Copy link
Member Author

Choose a reason for hiding this comment

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

I mostly just copied and pasted the wording in the corresponding JsonSerializerOptions properties. I can submit a follow-up PR that improves consistency between properties in this attribute.

@ghost ghost locked as resolved and limited conversation to collaborators Sep 3, 2023
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.

Improve feature parity between JsonSerializerOptions and JsonSourceGenerationOptionsAttribute
4 participants