Skip to content

Extend JsonSourceGenerationOptionsAttribute to have feature parity with JsonSerializerOptions. - #88753

Merged
eiriktsarpalis merged 1 commit into
dotnet:mainfrom
eiriktsarpalis:extend-jsonsourcegenoptions
Jul 14, 2023
Merged

Extend JsonSourceGenerationOptionsAttribute to have feature parity with JsonSerializerOptions.#88753
eiriktsarpalis merged 1 commit into
dotnet:mainfrom
eiriktsarpalis:extend-jsonsourcegenoptions

Conversation

@eiriktsarpalis

@eiriktsarpaliseiriktsarpalis commented Jul 12, 2023

Copy link
Copy Markdown
Member

Fix#57321.

@ghost

Copy link
Copy Markdown

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.

@eiriktsarpaliseiriktsarpalis modified the milestones: 7.0.x, 8.0.0Jul 12, 2023
@ghost

Copy link
Copy Markdown

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:-

@eiriktsarpaliseiriktsarpalis added the NO-MERGE The PR is not ready for merge yet (see discussion for detailed reasons) label Jul 12, 2023
@eiriktsarpalis
eiriktsarpalisforce-pushed the extend-jsonsourcegenoptions branch from a93f460 to 2eea328CompareJuly 13, 2023 16:55
@eiriktsarpaliseiriktsarpalis removed the NO-MERGE The PR is not ready for merge yet (see discussion for detailed reasons) label Jul 13, 2023
@eiriktsarpalis

eiriktsarpalis commented Jul 13, 2023

Copy link
Copy Markdown
MemberAuthor

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

};
""");

return policyName != null

@tarekghtarekghJul 14, 2023

Copy link
Copy Markdown
Member

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
Copy Markdown
MemberAuthor

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
Copy Markdown
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!;

@tarekghtarekghJul 14, 2023

Copy link
Copy Markdown
Member

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
Copy Markdown
MemberAuthor

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
Copy Markdown
MemberAuthor

Build failures not related to the change.

@eiriktsarpalis
eiriktsarpalis merged commit acedeb2 into dotnet:mainJul 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
Copy Markdown
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
Copy Markdown
MemberAuthor

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
Copy Markdown
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
Copy Markdown
MemberAuthor

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
Copy Markdown
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
Copy Markdown
MemberAuthor

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
Copy Markdown
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
Copy Markdown
MemberAuthor

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.

@ghostghost locked as resolved and limited conversation to collaborators Sep 3, 2023
Sign up for freeto 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

@eiriktsarpalis@MaceWindu@stephentoub@tarekgh