Skip to content

Commit

Permalink
Adjust serializer selection fallback procedure (#2147)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
softworkz committed Oct 24, 2023
1 parent 6880e58 commit d99d494
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions src/RestSharp/Serializers/RestSerializers.cs
Expand Up @@ -87,13 +87,24 @@ public IRestSerializer GetSerializer(DataFormat dataFormat)
if (string.IsNullOrWhiteSpace(response.Content)) return null;

var contentType = response.ContentType ?? DetectContentType()?.Value;
if (contentType == null) return null;

if (contentType == null) {
Serializers.TryGetValue(response.Request.RequestFormat, out var serializerByRequestFormat);
return serializerByRequestFormat?.GetSerializer().Deserializer;
}

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

var factory = serializer ??
(Serializers.ContainsKey(response.Request.RequestFormat) ? Serializers[response.Request.RequestFormat] : null);
return factory?.GetSerializer().Deserializer;
if (serializer == null) {
var detectedType = DetectContentType()?.Value;

if (detectedType != null && detectedType != contentType)
{
serializer = Serializers.Values.FirstOrDefault(x => x.SupportsContentType(detectedType));
}
}

return serializer?.GetSerializer().Deserializer;

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

0 comments on commit d99d494

Please sign in to comment.