Skip to content

Commit dbe60c1

Browse files
authored
Fixed issues with the request middleware source generator (#8274)
1 parent c6c658d commit dbe60c1

File tree

5 files changed

+132
-5
lines changed

5 files changed

+132
-5
lines changed

src/HotChocolate/Core/src/Types.Analyzers/FileBuilders/RequestMiddlewareFileBuilder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,14 +121,14 @@ private void WriteCtorServiceResolution(List<RequestMiddlewareParameterInfo> par
121121

122122
case RequestMiddlewareParameterKind.SchemaService when !parameter.IsNullable:
123123
_writer.WriteIndentedLine(
124-
"var cp{0} = core.SchemaServices.GetRequiredService<global::{1}>();",
124+
"var cp{0} = core.SchemaServices.GetRequiredService<{1}>();",
125125
i,
126126
parameter.TypeName);
127127
break;
128128

129129
case RequestMiddlewareParameterKind.SchemaService when parameter.IsNullable:
130130
_writer.WriteIndentedLine(
131-
"var cp{0} = core.SchemaServices.GetService<global::{1}>();",
131+
"var cp{0} = core.SchemaServices.GetService<{1}>();",
132132
i,
133133
parameter.TypeName);
134134
break;

src/HotChocolate/Core/src/Types.Analyzers/Inspectors/RequestMiddlewareInspector.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,11 @@ ctor is not
8787
kind = RequestMiddlewareParameterKind.Service;
8888
}
8989

90-
ctorParameters.Add(new RequestMiddlewareParameterInfo(kind, parameterTypeName));
90+
ctorParameters.Add(
91+
new RequestMiddlewareParameterInfo(
92+
kind,
93+
parameterTypeName,
94+
isNullable: !parameter.IsNonNullable()));
9195
}
9296

9397
foreach (var parameter in invokeMethod.Parameters)
@@ -113,7 +117,11 @@ ctor is not
113117
kind = RequestMiddlewareParameterKind.Service;
114118
}
115119

116-
invokeParameters.Add(new RequestMiddlewareParameterInfo(kind, parameterTypeName));
120+
invokeParameters.Add(
121+
new RequestMiddlewareParameterInfo(
122+
kind,
123+
parameterTypeName,
124+
isNullable: !parameter.IsNonNullable()));
117125
}
118126

