Skip to content

Commit

Permalink
Fix DateTime bug (Azure#45818)
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshLove-msft authored Sep 5, 2024
1 parent 29f16b2 commit 15de258
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
# Release History

## 3.5.0-beta.1 (Unreleased)

### Features Added

### Breaking Changes
## 3.4.3 (2024-09-10)

### Bugs Fixed

### Other Changes
- Prevent DateTime values from being translated by NewtonSoft when parsing payloads. They are now treated as strings.

## 3.4.2 (2024-07-30)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFrameworks>$(RequiredTargetFrameworks)</TargetFrameworks>
<Description>This extension adds bindings for EventGrid</Description>
<Version>3.5.0-beta.1</Version>
<Version>3.4.3</Version>
<!--The ApiCompatVersion is managed automatically and should not generally be modified manually.-->
<ApiCompatVersion>3.4.2</ApiCompatVersion>
<NoWarn>$(NoWarn);CS1591</NoWarn>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Azure.Messaging.EventGrid.SystemEvents;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using STJ = System.Text.Json;

Expand Down Expand Up @@ -104,7 +106,11 @@ internal async Task<HttpResponseMessage> ProcessAsync(
if (string.Equals(eventTypeHeader, NotificationEvent, StringComparison.OrdinalIgnoreCase))
{
string requestContent = await req.Content.ReadAsStringAsync().ConfigureAwait(false);
JToken token = JToken.Parse(requestContent);
JToken token;
using (JsonReader reader = new JsonTextReader(new StringReader(requestContent)) { DateParseHandling = DateParseHandling.None })
{
token = JToken.Load(reader);
}
JArray events = token.Type switch
{
JTokenType.Array => (JArray) token,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
using Azure.Messaging.EventGrid;
using Microsoft.Azure.WebJobs.Extensions.EventGrid.Config;
using Microsoft.Azure.WebJobs.Host.Config;
using Microsoft.Extensions.Azure;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
Expand All @@ -23,6 +21,7 @@
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace Microsoft.Azure.WebJobs.Extensions.EventGrid.Tests
{
Expand Down Expand Up @@ -173,14 +172,22 @@ public async Task TestCloudEvent()
var ext = host.Services.GetServices<IExtensionConfigProvider>().OfType<EventGridExtensionConfigProvider>().Single();
await host.StartAsync(); // add listener

var request = CreateSingleRequest("TestCloudEvent",
JObject.Parse(@"{'id':'one','source':'one','type':'t','data':'','specversion':'1.0','data':{'prop':'alpha'}}"));
var dateTime = "2024-08-15T12:34:56.0000000-08:00";
var jObject = JsonConvert.DeserializeObject<JObject>(
$"{{'id':'one', 'time':'{dateTime}', 'source':'one','type':'t','data':'','specversion':'1.0','data':{{'prop':'alpha'}}}}",
new JsonSerializerSettings { DateParseHandling = DateParseHandling.None });
var request = CreateSingleRequest("TestCloudEvent", jObject);
var response = await ext.ConvertAsync(request, CancellationToken.None);

// verifies each instance gets its own proper binding data (from FakePayload.Prop)
_log.TryGetValue("one", out string alpha);
Assert.AreEqual("alpha", alpha);
Assert.AreEqual(1, _log.Count);

// validate that the time matches exactly - i.e. it should not have been translated to UTC or local time of the machine
_log.TryGetValue("time", out string time);
Assert.AreEqual(dateTime, time);

Assert.AreEqual(2, _log.Count);
Assert.AreEqual(HttpStatusCode.Accepted, response.StatusCode);
}

Expand Down Expand Up @@ -552,6 +559,11 @@ public void RunSingle(
{
throw new InvalidOperationException($"duplicate subject '{value.Id}'");
}

if (!_log.TryAdd("time", value.Time.Value.ToString("o")))
{
throw new InvalidOperationException($"duplicate time '{value.Time}'");
}
}

[FunctionName("TestCloudEventMultiple")]
Expand Down

0 comments on commit 15de258

Please sign in to comment.