@@ -11,7 +11,7 @@ internal static void EmitJsonPreparation(this EndpointResponse endpointResponse,
11
11
{
12
12
if ( endpointResponse . IsSerializableJsonResponse ( out var responseType ) )
13
13
{
14
- var typeName = responseType . ToDisplayString ( EmitterConstants . DisplayFormat ) ;
14
+ var typeName = responseType . ToDisplayString ( EmitterConstants . DisplayFormatWithoutNullability ) ;
15
15
16
16
codeWriter . WriteLine ( "var serializerOptions = serviceProvider?.GetService<IOptions<JsonOptions>>()?.Value.SerializerOptions ?? new JsonOptions().SerializerOptions;" ) ;
17
17
codeWriter . WriteLine ( $ "var jsonTypeInfo = (JsonTypeInfo<{ typeName } >)serializerOptions.GetTypeInfo(typeof({ typeName } ));") ;
@@ -73,7 +73,7 @@ internal static void EmitBuiltinResponseTypeMetadata(this Endpoint endpoint, Cod
73
73
}
74
74
else
75
75
{
76
- codeWriter . WriteLine ( $ "options.EndpointBuilder.Metadata.Add(new GeneratedProducesResponseTypeMetadata(type: typeof({ responseType . ToDisplayString ( EmitterConstants . DisplayFormat ) } ), statusCode: StatusCodes.Status200OK, contentTypes: GeneratedMetadataConstants.JsonContentType));") ;
76
+ codeWriter . WriteLine ( $ "options.EndpointBuilder.Metadata.Add(new GeneratedProducesResponseTypeMetadata(type: typeof({ responseType . ToDisplayString ( EmitterConstants . DisplayFormatWithoutNullability ) } ), statusCode: StatusCodes.Status200OK, contentTypes: GeneratedMetadataConstants.JsonContentType));") ;
77
77
}
78
78
}
79
79
@@ -89,4 +89,21 @@ internal static void EmitCallToMetadataProviderForResponse(this Endpoint endpoin
89
89
codeWriter . WriteLine ( $ "PopulateMetadataForEndpoint<{ responseType . ToDisplayString ( EmitterConstants . DisplayFormat ) } >(methodInfo, options.EndpointBuilder);") ;
90
90
}
91
91
}
92
+
93
+ internal static void EmitHttpResponseContentType ( this EndpointResponse endpointResponse , CodeWriter codeWriter )
94
+ {
95
+ if ( ! endpointResponse . HasNoResponse
96
+ && endpointResponse . ResponseType is { } responseType
97
+ && ( responseType . SpecialType == SpecialType . System_Object || responseType . SpecialType == SpecialType . System_String ) )
98
+ {
99
+ codeWriter . WriteLine ( "if (result is string)" ) ;
100
+ codeWriter . StartBlock ( ) ;
101
+ codeWriter . WriteLine ( $@ "httpContext.Response.ContentType ??= ""text/plain; charset=utf-8"";") ;
102
+ codeWriter . EndBlock ( ) ;
103
+ codeWriter . WriteLine ( "else" ) ;
104
+ codeWriter . StartBlock ( ) ;
105
+ codeWriter . WriteLine ( $@ "httpContext.Response.ContentType ??= ""application/json; charset=utf-8"";") ;
106
+ codeWriter . EndBlock ( ) ;
107
+ }
108
+ }
92
109
}
0 commit comments