Skip to content

Commit 68545a0

Browse files
authored
RDF test migration for some response test cases. (#48611)
1 parent b07bb1a commit 68545a0

File tree

40 files changed

+1023
-407
lines changed

40 files changed

+1023
-407
lines changed

src/Http/Http.Extensions/gen/StaticRouteHandlerModel/Emitters/EndpointJsonResponseEmitter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ internal static void EmitJsonPreparation(this EndpointResponse endpointResponse,
99
{
1010
if (endpointResponse.IsSerializableJsonResponse(out var responseType))
1111
{
12-
var typeName = responseType.ToDisplayString(EmitterConstants.DisplayFormat);
12+
var typeName = responseType.ToDisplayString(EmitterConstants.DisplayFormatWithoutNullability);
1313

1414
codeWriter.WriteLine("var serializerOptions = serviceProvider?.GetService<IOptions<JsonOptions>>()?.Value.SerializerOptions ?? new JsonOptions().SerializerOptions;");
1515
codeWriter.WriteLine($"var jsonTypeInfo = (JsonTypeInfo<{typeName}>)serializerOptions.GetTypeInfo(typeof({typeName}));");

src/Http/Http.Extensions/gen/StaticRouteHandlerModel/EndpointResponse.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System;
5-
using System.Linq;
65
using Microsoft.AspNetCore.Analyzers.RouteEmbeddedLanguage.Infrastructure;
76
using Microsoft.AspNetCore.App.Analyzers.Infrastructure;
87
using Microsoft.AspNetCore.Http.RequestDelegateGenerator.StaticRouteHandlerModel.Emitters;
@@ -34,7 +33,7 @@ internal EndpointResponse(IMethodSymbol method, WellKnownTypes wellKnownTypes)
3433
HasNoResponse = method.ReturnsVoid || awaitableIsVoid;
3534
IsIResult = GetIsIResult();
3635
IsSerializable = GetIsSerializable();
37-
ContentType = GetContentType(method);
36+
ContentType = GetContentType();
3837
IsAnonymousType = method.ReturnType.IsAnonymousType;
3938
IsEndpointMetadataProvider = ImplementsIEndpointMetadataProvider(ResponseType, wellKnownTypes);
4039
}
@@ -84,18 +83,19 @@ private bool GetIsIResult()
8483
SymbolEqualityComparer.Default.Equals(ResponseType, resultType);
8584
}
8685

87-
private string? GetContentType(IMethodSymbol method)
86+
private string? GetContentType()
8887
{
8988
// `void` returning methods do not have a Content-Type.
9089
// We don't have a strategy for resolving a Content-Type
9190
// from an IResult. Typically, this would be done via an
9291
// IEndpointMetadataProvider so we don't need to set a
9392
// Content-Type here.
94-
if (method.ReturnsVoid || IsIResult)
93+
if (IsIResult || HasNoResponse)
9594
{
9695
return null;
9796
}
98-
return method.ReturnType.SpecialType is SpecialType.System_String ? "text/plain" : "application/json";
97+
98+
return ResponseType!.SpecialType is SpecialType.System_String ? "text/plain; charset=utf-8" : "application/json";
9999
}
100100

101101
public override bool Equals(object obj)

src/Http/Http.Extensions/gen/StaticRouteHandlerModel/StaticRouteHandlerModel.Emitter.cs

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,6 @@ public static void EmitRequestHandler(this Endpoint endpoint, CodeWriter codeWri
7676
{
7777
return;
7878
}
79-
if (!endpoint.Response.HasNoResponse && endpoint.Response is { ContentType: { } contentType })
80-
{
81-
codeWriter.WriteLine($@"httpContext.Response.ContentType ??= ""{contentType}"";");
82-
}
8379
if (!endpoint.Response.HasNoResponse)
8480
{
8581
codeWriter.Write("var result = ");
@@ -89,6 +85,9 @@ public static void EmitRequestHandler(this Endpoint endpoint, CodeWriter codeWri
8985
codeWriter.Write("await ");
9086
}
9187
codeWriter.WriteLine($"handler({endpoint.EmitArgumentList()});");
88+
89+
endpoint.Response.EmitHttpResponseContentType(codeWriter);
90+
9291
if (!endpoint.Response.HasNoResponse)
9392
{
9493
codeWriter.WriteLine(endpoint.Response.EmitResponseWritingCall(endpoint.IsAwaitable));
@@ -97,9 +96,27 @@ public static void EmitRequestHandler(this Endpoint endpoint, CodeWriter codeWri
9796
{
9897
codeWriter.WriteLine("return Task.CompletedTask;");
9998
}
99+
100100
codeWriter.EndBlock(); // End handler method block
101101
}
102102

103+
private static void EmitHttpResponseContentType(this EndpointResponse endpointResponse, CodeWriter codeWriter)
104+
{
105+
if (!endpointResponse.HasNoResponse
106+
&& endpointResponse.ResponseType is { } responseType
107+
&& (responseType.SpecialType == SpecialType.System_Object || responseType.SpecialType == SpecialType.System_String))
108+
{
109+
codeWriter.WriteLine("if (result is string)");
110+
codeWriter.StartBlock();
111+
codeWriter.WriteLine($@"httpContext.Response.ContentType ??= ""text/plain; charset=utf-8"";");
112+
codeWriter.EndBlock();
113+
codeWriter.WriteLine("else");
114+
codeWriter.StartBlock();
115+
codeWriter.WriteLine($@"httpContext.Response.ContentType ??= ""application/json; charset=utf-8"";");
116+
codeWriter.EndBlock();
117+
}
118+
}
119+
103120
private static string EmitResponseWritingCall(this EndpointResponse endpointResponse, bool isAwaitable)
104121
{
105122
var returnOrAwait = isAwaitable ? "await" : "return";
@@ -183,7 +200,7 @@ private static void EmitBuiltinResponseTypeMetadata(this Endpoint endpoint, Code
183200
}
184201
else
185202
{
186-
codeWriter.WriteLine($"options.EndpointBuilder.Metadata.Add(new GeneratedProducesResponseTypeMetadata(type: typeof({responseType.ToDisplayString(EmitterConstants.DisplayFormat)}), statusCode: StatusCodes.Status200OK, contentTypes: GeneratedMetadataConstants.JsonContentType));");
203+
codeWriter.WriteLine($"options.EndpointBuilder.Metadata.Add(new GeneratedProducesResponseTypeMetadata(type: typeof({responseType.ToDisplayString(EmitterConstants.DisplayFormatWithoutNullability)}), statusCode: StatusCodes.Status200OK, contentTypes: GeneratedMetadataConstants.JsonContentType));");
187204
}
188205
}
189206

0 commit comments

Comments
 (0)