Skip to content

Commit 10f4308

Browse files
committed
Fix AzureAdOptions tests
- Set content root to AppContext.BaseDirectory to prevent failure when running in helix - Add AzureAdOptions type info
1 parent e70e9e7 commit 10f4308

File tree

3 files changed

+82
-19
lines changed

3 files changed

+82
-19
lines changed

src/Tests/Microsoft.Diagnostics.Monitoring.Tool.UnitTestsSample/Program.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using Microsoft.Diagnostics.Tools.Monitor;
45
using Microsoft.AspNetCore.Http.Validation;
56
using Microsoft.Diagnostics.Tools.Monitor.CollectionRules.Options.Actions;
67
using Microsoft.Diagnostics.Tools.Monitor.Extensibility;
78
using Microsoft.Diagnostics.Tools.Monitor.Egress.FileSystem;
89

9-
var builder = WebApplication.CreateBuilder(args);
10+
var builder = WebApplication.CreateBuilder(new WebApplicationOptions
11+
{
12+
ContentRootPath = AppContext.BaseDirectory
13+
});
14+
1015
Microsoft.AspNetCore.Http.Validation.Generated.GeneratedServiceCollectionExtensions.AddValidation(builder.Services);
1116
builder.Build();
1217

@@ -22,4 +27,6 @@ sealed class TestValidatableType
2227
public required FileSystemEgressProviderOptions FileSystemEgressProviderOptions { get; init; }
2328

2429
public required ExecuteOptions ExecuteOptions { get; init; }
30+
31+
public required AzureAdOptions AzureAdOptions { get; init; }
2532
}

src/Tests/Microsoft.Diagnostics.Monitoring.Tool.UnitTestsSample/ValidatableInfoResolver.cs

