Skip to content

Commit c454bf0

Browse files
committed
Fix ExtensionManifest_EmptyObject_ThrowOnValidate test
1 parent 82b89b8 commit c454bf0

File tree

11 files changed

+90
-21
lines changed

11 files changed

+90
-21
lines changed

Directory.Packages.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,6 @@
2929
<PackageVersion Include="System.CommandLine" Version="$(SystemCommandLineVersion)" />
3030
<PackageVersion Include="System.Private.Uri" Version="$(SystemPrivateUriVersion)" />
3131
<PackageVersion Include="System.Text.Json" Version="$(SystemTextJsonVersion)" />
32+
<PackageVersion Include="Microsoft.AspNetCore.Mvc.Testing" Version="$(MicrosoftAspNetCoreApp100Version)" />
3233
</ItemGroup>
3334
</Project>

src/Tests/CollectionRuleActions.UnitTests/CollectionRuleActions.UnitTests.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
<PropertyGroup>
44
<TargetFrameworks>$(ToolTargetFrameworks)</TargetFrameworks>
5-
<DisableCompileTimeOpenApiXmlGenerator>true</DisableCompileTimeOpenApiXmlGenerator>
65
</PropertyGroup>
76

87
<ItemGroup>

src/Tests/Microsoft.Diagnostics.Monitoring.Tool.UnitTests/ExtensionManifestTests.cs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,34 @@
44
using Microsoft.AspNetCore.Http.Validation;
55
using Microsoft.Diagnostics.Monitoring.TestCommon;
66
using Microsoft.Diagnostics.Tools.Monitor.Extensibility;
7+
using Microsoft.Extensions.DependencyInjection;
8+
using Microsoft.Extensions.Options;
9+
using Microsoft.AspNetCore.Mvc.Testing;
710
using System.IO;
811
using System.Text.Json;
912
using Xunit;
1013
using Xunit.Abstractions;
1114

1215
namespace Microsoft.Diagnostics.Monitoring.Tool.UnitTests
1316
{
17+
public class ExtensionManifestFixture : WebApplicationFactory<Program>
18+
{
19+
}
20+
1421
[TargetFrameworkMonikerTrait(TargetFrameworkMonikerExtensions.CurrentTargetFrameworkMoniker)]
15-
public sealed class ExtensionManifestTests
22+
public sealed class ExtensionManifestTests : IClassFixture<ExtensionManifestFixture>
1623
{
1724
private const string ExpectedName = "CustomEgress";
1825
private const string ExpectedExecutableName = "CustomExecutable";
1926
private const string ExpectedAssemblyName = "CustomAssembly";
2027

2128
private readonly ITestOutputHelper _outputHelper;
29+
private readonly ExtensionManifestFixture _fixture;
2230

23-
public ExtensionManifestTests(ITestOutputHelper outputHelper)
31+
public ExtensionManifestTests(ITestOutputHelper outputHelper, ExtensionManifestFixture fixture)
2432
{
2533
_outputHelper = outputHelper;
34+
_fixture = fixture;
2635
}
2736

2837
[Fact]
@@ -92,7 +101,8 @@ public void ExtensionManifest_EmptyObject_ThrowOnValidate()
92101
Assert.Null(manifest.AssemblyFileName);
93102
Assert.Null(manifest.ExecutableFileName);
94103

95-
ExtensionException ex = Assert.Throws<ExtensionException>(() => manifest.Validate(new Microsoft.AspNetCore.Http.Validation.ValidationOptions()));
104+
var validationOptions = _fixture.Services.GetRequiredService<IOptions<ValidationOptions>>().Value;
105+
ExtensionException ex = Assert.Throws<ExtensionException>(() => manifest.Validate(validationOptions));
96106
Assert.Null(ex.InnerException);
97107
}
98108

@@ -106,8 +116,7 @@ public void ExtensionManifest_NameOnly_ThrowOnValidate()
106116
{
107117
using Utf8JsonWriter writer = new(stream);
108118
writer.WriteStartObject();
109-
writer.WriteString(nameof(ExtensionManifest.Name), ExpectedName);
110-
writer.WriteEndObject();
119+
writer.WriteString(nameof(ExtensionManifest.Name), ExpectedName);writer.WriteEndObject();
111120
writer.Flush();
112121
}
113122

@@ -116,7 +125,8 @@ public void ExtensionManifest_NameOnly_ThrowOnValidate()
116125
Assert.Null(manifest.AssemblyFileName);
117126
Assert.Null(manifest.ExecutableFileName);
118127

119-
ExtensionException ex = Assert.Throws<ExtensionException>(() => manifest.Validate(new ValidationOptions()));
128+
var validationOptions = _fixture.Services.GetRequiredService<IOptions<ValidationOptions>>().Value;
129+
ExtensionException ex = Assert.Throws<ExtensionException>(() => manifest.Validate(validationOptions));
120130
Assert.Null(ex.InnerException);
121131
}
122132

src/Tests/Microsoft.Diagnostics.Monitoring.Tool.UnitTests/Microsoft.Diagnostics.Monitoring.Tool.UnitTests.csproj

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
22

