Skip to content

Commit 7a64683

Browse files
oroztocilgithub-actions
authored andcommitted
Add test case, reuse ImplementsInterface method
1 parent 445d4c7 commit 7a64683

File tree

3 files changed

+69
-11
lines changed

3 files changed

+69
-11
lines changed

src/Validation/gen/Parsers/ValidationsGenerator.TypesParser.cs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -305,15 +305,6 @@ internal static bool HasValidationAttributes(ISymbol symbol, WellKnownTypes well
305305
internal static bool HasIValidatableObjectInterface(ITypeSymbol typeSymbol, WellKnownTypes wellKnownTypes)
306306
{
307307
var validatableObjectSymbol = wellKnownTypes.Get(WellKnownTypeData.WellKnownType.System_ComponentModel_DataAnnotations_IValidatableObject);
308-
309-
foreach (var inter in typeSymbol.AllInterfaces)
310-
{
311-
if (SymbolEqualityComparer.Default.Equals(inter, validatableObjectSymbol))
312-
{
313-
return true;
314-
}
315-
}
316-
317-
return false;
308+
return typeSymbol.ImplementsInterface(validatableObjectSymbol);
318309
}
319310
}

src/Validation/test/Microsoft.Extensions.Validation.GeneratorTests/ValidationsGenerator.IValidatableObject.cs

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ public async Task CanValidateIValidatableObject_WithoutPropertyValidations()
187187
188188
app.MapPost("/base", (BaseClass model) => Results.Ok(model));
189189
app.MapPost("/derived", (DerivedClass model) => Results.Ok(model));
190+
app.MapPost("/complex", (ComplexClass model) => Results.Ok(model));
190191
191192
app.Run();
192193
@@ -206,6 +207,23 @@ public IEnumerable<ValidationResult> Validate(ValidationContext validationContex
206207
public class DerivedClass : BaseClass
207208
{
208209
}
210+
211+
public class ComplexClass
212+
{
213+
public NestedClass? NestedObject { get; set; }
214+
}
215+
216+
public class NestedClass : IValidatableObject
217+
{
218+
public string? Value { get; set; }
219+
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
220+
{
221+
if (string.IsNullOrEmpty(Value))
222+
{
223+
yield return new ValidationResult("Value cannot be null or empty.", [nameof(Value)]);
224+
}
225+
}
226+
}
209227
""";
210228

211229
await Verify(source, out var compilation);
@@ -259,6 +277,32 @@ async Task ValidateMethodCalled()
259277
});
260278
}
261279
});
262-
}
263280

281+
await VerifyEndpoint(compilation, "/complex", async (endpoint, serviceProvider) =>
282+
{
283+
await ValidateMethodCalled();
284+
285+
async Task ValidateMethodCalled()
286+
{
287+
var httpContext = CreateHttpContextWithPayload("""
288+
{
289+
"NestedObject": {
290+
"Value": ""
291+
}
292+
}
293+
""", serviceProvider);
294+
295+
await endpoint.RequestDelegate(httpContext);
296+
297+
var problemDetails = await AssertBadRequest(httpContext);
298+
Assert.Collection(problemDetails.Errors,
299+
error =>
300+
{
301+
Assert.Equal("NestedObject.Value", error.Key);
302+
Assert.Collection(error.Value,
303+
msg => Assert.Equal("Value cannot be null or empty.", msg));
304+
});
305+
}
306+
});
307+
}
264308
}

src/Validation/test/Microsoft.Extensions.Validation.GeneratorTests/snapshots/ValidationsGeneratorTests.CanValidateIValidatableObject_WithoutPropertyValidations#ValidatableInfoResolver.g.verified.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,29 @@ public bool TryGetValidatableTypeInfo(global::System.Type type, [global::System.
8787
);
8888
return true;
8989
}
90+
if (type == typeof(global::NestedClass))
91+
{
92+
validatableInfo = new GeneratedValidatableTypeInfo(
93+
type: typeof(global::NestedClass),
94+
members: []
95+
);
96+
return true;
97+
}
98+
if (type == typeof(global::ComplexClass))
99+
{
100+
validatableInfo = new GeneratedValidatableTypeInfo(
101+
type: typeof(global::ComplexClass),
102+
members: [
103+
new GeneratedValidatablePropertyInfo(
104+
containingType: typeof(global::ComplexClass),
105+
propertyType: typeof(global::NestedClass),
106+
name: "NestedObject",
107+
displayName: "NestedObject"
108+
),
109+
]
110+
);
111+
return true;
112+
}
90113

91114
return false;
92115
}

0 commit comments

Comments
 (0)