Lines changed: 74 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,11 @@ public bool TryGetValidatableTypeInfo(global::System.Type type, [global::System.
159159
validatableInfo = CreateExecuteOptions();
160160
return true;
161161
}
162+
if (type == typeof(global::Microsoft.Diagnostics.Tools.Monitor.AzureAdOptions))
163+
{
164+
validatableInfo = CreateAzureAdOptions();
165+
return true;
166+
}
162167
if (type == typeof(global::TestValidatableType))
163168
{
164169
validatableInfo = CreateTestValidatableType();
@@ -688,12 +693,6 @@ private ValidatableTypeInfo CreateBaseRecordOptions()
688693
return new GeneratedValidatableTypeInfo(
689694
type: typeof(global::Microsoft.Diagnostics.Tools.Monitor.CollectionRules.Options.Actions.BaseRecordOptions),
690695
members: [
691-
// new GeneratedValidatablePropertyInfo(
692-
// containingType: typeof(global::Microsoft.Diagnostics.Tools.Monitor.CollectionRules.Options.Actions.BaseRecordOptions),
693-
// propertyType: typeof(global::System.Type),
694-
// name: "EqualityContract",
695-
// displayName: "EqualityContract"
696-
// ),
697696
]
698697
);
699698
}
@@ -702,12 +701,6 @@ private ValidatableTypeInfo CreateExecuteOptions()
702701
return new GeneratedValidatableTypeInfo(
703702
type: typeof(global::Microsoft.Diagnostics.Tools.Monitor.CollectionRules.Options.Actions.ExecuteOptions),
704703
members: [
705-
// new GeneratedValidatablePropertyInfo(
706-
// containingType: typeof(global::Microsoft.Diagnostics.Tools.Monitor.CollectionRules.Options.Actions.ExecuteOptions),
707-
// propertyType: typeof(global::System.Type),
708-
// name: "EqualityContract",
709-
// displayName: "EqualityContract"
710-
// ),
711704
new GeneratedValidatablePropertyInfo(
712705
containingType: typeof(global::Microsoft.Diagnostics.Tools.Monitor.CollectionRules.Options.Actions.ExecuteOptions),
713706
propertyType: typeof(string),
@@ -717,6 +710,38 @@ private ValidatableTypeInfo CreateExecuteOptions()
717710
]
718711
);
719712
}
713+
private ValidatableTypeInfo CreateAzureAdOptions()
714+
{
715+
return new GeneratedValidatableTypeInfo(
716+
type: typeof(global::Microsoft.Diagnostics.Tools.Monitor.AzureAdOptions),
717+
members: [
718+
new GeneratedValidatablePropertyInfo(
719+
containingType: typeof(global::Microsoft.Diagnostics.Tools.Monitor.AzureAdOptions),
720+
propertyType: typeof(string),
721+
name: "TenantId",
722+
displayName: "TenantId"
723+
),
724+
new GeneratedValidatablePropertyInfo(
725+
containingType: typeof(global::Microsoft.Diagnostics.Tools.Monitor.AzureAdOptions),
726+
propertyType: typeof(string),
727+
name: "ClientId",
728+
displayName: "ClientId"
729+
),
730+
new GeneratedValidatablePropertyInfo(
731+
containingType: typeof(global::Microsoft.Diagnostics.Tools.Monitor.AzureAdOptions),
732+
propertyType: typeof(global::System.Uri),
733+
name: "AppIdUri",
734+
displayName: "AppIdUri"
735+
),
736+
new GeneratedValidatablePropertyInfo(
737+
containingType: typeof(global::Microsoft.Diagnostics.Tools.Monitor.AzureAdOptions),
738+
propertyType: typeof(string),
739+
name: "RequiredRole",
740+
displayName: "RequiredRole"
741+
),
742+
]
743+
);
744+
}
720745
private ValidatableTypeInfo CreateTestValidatableType()
721746
{
722747
return new GeneratedValidatableTypeInfo(
@@ -740,6 +765,12 @@ private ValidatableTypeInfo CreateTestValidatableType()
740765
name: "ExecuteOptions",
741766
displayName: "ExecuteOptions"
742767
),
768+
new GeneratedValidatablePropertyInfo(
769+
containingType: typeof(global::TestValidatableType),
770+
propertyType: typeof(global::Microsoft.Diagnostics.Tools.Monitor.AzureAdOptions),
771+
name: "AzureAdOptions",
772+
displayName: "AzureAdOptions"
773+
),
743774
]
744775
);
745776
}
@@ -749,8 +780,8 @@ private ValidatableTypeInfo CreateTestValidatableType()
749780
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.AspNetCore.Http.ValidationsGenerator, Version=10.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60", "10.0.0.0")]
750781
internal static class GeneratedServiceCollectionExtensions
751782
{
752-
// [global::System.Runtime.CompilerServices.InterceptsLocationAttribute(1, "nMLdYxVpxyjXwof6UabUh7sBAABQcm9ncmFtLmNz")]
753-
public static global::Microsoft.Extensions.DependencyInjection.IServiceCollection AddValidation(this global::Microsoft.Extensions.DependencyInjection.IServiceCollection services, global::System.Action<ValidationOptions>? configureOptions = null)
783+
// [global::System.Runtime.CompilerServices.InterceptsLocationAttribute(1, "/S4C/fYaYK+RGue0pzw4eqSWAABWYWxpZGF0YWJsZUluZm9SZXNvbHZlci5jcw==")]
784+
public static global::Microsoft.Extensions.DependencyInjection.IServiceCollection AddValidation(this global::Microsoft.Extensions.DependencyInjection.IServiceCollection services, global::System.Action<global::Microsoft.AspNetCore.Http.Validation.ValidationOptions>? configureOptions = null)
754785
{
755786
// Use non-extension method to avoid infinite recursion.
756787
return global::Microsoft.Extensions.DependencyInjection.ValidationServiceCollectionExtensions.AddValidation(services, options =>
@@ -777,13 +808,39 @@ private sealed record CacheKey(global::System.Type ContainingType, string Proper
777808
var key = new CacheKey(containingType, propertyName);
778809
return _cache.GetOrAdd(key, static k =>
779810
{
811+
var results = new global::System.Collections.Generic.List<global::System.ComponentModel.DataAnnotations.ValidationAttribute>();
812+
813+
// Get attributes from the property
780814
var property = k.ContainingType.GetProperty(k.PropertyName);
781-
if (property == null)
815+
if (property != null)
782816
{
783-
return [];
817+
var propertyAttributes = global::System.Reflection.CustomAttributeExtensions
818+
.GetCustomAttributes<global::System.ComponentModel.DataAnnotations.ValidationAttribute>(property, inherit: true);
819+
820+
results.AddRange(propertyAttributes);
821+
}
822+
823+
// Check constructors for parameters that match the property name
824+
// to handle record scenarios
825+
foreach (var constructor in k.ContainingType.GetConstructors())
826+
{
827+
// Look for parameter with matching name (case insensitive)
828+
var parameter = global::System.Linq.Enumerable.FirstOrDefault(
829+
constructor.GetParameters(),
830+
p => string.Equals(p.Name, k.PropertyName, global::System.StringComparison.OrdinalIgnoreCase));
831+
832+
if (parameter != null)
833+
{
834+
var paramAttributes = global::System.Reflection.CustomAttributeExtensions
835+
.GetCustomAttributes<global::System.ComponentModel.DataAnnotations.ValidationAttribute>(parameter, inherit: true);
836+
837+
results.AddRange(paramAttributes);
838+
839+
break;
840+
}
784841
}
785842

786-
return [.. global::System.Reflection.CustomAttributeExtensions.GetCustomAttributes<global::System.ComponentModel.DataAnnotations.ValidationAttribute>(property, inherit: true)];
843+
return results.ToArray();
787844
});
788845
}
789846
}

src/Tools/dotnet-monitor/CollectionRules/Configuration/TemplatesConfigureNamedOptions.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ public TemplatesPostConfigureOptions(
3030

3131
public void PostConfigure(string? name, TemplateOptions options)
3232
{
33-
System.Console.WriteLine("TemplatesConfigureNamedOptions PostConfigure");
3433
IConfigurationSection collectionRuleActionsSection = _configuration.GetSection(collectionRuleActionsPath);
3534

3635
if (options.CollectionRuleActions != null)

0 commit comments

Comments
 (0)