-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
System.Text.Json source generator fails if [Required(ErrorMessage = "...")] attribute is present. #62937
Comments
Tagging subscribers to this area: @dotnet/area-system-text-json Issue DetailsDescriptionI tried to use the Json source generator with a class which has a [Required] attribute with an error message, but the generator succeeds only when the ErrorMessage is not specified. I tried it with other Validation attributes (StringLength, Range), and I got the same result. Reproduction StepsAdd the following two classes in a .NET 6.0 project, then try to build it. using System.ComponentModel.DataAnnotations;
using System.Text.Json.Serialization;
public class TestDto
{
[Required(ErrorMessage = "asdf")]
public string Name { get; set; }
}
[JsonSerializable(typeof(TestDto))]
public partial class Serializers : JsonSerializerContext
{
} Expected behaviorThe source generator should generate the properties and the build should succeed. Actual behaviorThe source generator and the build fails and I get an error in the Output window:
Regression?No response Known WorkaroundsRemove the ErrorMessage, ErrorMessageResourceName and ErrorMessageResourceType properties from the validation attributes. Configuration.NET 6.0.100 Other informationNo response
|
Can repro. This bug can affect lots of users and a bug fix should be considered for 6.0. |
This comment has been minimized.
This comment has been minimized.
Oh I see the bot has already created a backport PR. |
This issue did not surface for me until I used .NET SDK 6.0.2 (dotnet/core#7172).
|
Taking a look. |
I've just ran into this issue as well. I'm able to repro the issue in 6.0.1 as well as 6.0.2. Easy repro here, switching to 6.0.1 of STJ still reproduces the issue. https://github.com/JakeYallop/JsonSerializerContextInvalidOperationExceptionRepro public class MyClass
{
[Derived(BaseProperty = "")] //source generator does not compile
//[Derived(DerivedProperty = "", BaseProperty = "")] //does not compile
//[Derived] //compiles fine
//[Derived(DerivedProperty = "")] //compiles fine
public string Property { get; set; } = null!;
}
public abstract class BaseAttribute : Attribute
{
public string BaseProperty { get; set; } = null!;
}
[AttributeUsage(AttributeTargets.All)]
public class DerivedAttribute : BaseAttribute
{
public string DerivedProperty { get; set; } = null!;
}
[JsonSourceGenerationOptions]
[JsonSerializable(typeof(MyClass))]
public partial class JsonContext : JsonSerializerContext
{
} As a workaround for the short term, I've added an explicit reference to version 7.0.0-preview.2.22152.2 of STJ in my .NET 6 project which includes the fix made above (I usually just use the in-box version, rather than an explicit reference). Will this fix make it into a 6.0.x version of the STJ package in future? |
Moving to .NET 8 milestone. |
I'm not sure if this issue should still be open - it seems like this is fixed in .NET 7.0. |
@layomia According to #62937 (comment) this is a regression in servicing (although I couldn't pinpoint a specific change that might have introduced this). Should we consider backporting #63837 to .NET 6? cc @ericstj |
.net 7's new 'required' & init-only props are also unsupported; i.e. Is there something tracking this I can subscribe to? |
Yes, see #58770 |
@veertien I checked this particular scenario using sdk version 6.0.101
On my machine the compile error is reproducing with that configuration as well. I don't believe we're dealing with a servicing regression here, so I'm going to close this issue. .NET 6 impacted by this issue can try referencing the v7 System.Text.Json NuGet package to incorporate the fix. |
CS0534: |
@xlievo please create a new issue containing a minimal reproduction. Thanks! |
Description
I tried to use the Json source generator with a class which has a [Required] attribute with an error message, but the generator succeeds only when the ErrorMessage is not specified. I tried it with other Validation attributes (StringLength, Range), and I got the same result.
Reproduction Steps
Add the following two classes in a .NET 6.0 project, then try to build it.
Expected behavior
The source generator should generate the properties and the build should succeed.
Actual behavior
The source generator and the build fails and I get an error in the Output window:
Regression?
No response
Known Workarounds
Remove the ErrorMessage, ErrorMessageResourceName and ErrorMessageResourceType properties from the validation attributes.
Configuration
.NET 6.0.100
Microsoft Windows [Version 10.0.19043.1348]
x64
Other information
No response
The text was updated successfully, but these errors were encountered: