Skip to content

Commit

Permalink
fix: Propagate properties in FirestoreDbBuilder when using the emulator
Browse files Browse the repository at this point in the history
Fixes #6313
  • Loading branch information
jskeet committed Apr 6, 2021
1 parent 3297fe1 commit dc62cbc
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using Google.Api.Gax;
using System;
using System.Threading.Tasks;
using Xunit;

Expand All @@ -36,5 +38,37 @@ public async Task TimestampTruncated()
var storedData = snapshot.GetValue<Timestamp>("Timestamp");
Assert.Equal(new Timestamp(100, 123456000), storedData);
}

[Fact]
public async Task ConverterRegistry()
{
var db = new FirestoreDbBuilder
{
ProjectId = _fixture.ProjectId,
EmulatorDetection = EmulatorDetection.EmulatorOrProduction,
ConverterRegistry = new ConverterRegistry { new GuidConverter() }
}.Build();
CollectionReference collection = db.Collection("guids");
Guid guid = Guid.NewGuid();
DocumentReference document = await collection.AddAsync(new { Value = guid });
DocumentSnapshot snapshot = await document.GetSnapshotAsync();
Guid fetchedGuid = snapshot.GetValue<Guid>("Value");
Assert.Equal(fetchedGuid, guid);
}

public class GuidConverter : IFirestoreConverter<Guid>
{
public object ToFirestore(Guid value) => value.ToString("N");

public Guid FromFirestore(object value)
{
switch (value)
{
case string guid: return Guid.ParseExact(guid, "N");
case null: throw new ArgumentNullException(nameof(value));
default: throw new ArgumentException($"Unexpected data: {value.GetType()}");
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,15 @@ private FirestoreDbBuilder MaybeUseEmulator()
// copied our existing value in here, we'd recurse infinitely (until we overflowed the stack).
return new FirestoreDbBuilder
{
// Properties used to build the FirestoreClient
Endpoint = hostAndPort,
Settings = settings,
ChannelCredentials = ChannelCredentials.Insecure,
// FirestoreDb-specific properties
ProjectId = ProjectId,
ChannelCredentials = ChannelCredentials.Insecure
DatabaseId = DatabaseId,
ConverterRegistry = ConverterRegistry,
WarningLogger = WarningLogger
};
}
}
Expand Down

0 comments on commit dc62cbc

Please sign in to comment.