@@ -7,6 +7,153 @@ namespace Microsoft.Extensions.Validation.GeneratorTests;
77
88public partial class ValidationsGeneratorTests : ValidationsGeneratorTestBase
99{
10+ [ Fact ]
11+ public async Task CanValidateComplexTypesWithJsonIgnore ( )
12+ {
13+ // Arrange
14+ var source = """
15+ using System;
16+ using System.ComponentModel.DataAnnotations;
17+ using System.Collections.Generic;
18+ using System.Threading.Tasks;
19+ using Microsoft.AspNetCore.Builder;
20+ using Microsoft.AspNetCore.Http;
21+ using Microsoft.Extensions.Validation;
22+ using Microsoft.AspNetCore.Routing;
23+ using Microsoft.Extensions.DependencyInjection;
24+ using Microsoft.AspNetCore.Mvc;
25+ using System.Text.Json.Serialization;
26+
27+ var builder = WebApplication.CreateBuilder();
28+
29+ builder.Services.AddValidation();
30+
31+ var app = builder.Build();
32+
33+ app.MapPost("/complex-type-with-json-ignore", (ComplexTypeWithJsonIgnore complexType) => Results.Ok("Passed"!));
34+ app.MapPost("/record-type-with-json-ignore", (RecordTypeWithJsonIgnore recordType) => Results.Ok("Passed"!));
35+
36+ app.Run();
37+
38+ public class ComplexTypeWithJsonIgnore
39+ {
40+ [Range(10, 100)]
41+ public int ValidatedProperty { get; set; } = 10;
42+
43+ [JsonIgnore]
44+ [Required] // This should be ignored because of [JsonIgnore]
45+ public string IgnoredProperty { get; set; } = null!;
46+
47+ [JsonIgnore]
48+ public CircularReferenceType? CircularReference { get; set; }
49+ }
50+
51+ public class CircularReferenceType
52+ {
53+ [JsonIgnore]
54+ public ComplexTypeWithJsonIgnore? Parent { get; set; }
55+
56+ public string Name { get; set; } = "test";
57+ }
58+
59+ public record RecordTypeWithJsonIgnore
60+ {
61+ [Range(10, 100)]
62+ public int ValidatedProperty { get; set; } = 10;
63+
64+ [JsonIgnore]
65+ [Required] // This should be ignored because of [JsonIgnore]
66+ public string IgnoredProperty { get; set; } = null!;
67+
68+ [JsonIgnore]
69+ public CircularReferenceRecord? CircularReference { get; set; }
70+ }
71+
72+ public record CircularReferenceRecord
73+ {
74+ [JsonIgnore]
75+ public RecordTypeWithJsonIgnore? Parent { get; set; }
76+
77+ public string Name { get; set; } = "test";
78+ }
79+ """ ;
80+ await Verify ( source , out var compilation ) ;
81+ await VerifyEndpoint ( compilation , "/complex-type-with-json-ignore" , async ( endpoint , serviceProvider ) =>
82+ {
83+ await ValidInputWithJsonIgnoreProducesNoWarnings ( endpoint ) ;
84+ await InvalidValidatedPropertyProducesError ( endpoint ) ;
85+
86+ async Task ValidInputWithJsonIgnoreProducesNoWarnings ( Endpoint endpoint )
87+ {
88+ var payload = """
89+ {
90+ "ValidatedProperty": 50
91+ }
92+ """ ;
93+ var context = CreateHttpContextWithPayload ( payload , serviceProvider ) ;
94+ await endpoint . RequestDelegate ( context ) ;
95+
96+ Assert . Equal ( 200 , context . Response . StatusCode ) ;
97+ }
98+
99+ async Task InvalidValidatedPropertyProducesError ( Endpoint endpoint )
100+ {
101+ var payload = """
102+ {
103+ "ValidatedProperty": 5
104+ }
105+ """ ;
106+ var context = CreateHttpContextWithPayload ( payload , serviceProvider ) ;
107+
108+ await endpoint . RequestDelegate ( context ) ;
109+
110+ var problemDetails = await AssertBadRequest ( context ) ;
111+ Assert . Collection ( problemDetails . Errors , kvp =>
112+ {
113+ Assert . Equal ( "ValidatedProperty" , kvp . Key ) ;
114+ Assert . Equal ( "The field ValidatedProperty must be between 10 and 100." , kvp . Value . Single ( ) ) ;
115+ } ) ;
116+ }
117+ } ) ;
118+
119+ await VerifyEndpoint ( compilation , "/record-type-with-json-ignore" , async ( endpoint , serviceProvider ) =>
120+ {
121+ await ValidInputWithJsonIgnoreProducesNoWarningsForRecord ( endpoint ) ;
122+ await InvalidValidatedPropertyProducesErrorForRecord ( endpoint ) ;
123+
124+ async Task ValidInputWithJsonIgnoreProducesNoWarningsForRecord ( Endpoint endpoint )
125+ {
126+ var payload = """
127+ {
128+ "ValidatedProperty": 50
129+ }
130+ """ ;
131+ var context = CreateHttpContextWithPayload ( payload , serviceProvider ) ;
132+ await endpoint . RequestDelegate ( context ) ;
133+
134+ Assert . Equal ( 200 , context . Response . StatusCode ) ;
135+ }
136+
137+ async Task InvalidValidatedPropertyProducesErrorForRecord ( Endpoint endpoint )
138+ {
139+ var payload = """
140+ {
141+ "ValidatedProperty": 5
142+ }
143+ """ ;
144+ var context = CreateHttpContextWithPayload ( payload , serviceProvider ) ;
145+
146+ await endpoint . RequestDelegate ( context ) ;
147+
148+ var problemDetails = await AssertBadRequest ( context ) ;
149+ Assert . Collection ( problemDetails . Errors , kvp =>
150+ {
151+ Assert . Equal ( "ValidatedProperty" , kvp . Key ) ;
152+ Assert . Equal ( "The field ValidatedProperty must be between 10 and 100." , kvp . Value . Single ( ) ) ;
153+ } ) ;
154+ }
155+ } ) ;
156+ }
10157 [ Fact ]
11158 public async Task CanValidateComplexTypes ( )
12159 {
0 commit comments