Skip to content

Commit

Permalink
Merge pull request microsoft#1096 from Microsoft/johtaylo/serializati…
Browse files Browse the repository at this point in the history
…on-fix

change serialization settings so anonymous supported
  • Loading branch information
johnataylor authored Nov 9, 2018
2 parents b6b6b78 + dce56ed commit aaeb276
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 30 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using Microsoft.Bot.Connector;
using Newtonsoft.Json;

namespace Microsoft.Bot.Builder.Integration
{
public static class MessageSerializerSettings
{
public static JsonSerializerSettings Create()
{
var connector = new ConnectorClient(new Uri("http://localhost/"));
return connector.DeserializationSettings;
}
}
}
17 changes: 9 additions & 8 deletions libraries/Microsoft.Bot.Connector/ConnectorClientEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@
// Licensed under the MIT License.

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Reflection;
using System.Runtime.Versioning;
using Microsoft.Bot.Connector.Authentication;
using Microsoft.Rest;
using Newtonsoft.Json.Serialization;

namespace Microsoft.Bot.Connector
{
Expand Down Expand Up @@ -63,18 +60,22 @@ partial void CustomInitialize()
{
// The Schema version is 3.1, put into the Microsoft-BotFramework header
// https://github.com/Microsoft/botbuilder-dotnet/issues/471
this.HttpClient.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue("Microsoft-BotFramework", "3.1"));
HttpClient.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue("Microsoft-BotFramework", "3.1"));

// The Client SDK Version
// https://github.com/Microsoft/botbuilder-dotnet/blob/d342cd66d159a023ac435aec0fdf791f93118f5f/doc/UserAgents.md
this.HttpClient.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue("BotBuilder", GetClientVersion(this)));
HttpClient.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue("BotBuilder", GetClientVersion(this)));

// Additional Info.
// https://github.com/Microsoft/botbuilder-dotnet/blob/d342cd66d159a023ac435aec0fdf791f93118f5f/doc/UserAgents.md
var userAgent = $"({GetASPNetVersion()}; {GetOsVersion()}; {GetArchitecture()})";
this.HttpClient.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue(userAgent));
HttpClient.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue(userAgent));

this.HttpClient.DefaultRequestHeaders.ExpectContinue = false;
HttpClient.DefaultRequestHeaders.ExpectContinue = false;

// Override the contract resolver with the Default because we want to be able to serialize annonymous types
SerializationSettings.ContractResolver = new DefaultContractResolver();
DeserializationSettings.ContractResolver = new DefaultContractResolver();
}

/// <summary>Gets a description of the operating system of the Azure Bot Service.</summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,20 @@
// Licensed under the MIT License.

using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Net.Http.Headers;
using Microsoft.Rest.Serialization;
using Newtonsoft.Json;

namespace Microsoft.Bot.Builder.Integration.AspNet.Core.Handlers
{
public abstract class BotMessageHandlerBase
{
public static readonly JsonSerializer BotMessageSerializer = JsonSerializer.Create(new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore,
Formatting = Formatting.Indented,
DateFormatHandling = DateFormatHandling.IsoDateFormat,
DateTimeZoneHandling = DateTimeZoneHandling.Utc,
ReferenceLoopHandling = ReferenceLoopHandling.Serialize,
ContractResolver = new ReadOnlyJsonContractResolver(),
Converters = new List<JsonConverter> { new Iso8601TimeSpanConverter() },
});
public static readonly JsonSerializer BotMessageSerializer = JsonSerializer.Create(MessageSerializerSettings.Create());

public BotMessageHandlerBase()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Threading.Tasks;
using Microsoft.Rest.Serialization;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;

namespace Microsoft.Bot.Builder.Integration.AspNet.WebApi.Handlers
{
Expand All @@ -19,16 +20,7 @@ public abstract class BotMessageHandlerBase : HttpMessageHandler
{
new JsonMediaTypeFormatter
{
SerializerSettings =
{
NullValueHandling = NullValueHandling.Ignore,
Formatting = Formatting.Indented,
DateFormatHandling = DateFormatHandling.IsoDateFormat,
DateTimeZoneHandling = DateTimeZoneHandling.Utc,
ReferenceLoopHandling = ReferenceLoopHandling.Serialize,
ContractResolver = new ReadOnlyJsonContractResolver(),
Converters = new List<JsonConverter> { new Iso8601TimeSpanConverter() },
},
SerializerSettings = MessageSerializerSettings.Create(),
SupportedMediaTypes =
{
new System.Net.Http.Headers.MediaTypeHeaderValue("application/json") { CharSet = "utf-8" },
Expand Down
31 changes: 31 additions & 0 deletions tests/Microsoft.Bot.Builder.Tests/Integration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using Microsoft.Bot.Builder.Integration;
using Microsoft.Bot.Connector;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json.Serialization;

namespace Microsoft.Bot.Builder.Tests
{
[TestClass]
[TestCategory("BotAdapter")]

public class Integration
{
[TestMethod]
public void CheckSerializerSettings()
{
// used in the integration layer
var settings = MessageSerializerSettings.Create();

// connector exposes the serializer settings it uses
var connector = new ConnectorClient(new Uri("http://localhost/"));

Assert.IsInstanceOfType(settings.ContractResolver, typeof(DefaultContractResolver));
Assert.IsInstanceOfType(connector.DeserializationSettings.ContractResolver, typeof(DefaultContractResolver));
Assert.IsInstanceOfType(connector.SerializationSettings.ContractResolver, typeof(DefaultContractResolver));
}
}
}

0 comments on commit aaeb276

Please sign in to comment.