dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
15.27k stars 4.73k forks source link

OptionsValidatorGenerator does not generate a Validate method #94912

Closed koenigst closed 11 months ago

koenigst commented 11 months ago

Description

When a class is marked with the OptionsValidatorAttribute implements IValidatableObject the OptionsValidatorGenerator does not generate a Validate method if no properties are marked with validation attributes.

Reproduction Steps

using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using Microsoft.Extensions.Options;

[OptionsValidator]
public partial class SampleOptions : IValidateOptions<SampleOptions>, IValidatableObject
{
    public IEnumerable<ValidationResult> Validate(ValidationContext context)
    {
        return new[] { new ValidationResult($"Display: {context.DisplayName}, Member: {context.MemberName}") };
    }
}

Expected behavior

The generator generates a Validate method calling the IValidatableObject.Validate method.

Actual behavior

The sample does not compile due to the missing Validate method.

Regression?

No response

Known Workarounds

No response

Configuration

Other information

No response

ghost commented 11 months ago

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

Issue Details
### Description When a class is marked with the `OptionsValidatorAttribute` implements `IValidatableObject` the `OptionsValidatorGenerator` does not generate a `Validate` method if no properties are marked with validation attributes. ### Reproduction Steps ```csharp using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using Microsoft.Extensions.Options; [OptionsValidator] public partial class SampleOptions : IValidateOptions, IValidatableObject { public IEnumerable Validate(ValidationContext context) { return new[] { new ValidationResult($"Display: {context.DisplayName}, Member: {context.MemberName}") }; } } ``` ### Expected behavior The generator generates a `Validate` method calling the `IValidatableObject.Validate` method. ### Actual behavior The sample does not compile due to the missing `Validate` method. ### Regression? _No response_ ### Known Workarounds _No response_ ### Configuration * .NET SDK: 8.0.100 * OS: Windows 11 * Arch: x64 * `Microsoft.Extensions.Options`: 8.0.0 ### Other information _No response_
Author: koenigst
Assignees: -
Labels: `area-System.ComponentModel.DataAnnotations`
Milestone: -
ghost commented 11 months ago

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

Issue Details
### Description When a class is marked with the `OptionsValidatorAttribute` implements `IValidatableObject` the `OptionsValidatorGenerator` does not generate a `Validate` method if no properties are marked with validation attributes. ### Reproduction Steps ```csharp using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using Microsoft.Extensions.Options; [OptionsValidator] public partial class SampleOptions : IValidateOptions, IValidatableObject { public IEnumerable Validate(ValidationContext context) { return new[] { new ValidationResult($"Display: {context.DisplayName}, Member: {context.MemberName}") }; } } ``` ### Expected behavior The generator generates a `Validate` method calling the `IValidatableObject.Validate` method. ### Actual behavior The sample does not compile due to the missing `Validate` method. ### Regression? _No response_ ### Known Workarounds _No response_ ### Configuration * .NET SDK: 8.0.100 * OS: Windows 11 * Arch: x64 * `Microsoft.Extensions.Options`: 8.0.0 ### Other information _No response_
Author: koenigst
Assignees: -
Labels: `area-Extensions-Options`, `in-pr`, `source-generator`
Milestone: 9.0.0