33
<PropertyGroup>
44
<TargetFrameworks>$(ToolTargetFrameworks)</TargetFrameworks>
@@ -8,13 +8,15 @@
88
<ItemGroup>
99
<PackageReference Include="Microsoft.AspNetCore.OpenApi" GeneratePathProperty="true" />
1010
<PackageReference Include="Moq" />
11+
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" />
1112
</ItemGroup>
1213

1314
<ItemGroup>
1415
<ProjectReference Include="..\..\Microsoft.Diagnostics.Monitoring.Options\Microsoft.Diagnostics.Monitoring.Options.csproj" />
1516
<ProjectReference Include="..\..\Tools\dotnet-monitor\dotnet-monitor.csproj" />
1617
<ProjectReference Include="..\Microsoft.Diagnostics.Monitoring.TestCommon\Microsoft.Diagnostics.Monitoring.TestCommon.csproj" />
1718
<ProjectReference Include="..\Microsoft.Diagnostics.Monitoring.Tool.UnitTestCommon\Microsoft.Diagnostics.Monitoring.Tool.UnitTestCommon.csproj" />
19+
<ProjectReference Include="..\Microsoft.Diagnostics.Monitoring.Tool.UnitTestsSample\Microsoft.Diagnostics.Monitoring.Tool.UnitTestsSample.csproj" />
1820
</ItemGroup>
1921

2022
<ItemGroup>
@@ -182,4 +184,9 @@
182184
</None>
183185
</ItemGroup>
184186

187+
<ItemGroup Condition="'$(TargetFramework)' == 'net10.0'">
188+
<KnownFrameworkReference Update="Microsoft.NETCore.App" TargetingPackVersion="$(NetCoreAppVersion)" DefaultRuntimeFrameworkVersion="$(NetCoreAppVersion)" LatestRuntimeFrameworkVersion="$(NetCoreAppVersion)" />
189+
<KnownFrameworkReference Update="Microsoft.AspNetCore.App" TargetingPackVersion="$(AspNetCoreAppVersion)" DefaultRuntimeFrameworkVersion="$(AspNetCoreAppVersion)" LatestRuntimeFrameworkVersion="$(AspNetCoreAppVersion)" />
190+
</ItemGroup>
191+
185192
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net10.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<InterceptorsNamespaces>$(InterceptorsNamespaces);Microsoft.AspNetCore.Http.Validation.Generated</InterceptorsNamespaces>
8+
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<KnownFrameworkReference Update="Microsoft.NETCore.App" TargetingPackVersion="$(NetCoreAppVersion)" DefaultRuntimeFrameworkVersion="$(NetCoreAppVersion)" LatestRuntimeFrameworkVersion="$(NetCoreAppVersion)" />
13+
<KnownFrameworkReference Update="Microsoft.AspNetCore.App" TargetingPackVersion="$(AspNetCoreAppVersion)" DefaultRuntimeFrameworkVersion="$(AspNetCoreAppVersion)" LatestRuntimeFrameworkVersion="$(AspNetCoreAppVersion)" />
14+
</ItemGroup>
15+
16+
<ItemGroup>
17+
<ProjectReference Include="..\..\Tools\dotnet-monitor\dotnet-monitor.csproj" />
18+
</ItemGroup>
19+
20+
</Project>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using Microsoft.AspNetCore.Http.Validation;
5+
using Microsoft.Diagnostics.Tools.Monitor.Extensibility;
6+
7+
var builder = WebApplication.CreateBuilder(args);
8+
builder.Services.AddValidation();
9+
builder.Build();
10+
11+
public partial class Program {}
12+
13+
[ValidatableType]
14+
sealed class TestValidatableType
15+
{
16+
public required ExtensionManifest ExtensionManifest { get; init; }
17+
}

src/Tools/dotnet-monitor/Extensibility/ExtensionManifest.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ internal partial class ExtensionManifestContext : JsonSerializerContext
2020
{
2121
}
2222

23+
[ValidatableType]
2324
internal class ExtensionManifest : IValidatableObject
2425
{
2526
public const string DefaultFileName = "extension.json";
@@ -96,7 +97,8 @@ public IEnumerable<ValidationResult> Validate(ValidationContext validationContex
9697
CultureInfo.InvariantCulture,
9798
Strings.ErrorMessage_TwoFieldsCannotBeSpecified,
9899
nameof(AssemblyFileName),
99-
nameof(ExecutableFileName))));
100+
nameof(ExecutableFileName)),
101+
[nameof(AssemblyFileName), nameof(ExecutableFileName)]));
100102
}
101103

102104
if (!hasAssemblyFileName && !hasExecutableFileName)
@@ -107,7 +109,8 @@ public IEnumerable<ValidationResult> Validate(ValidationContext validationContex
107109
CultureInfo.InvariantCulture,
108110
Strings.ErrorMessage_TwoFieldsMissing,
109111
nameof(AssemblyFileName),
110-
nameof(ExecutableFileName))));
112+
nameof(ExecutableFileName)),
113+
[nameof(AssemblyFileName), nameof(ExecutableFileName)]));
111114
}
112115

