Skip to content

Commit

Permalink
[fix] use presence testing instead of rejecting default (googleapis#461)
Browse files Browse the repository at this point in the history
  • Loading branch information
viacheslav-rostovtsev authored May 7, 2021
1 parent d30609e commit 224dfc9
Showing 1 changed file with 5 additions and 38 deletions.
43 changes: 5 additions & 38 deletions Google.Api.Gax.Grpc/Rest/RestMethod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
Expand Down Expand Up @@ -115,8 +116,7 @@ private static Func<IMessage, TranscodingOutput> CreateTranscodingContentFactory
var path = pathFactory(message);
var body = bodyFactory(message);

var fieldsToEncode = unusedEligibleFields.Where(field =>
field.IsRequired || !IsDefaultValueForTranscoding(field, field.Accessor.GetValue(message)));
var fieldsToEncode = unusedEligibleFields.Where(field => !field.HasPresence || field.Accessor.HasValue(message));

var queryStringParams = fieldsToEncode.ToDictionary(field => field.JsonName,
field => field.Accessor.GetValue(message).ToString());
Expand All @@ -142,11 +142,13 @@ internal HttpRequestMessage CreateRequest(IMessage protoRequest, string host)

var uri = host is null ? new Uri(uriPathWithParams, UriKind.Relative) : new UriBuilder { Host = host, Path = uriPathWithParams }.Uri;

var content = transcodingResult.Body is null ? null : new StringContent(transcodingResult.Body, Encoding.UTF8, s_applicationJsonMediaType);

return new HttpRequestMessage
{
RequestUri = uri,
Method = _httpMethod,
Content = transcodingResult.Body is null ? null : new StringContent(transcodingResult.Body, Encoding.UTF8, s_applicationJsonMediaType),
Content = content,
};
}

Expand Down Expand Up @@ -174,41 +176,6 @@ private static string AppendQueryStringParameters(string uriPath, IOrderedEnumer

return sb.ToString();
}

private static bool IsDefaultValueForTranscoding(FieldDescriptor field, object fieldValue)
{
if (TypesIneligibleForQueryStringEncoding.Contains(field.FieldType))
throw new NotImplementedException($"Field {field.Name} in method service {field.MessageType.Name} has a type {field.FieldType} for which a default value comparison for GRPC Transcoding should not be performed since the type {field.FieldType} should not be transcoded.");

switch(field.FieldType)
{ case FieldType.Bool:
return (bool) fieldValue;
case FieldType.String:
return (string) fieldValue == "";
case FieldType.Enum:
return (int) fieldValue == 0;
case FieldType.Double:
return (double) fieldValue == 0;
case FieldType.Float:
return (float) fieldValue == 0F;
case FieldType.Int32:
case FieldType.SInt32:
case FieldType.SFixed32:
return (int) fieldValue == 0;
case FieldType.Int64:
case FieldType.SInt64:
case FieldType.SFixed64:
return (long) fieldValue == 0L;
case FieldType.UInt32:
case FieldType.Fixed32:
return (uint) fieldValue == 0U;
case FieldType.UInt64:
case FieldType.Fixed64:
return (ulong) fieldValue == 0UL;
default:
throw new ArgumentException($"Field {field.Name} in method service {field.MessageType.Name} has a type {field.FieldType} for which a default value comparison is not defined in the Gax");
}
}

// TODO: Handle cancellation?
/// <summary>
Expand Down

0 comments on commit 224dfc9

Please sign in to comment.