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

System.Text.Json source generator fails if [Required(ErrorMessage = "...")] attribute is present. #62937

Closed
Kloizdena opened this issue Dec 17, 2021 · 15 comments · Fixed by #63837
Assignees
Milestone

Comments

@Kloizdena
Copy link
Contributor

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.

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

Build started...
1>------ Build started: Project: SystemTextJsonTest, Configuration: Debug Any CPU ------
1>CSC : warning CS8785: Generator 'JsonSourceGenerator' failed to generate source. It will not contribute to the output and compilation errors may occur as a result. Exception was of type 'InvalidOperationException' with message 'Sequence contains no elements'
1>D:\src\SystemTextJsonTest\SystemTextJsonTest\TestDto.cs(11,22,11,33): error CS0534: 'Serializers' does not implement inherited abstract member 'JsonSerializerContext.GetTypeInfo(Type)'
1>D:\src\SystemTextJsonTest\SystemTextJsonTest\TestDto.cs(11,22,11,33): error CS0534: 'Serializers' does not implement inherited abstract member 'JsonSerializerContext.GeneratedSerializerOptions.get'
1>Done building project "SystemTextJsonTest.csproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

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

@dotnet-issue-labeler dotnet-issue-labeler bot added area-System.Text.Json untriaged New issue has not been triaged by the area owner labels Dec 17, 2021
@ghost
Copy link

ghost commented Dec 17, 2021

Tagging subscribers to this area: @dotnet/area-system-text-json
See info in area-owners.md if you want to be subscribed.

Issue Details

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.

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

Build started...
1>------ Build started: Project: SystemTextJsonTest, Configuration: Debug Any CPU ------
1>CSC : warning CS8785: Generator 'JsonSourceGenerator' failed to generate source. It will not contribute to the output and compilation errors may occur as a result. Exception was of type 'InvalidOperationException' with message 'Sequence contains no elements'
1>D:\src\SystemTextJsonTest\SystemTextJsonTest\TestDto.cs(11,22,11,33): error CS0534: 'Serializers' does not implement inherited abstract member 'JsonSerializerContext.GetTypeInfo(Type)'
1>D:\src\SystemTextJsonTest\SystemTextJsonTest\TestDto.cs(11,22,11,33): error CS0534: 'Serializers' does not implement inherited abstract member 'JsonSerializerContext.GeneratedSerializerOptions.get'
1>Done building project "SystemTextJsonTest.csproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

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

Author: Kloizdena
Assignees: -
Labels:

area-System.Text.Json, untriaged

Milestone: -

@layomia layomia added bug and removed untriaged New issue has not been triaged by the area owner labels Dec 20, 2021
@layomia
Copy link
Contributor

layomia commented Dec 20, 2021

Can repro. This bug can affect lots of users and a bug fix should be considered for 6.0.

@layomia layomia added this to the 6.0.x milestone Dec 20, 2021
@layomia layomia self-assigned this Dec 20, 2021
@deeprobin

This comment has been minimized.

@deeprobin
Copy link
Contributor

Oh I see the bot has already created a backport PR.

@ghost ghost added the in-pr There is an active PR which will close this issue when it is merged label Jan 15, 2022
@layomia layomia modified the milestones: 6.0.x, 7.0.0 Feb 2, 2022
layomia pushed a commit that referenced this issue Feb 2, 2022
* Member resolving fix for derived attributes (#62937)

* Added property with simple [Required] attribute
@ghost ghost removed the in-pr There is an active PR which will close this issue when it is merged label Feb 2, 2022
@veertien
Copy link

This issue did not surface for me until I used .NET SDK 6.0.2 (dotnet/core#7172).

sdk setup result
6.0.1 JsonSerializerContext in project A, type to serialize with RequiredAttribute in project B both projects compile without error
6.0.1 JsonSerializerContext and type to serialize both in the same project build error due to 'JsonSourceGenerator' failed to generate source
6.0.2 JsonSerializerContext in project A, type to serialize with RequiredAttribute in project B build error due to 'JsonSourceGenerator' failed to generate source
6.0.2 JsonSerializerContext and type to serialize both in the same project build error due to 'JsonSourceGenerator' failed to generate source

@layomia
Copy link
Contributor

layomia commented Mar 7, 2022

Taking a look.

@JakeYallop
Copy link
Contributor

JakeYallop commented Apr 2, 2022

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?

@eiriktsarpalis
Copy link
Member

Moving to .NET 8 milestone.

@eiriktsarpalis eiriktsarpalis modified the milestones: 7.0.0, 8.0.0 Jul 19, 2022
@JakeYallop
Copy link
Contributor

I'm not sure if this issue should still be open - it seems like this is fixed in .NET 7.0.

@eiriktsarpalis
Copy link
Member

eiriktsarpalis commented Sep 12, 2022

@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

@eiriktsarpalis eiriktsarpalis modified the milestones: 8.0.0, 6.0.x Sep 12, 2022
@onionhammer
Copy link

.net 7's new 'required' & init-only props are also unsupported;

i.e. public required string Test { get; init; }

Is there something tracking this I can subscribe to?

@eiriktsarpalis
Copy link
Member

Yes, see #58770

@eiriktsarpalis
Copy link
Member

@veertien I checked this particular scenario using sdk version 6.0.101

sdk setup result
6.0.1 JsonSerializerContext in project A, type to serialize with RequiredAttribute in project B both projects compile without error

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.

@xlievo
Copy link

xlievo commented Dec 7, 2022

CS0534:
So far, it is still easy to reproduce this problem. Referencing Business.SourceGenerator.Test
v7 System.Text.Json NuGet package It didn't work!

@eiriktsarpalis
Copy link
Member

@xlievo please create a new issue containing a minimal reproduction. Thanks!

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

Successfully merging a pull request may close this issue.

8 participants