Skip to content

Commit 739f60f

Browse files
committed
Refactor type info resolver
1 parent 5f7b9f7 commit 739f60f

File tree

2 files changed

+29
-24
lines changed

2 files changed

+29
-24
lines changed

Diff for: src/Grpc/JsonTranscoding/src/Microsoft.AspNetCore.Grpc.JsonTranscoding/Internal/Json/JsonConverterHelper.cs

+6-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Text.Encodings.Web;
77
using System.Text.Json;
88
using System.Text.Json.Serialization;
9+
using System.Text.Json.Serialization.Metadata;
910
using Google.Protobuf;
1011
using Google.Protobuf.Collections;
1112
using Google.Protobuf.Reflection;
@@ -36,12 +37,16 @@ internal static JsonSerializerOptions CreateSerializerOptions(JsonContext contex
3637
// For streaming to work, indenting must be disabled when streaming.
3738
var writeIndented = !isStreamingOptions ? context.Settings.WriteIndented : false;
3839

40+
var typeInfoResolver = JsonTypeInfoResolver.Combine(
41+
new MessageTypeInfoResolver(context),
42+
new DefaultJsonTypeInfoResolver());
43+
3944
var options = new JsonSerializerOptions
4045
{
4146
WriteIndented = writeIndented,
4247
NumberHandling = JsonNumberHandling.AllowNamedFloatingPointLiterals,
4348
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
44-
TypeInfoResolver = new MessageTypeInfoResolver(context)
49+
TypeInfoResolver = typeInfoResolver
4550
};
4651
options.Converters.Add(new NullValueConverter());
4752
options.Converters.Add(new ByteStringConverter());

Diff for: src/Grpc/JsonTranscoding/src/Microsoft.AspNetCore.Grpc.JsonTranscoding/Internal/Json/MessageTypeInfoResolver.cs

+23-23
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,12 @@
77
using System.Text.Json.Serialization.Metadata;
88
using Google.Protobuf;
99
using Google.Protobuf.Reflection;
10-
using Google.Protobuf.WellKnownTypes;
1110
using Grpc.Shared;
1211
using Type = System.Type;
1312

1413
namespace Microsoft.AspNetCore.Grpc.JsonTranscoding.Internal.Json;
1514

16-
internal sealed class MessageTypeInfoResolver : DefaultJsonTypeInfoResolver
15+
internal sealed class MessageTypeInfoResolver : IJsonTypeInfoResolver
1716
{
1817
private readonly JsonContext _context;
1918

@@ -22,33 +21,34 @@ public MessageTypeInfoResolver(JsonContext context)
2221
_context = context;
2322
}
2423

25-
public override JsonTypeInfo GetTypeInfo(Type type, JsonSerializerOptions options)
24+
public JsonTypeInfo? GetTypeInfo(Type type, JsonSerializerOptions options)
2625
{
27-
var typeInfo = base.GetTypeInfo(type, options);
28-
29-
if (IsStandardMessage(type, out var messageDescriptor))
26+
if (!IsStandardMessage(type, out var messageDescriptor))
3027
{
31-
typeInfo.Properties.Clear();
28+
return null;
29+
}
3230

33-
var fields = messageDescriptor.Fields.InFieldNumberOrder();
34-
var mappings = CreateJsonFieldMap(fields);
31+
var typeInfo = JsonTypeInfo.CreateJsonTypeInfo(type, options);
32+
typeInfo.CreateObject = () => Activator.CreateInstance(type)!;
3533

36-
foreach (var field in fields)
37-
{
38-
mappings.Remove(field.JsonName);
34+
var fields = messageDescriptor.Fields.InFieldNumberOrder();
35+
var mappings = CreateJsonFieldMap(fields);
3936

40-
var propertyInfo = CreatePropertyInfo(typeInfo, field.JsonName, field, isWritable: true);
41-
typeInfo.Properties.Add(propertyInfo);
42-
}
37+
foreach (var field in fields)
38+
{
39+
mappings.Remove(field.JsonName);
4340

44-
// Fields have two mappings: the original field name and the camelcased JSON name.
45-
// The JSON name can also be customized in proto with json_name option.
46-
// Add extra setter only properties for mappings that haven't already been added.
47-
foreach (var mapping in mappings)
48-
{
49-
var propertyInfo = CreatePropertyInfo(typeInfo, mapping.Key, mapping.Value, isWritable: false);
50-
typeInfo.Properties.Add(propertyInfo);
51-
}
41+
var propertyInfo = CreatePropertyInfo(typeInfo, field.JsonName, field, isWritable: true);
42+
typeInfo.Properties.Add(propertyInfo);
43+
}
44+
45+
// Fields have two mappings: the original field name and the camelcased JSON name.
46+
// The JSON name can also be customized in proto with json_name option.
47+
// Add extra setter only properties for mappings that haven't already been added.
48+
foreach (var mapping in mappings)
49+
{
50+
var propertyInfo = CreatePropertyInfo(typeInfo, mapping.Key, mapping.Value, isWritable: false);
51+
typeInfo.Properties.Add(propertyInfo);
5252
}
5353

5454
return typeInfo;

0 commit comments

Comments
 (0)