119127
syntaxInfo = new RequestMiddlewareInfo(
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
namespace HotChocolate.Types;
2+
3+
public class RequestMiddlewareTests
4+
{
5+
[Fact]
6+
public async Task GenerateSource_RequestMiddleware_MatchesSnapshot()
7+
{
8+
await TestHelper.GetGeneratedSourceSnapshot(
9+
"""
10+
#nullable enable
11+
using System.Threading.Tasks;
12+
using HotChocolate;
13+
using HotChocolate.Execution;
14+
using Microsoft.AspNetCore.Builder;
15+
using Microsoft.Extensions.DependencyInjection;
16+
17+
public class Program
18+
{
19+
public static void Main(string[] args)
20+
{
21+
var builder = WebApplication.CreateBuilder(args);
22+
builder.Services
23+
.AddGraphQLServer()
24+
.UseRequest<SomeRequestMiddleware>();
25+
}
26+
}
27+
28+
public class SomeRequestMiddleware(
29+
RequestDelegate next,
30+
#pragma warning disable CS9113
31+
[SchemaService] Service1 service1,
32+
[SchemaService] Service2? service2)
33+
#pragma warning restore CS9113
34+
{
35+
public async ValueTask InvokeAsync(
36+
IRequestContext context,
37+
#pragma warning disable CS9113
38+
Service1 service1,
39+
Service2? service2)
40+
#pragma warning restore CS9113
41+
{
42+
await next(context);
43+
}
44+
}
45+
46+
public class Service1;
47+
public class Service2;
48+
""").MatchMarkdownAsync();
49+
}
50+
}

src/HotChocolate/Core/test/Types.Analyzers.Tests/TestHelper.cs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,14 @@
88
using GreenDonut;
99
using GreenDonut.Data;
1010
using HotChocolate.Data.Filters;
11+
using HotChocolate.Execution;
12+
using HotChocolate.Execution.Configuration;
1113
using HotChocolate.Types.Analyzers;
1214
using HotChocolate.Types.Pagination;
15+
using Microsoft.AspNetCore.Builder;
1316
using Microsoft.CodeAnalysis;
1417
using Microsoft.CodeAnalysis.CSharp;
18+
using Microsoft.Extensions.DependencyInjection;
1519

1620
namespace HotChocolate.Types;
1721

@@ -33,6 +37,12 @@ public static Snapshot GetGeneratedSourceSnapshot(string[] sourceTexts, string?
3337
#elif NET9_0
3438
.. Net90.References.All,
3539
#endif
40+
// HotChocolate.Execution
41+
MetadataReference.CreateFromFile(typeof(RequestDelegate).Assembly.Location),
42+
43+
// HotChocolate.Execution.Abstractions
44+
MetadataReference.CreateFromFile(typeof(IRequestExecutorBuilder).Assembly.Location),
45+
3646
// HotChocolate.Types
3747
MetadataReference.CreateFromFile(typeof(ObjectTypeAttribute).Assembly.Location),
3848
MetadataReference.CreateFromFile(typeof(Connection).Assembly.Location),
@@ -41,6 +51,10 @@ public static Snapshot GetGeneratedSourceSnapshot(string[] sourceTexts, string?
4151
// HotChocolate.Abstractions
4252
MetadataReference.CreateFromFile(typeof(ParentAttribute).Assembly.Location),
4353

54+
// HotChocolate.AspNetCore
55+
MetadataReference.CreateFromFile(
56+
typeof(HotChocolateAspNetCoreServiceCollectionExtensions).Assembly.Location),
57+
4458
// GreenDonut
4559
MetadataReference.CreateFromFile(typeof(DataLoaderBase<,>).Assembly.Location),
4660
MetadataReference.CreateFromFile(typeof(IDataLoader).Assembly.Location),
@@ -50,7 +64,13 @@ public static Snapshot GetGeneratedSourceSnapshot(string[] sourceTexts, string?
5064
MetadataReference.CreateFromFile(typeof(IPredicateBuilder).Assembly.Location),
5165

5266
// HotChocolate.Data
53-
MetadataReference.CreateFromFile(typeof(IFilterContext).Assembly.Location)
67+
MetadataReference.CreateFromFile(typeof(IFilterContext).Assembly.Location),
68+
69+
// Microsoft.AspNetCore
70+
MetadataReference.CreateFromFile(typeof(WebApplication).Assembly.Location),
71+
72+
// Microsoft.Extensions.DependencyInjection.Abstractions
73+
MetadataReference.CreateFromFile(typeof(IServiceCollection).Assembly.Location)
5474
];
5575

5676
// Create a Roslyn compilation for the syntax tree.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# GenerateSource_RequestMiddleware_MatchesSnapshot
2+
3+
```csharp
4+
// <auto-generated/>
5+
6+
#nullable enable
7+
#pragma warning disable
8+
9+
using System;
10+
using System.Runtime.CompilerServices;
11+
using HotChocolate;
12+
using HotChocolate.Types;
13+
using HotChocolate.Execution.Configuration;
14+
using Microsoft.Extensions.DependencyInjection;
15+
16+
namespace HotChocolate.Execution.Generated
17+
{
18+
public static class TestsTypesMiddlewareFactoriesHASH
19+
{
20+
// global::SomeRequestMiddleware
21+
private static global::HotChocolate.Execution.RequestCoreMiddleware CreateMiddleware0()
22+
=> (core, next) =>
23+
{
24+
var cp1 = core.SchemaServices.GetRequiredService<global::Service1>();
25+
var cp2 = core.SchemaServices.GetService<global::Service2>();
26+
var middleware = new global::SomeRequestMiddleware(next, cp1, cp2);
27+
return async context =>
28+
{
29+
var ip1 = context.Services.GetRequiredService<global::Service1>();
30+
var ip2 = context.Services.GetService<global::Service2>();
31+
await middleware.InvokeAsync(context, ip1, ip2).ConfigureAwait(false);
32+
};
33+
};
34+
35+
[InterceptsLocation("", 15, 14)]
36+
public static global::HotChocolate.Execution.Configuration.IRequestExecutorBuilder UseRequestGen0<TMiddleware>(
37+
this HotChocolate.Execution.Configuration.IRequestExecutorBuilder builder) where TMiddleware : class
38+
=> builder.UseRequest(CreateMiddleware0());
39+
}
40+
}
41+
42+
#pragma warning disable CS9113 // Parameter is unread.
43+
namespace System.Runtime.CompilerServices
44+
{
45+
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
46+
file sealed class InterceptsLocationAttribute(string filePath, int line, int column) : Attribute;
47+
}
48+
#pragma warning restore CS9113 // Parameter is unread.
49+
```

0 commit comments

Comments
 (0)