Skip to content

Commit d99d494

Browse files
authored
Adjust serializer selection fallback procedure (#2147)
In summary: 1. Choose by request type only when the server didn't provide a content type and it wasn't possible to detect it 2. If the server did provide a content type but we don't have a deserializer for it, try detection as a fallback
1 parent 6880e58 commit d99d494

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

Diff for: src/RestSharp/Serializers/RestSerializers.cs

+15-4
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,24 @@ internal RestResponse<T> Deserialize<T>(RestRequest request, RestResponse raw, R
8787
if (string.IsNullOrWhiteSpace(response.Content)) return null;
8888

8989
var contentType = response.ContentType ?? DetectContentType()?.Value;
90-
if (contentType == null) return null;
90+
91+
if (contentType == null) {
92+
Serializers.TryGetValue(response.Request.RequestFormat, out var serializerByRequestFormat);
93+
return serializerByRequestFormat?.GetSerializer().Deserializer;
94+
}
9195

9296
var serializer = Serializers.Values.FirstOrDefault(x => x.SupportsContentType(contentType));
9397

94-
var factory = serializer ??
95-
(Serializers.ContainsKey(response.Request.RequestFormat) ? Serializers[response.Request.RequestFormat] : null);
96-
return factory?.GetSerializer().Deserializer;
98+
if (serializer == null) {
99+
var detectedType = DetectContentType()?.Value;
100+
101+
if (detectedType != null && detectedType != contentType)
102+
{
103+
serializer = Serializers.Values.FirstOrDefault(x => x.SupportsContentType(detectedType));
104+
}
105+
}
106+
107+
return serializer?.GetSerializer().Deserializer;
97108

98109
ContentType? DetectContentType()
99110
=> response.Content![0] switch {

0 commit comments

Comments
 (0)