113116
return results;

src/Tools/dotnet-monitor/HostBuilder/HostBuilderHelper.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,6 @@ public static IHostBuilder CreateHostBuilder(HostBuilderSettings settings)
115115
//is not added until WebHostDefaults are added.
116116
.ConfigureWebHostDefaults(webBuilder =>
117117
{
118-
TestAssemblies.AddHostingStartup(webBuilder);
119-
120118
// ASP.NET will initially create a configuration that primarily contains
121119
// the ASPNETCORE_* environment variables. This IWebHostBuilder configuration callback
122120
// is invoked before any of the usual configuration phases (host, app, service, container)

src/Tools/dotnet-monitor/Startup.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ public void ConfigureServices(IServiceCollection services)
3636
options.SerializerOptions.TypeInfoResolverChain.Add(MonitorJsonSerializerContext.Default);
3737
});
3838

39-
MyExtensions.AddValidation(services);
40-
4139
services.Configure<ApiBehaviorOptions>(options =>
4240
{
4341
options.InvalidModelStateResponseFactory = context =>

src/Tools/dotnet-monitor/ValidatableInfoResolver.cs

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#nullable enable annotations
1+
#nullable enable annotations
22
//------------------------------------------------------------------------------
33
// <auto-generated>
44
// This code was generated by a tool.
@@ -59,8 +59,12 @@ public GeneratedValidatableTypeInfo(
5959
{
6060
public bool TryGetValidatableTypeInfo(global::System.Type type, [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] out global::Microsoft.AspNetCore.Http.Validation.IValidatableInfo? validatableInfo)
6161
{
62-
System.Console.WriteLine("My TryGetValidatableTypeInfo");
6362
validatableInfo = null;
63+
if (type == typeof(global::Microsoft.Diagnostics.Tools.Monitor.Extensibility.ExtensionManifest))
64+
{
65+
validatableInfo = CreateExtensionManifest();
66+
return true;
67+
}
6468
if (type == typeof(global::Microsoft.Diagnostics.Monitoring.WebApi.MetricProvider))
6569
{
6670
validatableInfo = CreateMetricProvider();
@@ -102,6 +106,20 @@ public bool TryGetValidatableParameterInfo(global::System.Reflection.ParameterIn
102106
return false;
103107
}
104108

109+
private ValidatableTypeInfo CreateExtensionManifest()
110+
{
111+
return new GeneratedValidatableTypeInfo(
112+
type: typeof(global::Microsoft.Diagnostics.Tools.Monitor.Extensibility.ExtensionManifest),
113+
members: [
114+
new GeneratedValidatablePropertyInfo(
115+
containingType: typeof(global::Microsoft.Diagnostics.Tools.Monitor.Extensibility.ExtensionManifest),
116+
propertyType: typeof(string),
117+
name: "Name",
118+
displayName: "Name"
119+
),
120+
]
121+
);
122+
}
105123
private ValidatableTypeInfo CreateMetricProvider()
106124
{
107125
return new GeneratedValidatableTypeInfo(
@@ -242,10 +260,9 @@ static class MyExtensions
242260
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.AspNetCore.Http.ValidationsGenerator, Version=10.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60", "10.0.0.0")]
243261
file static class GeneratedServiceCollectionExtensions
244262
{
245-
// [global::System.Runtime.CompilerServices.InterceptsLocationAttribute(1, "KANyLGuJNiNmJ3Sx9yUA6pQGAABTdGFydHVwLmNz")]
263+
// [global::System.Runtime.CompilerServices.InterceptsLocationAttribute(1, "M2kOaR09/X9mJ22GvhUC4+4FAABTdGFydHVwLmNz")]
246264
public static global::Microsoft.Extensions.DependencyInjection.IServiceCollection AddValidation(this global::Microsoft.Extensions.DependencyInjection.IServiceCollection services, global::System.Action<ValidationOptions>? configureOptions = null)
247265
{
248-
System.Console.WriteLine("My AddValidation");
249266
// Use non-extension method to avoid infinite recursion.
250267
return global::Microsoft.Extensions.DependencyInjection.ValidationServiceCollectionExtensions.AddValidation(services, options =>
251268
{
@@ -265,7 +282,6 @@ private sealed record CacheKey([property: global::System.Diagnostics.CodeAnalysi
265282
private static readonly global::System.Collections.Concurrent.ConcurrentDictionary<CacheKey, global::System.ComponentModel.DataAnnotations.ValidationAttribute[]> _cache = new();
266283

267284
public static global::System.ComponentModel.DataAnnotations.ValidationAttribute[] GetValidationAttributes(
268-
[global::System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(global::System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicProperties)]
269285
global::System.Type containingType,
270286
string propertyName)
271287
{
@@ -282,4 +298,4 @@ private sealed record CacheKey([property: global::System.Diagnostics.CodeAnalysi
282298
});
283299
}
284300
}
285-
}
301+
}

0 commit comments

Comments
 (0)