Skip to content

Fix DefaultProblemDetailsWriter polymorphism #45906

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

Merged
merged 15 commits into from
Jan 12, 2023

Conversation

brunolins16
Copy link
Member

@brunolins16 brunolins16 commented Jan 6, 2023

Fix #45680

@ghost ghost added the area-runtime label Jan 6, 2023
@brunolins16 brunolins16 added old-area-web-frameworks-do-not-use *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels and removed area-runtime labels Jan 6, 2023
@brunolins16 brunolins16 changed the title Fix DefaultProblemDetailsWrite polymorphism Fix DefaultProblemDetailsWriter polymorphism Jan 9, 2023
@brunolins16 brunolins16 marked this pull request as ready for review January 10, 2023 17:55
@brunolins16 brunolins16 requested review from JamesNK and a team January 10, 2023 17:56
@brunolins16 brunolins16 assigned davidfowl and unassigned davidfowl Jan 11, 2023
@brunolins16 brunolins16 requested a review from davidfowl January 11, 2023 18:10
{
_options = options.Value;
_serializerOptions = new JsonSerializerOptions(jsonOptions.Value.SerializerOptions);

if (_serializerOptions.TypeInfoResolver is not null)
Copy link
Member

Choose a reason for hiding this comment

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

What happens if TypeInfoResolver is 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.

In non-aot scenario it will use the reflection-based resolver, so, we don't really need to include our internal context. In AOT scenario, unless your application only serializes Problem Details, it should not be null and if it is null not adding our internal context seems might be bad but probably it will fail elsewhere first. Make sense?

Copy link
Member

@JamesNK JamesNK Jan 12, 2023

Choose a reason for hiding this comment

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

But it seems that if TypeInfoResolver is null and you're in AOT, then this type can't work.

Should there be an error if IsDynamicCodeSupported is false (which means context is required) and TypeInfoResolver is null?

Either way, I think a comment here would be useful for future devs.

Copy link
Member Author

Choose a reason for hiding this comment

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

Interesting idea. Do you think error is enough or should we set our context in this case? I believe setting our context could hide the configuration issue and make it harder to understand.

Copy link
Member

Choose a reason for hiding this comment

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

I don't know. If the problem is configuration, then IMO throwing here with a helpful error message is better than pretending things are ok and then later failing when serialization happens with an obscure error message.

Copy link
Member Author

Choose a reason for hiding this comment

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

@JamesNK I have been thinking about this and maybe we shouldn't throw during the configuration because it could be too earlier to make sure a problem details will not be able to be serializer. I am saying that because in a situation where the user added a custom IProblemDetailsWriter that could serializer a ProblemDetails in a different format (eg, XML) throwing because the JsonContext was not configured correctly might be wrong. What do you think?

Copy link
Member

Choose a reason for hiding this comment

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

I agree with @brunolins16, I don't think we should throw because the TypeInfoResolver is null. That shouldn't come from this code. Feels like something more top level and central as a whole. Its part of the "how does the user know they need to set a context" in the first place.

@brunolins16 brunolins16 requested a review from halter73 January 12, 2023 18:12
[JsonSerializable(typeof(HttpValidationProblemDetails))]
// Additional values are specified on JsonSerializerContext to support some values for extensions.
// For example, the DeveloperExceptionMiddleware serializes its complex type to JsonElement, which problem details then needs to serialize.
[JsonSerializable(typeof(JsonElement))]
Copy link
Member

@davidfowl davidfowl Jan 12, 2023

Choose a reason for hiding this comment

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

This should be removed eventually. Can you file an issue for this?

@brunolins16 brunolins16 merged commit 334da01 into dotnet:main Jan 12, 2023
@brunolins16 brunolins16 deleted the brunolins16/issues/45680-2 branch January 12, 2023 21:37
@ghost ghost added this to the 8.0-preview1 milestone Jan 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
old-area-web-frameworks-do-not-use *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

DefaultProblemDetailsWriter does not serialize HttpValidationProblemDetails.Errors
4 participants