Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update JSON.NET code to work with Unity. #2428

Merged
merged 4 commits into from
Mar 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@
</PropertyGroup>
<PropertyGroup Condition="'$(TargetFramework)' == '$(TargetFrameworkNetStandard)'">
<!-- These are for MSAL json/* build infra -->
<DefineConstants>$(DefineConstants);PORTABLE;HAVE_ASYNC;HAVE_BIG_INTEGER;HAVE_BINARY_SERIALIZATION;HAVE_COVARIANT_GENERICS;HAVE_DATA_CONTRACTS;HAVE_DATE_TIME_OFFSET;HAVE_DYNAMIC;HAVE_EXPRESSIONS;HAVE_FSHARP_TYPES;HAVE_GUID_TRY_PARSE;HAVE_HASH_SET;HAVE_IGNORE_DATA_MEMBER_ATTRIBUTE;HAVE_INOTIFY_COLLECTION_CHANGED;HAVE_INOTIFY_PROPERTY_CHANGING;HAVE_ISET;HAVE_LINQ;HAVE_METHOD_IMPL_ATTRIBUTE;HAVE_NON_SERIALIZED_ATTRIBUTE;HAVE_READ_ONLY_COLLECTIONS;HAVE_REFLECTION_BINDER;HAVE_SERIALIZATION_BINDER_BIND_TO_NAME;HAVE_STRING_JOIN_WITH_ENUMERABLE;HAVE_TIME_SPAN_PARSE_WITH_CULTURE;HAVE_TIME_SPAN_TO_STRING_WITH_CULTURE;HAVE_TIME_ZONE_INFO;HAVE_TYPE_DESCRIPTOR;HAVE_UNICODE_SURROGATE_DETECTION;HAVE_VARIANT_TYPE_PARAMETERS;HAVE_VERSION_TRY_PARSE;HAVE_XLINQ;HAVE_OBSOLETE_FORMATTER_ASSEMBLY_STYLE;HAVE_XML_DOCUMENT;HAVE_CONCURRENT_DICTIONARY;HAVE_ICONVERTIBLE;$(AdditionalConstants)</DefineConstants>
<DefineConstants>$(DefineConstants);UNITY;PORTABLE;HAVE_ASYNC;HAVE_BIG_INTEGER;HAVE_BINARY_SERIALIZATION;HAVE_COVARIANT_GENERICS;HAVE_DATA_CONTRACTS;HAVE_DATE_TIME_OFFSET;HAVE_DYNAMIC;HAVE_EXPRESSIONS;HAVE_FSHARP_TYPES;HAVE_GUID_TRY_PARSE;HAVE_HASH_SET;HAVE_IGNORE_DATA_MEMBER_ATTRIBUTE;HAVE_INOTIFY_COLLECTION_CHANGED;HAVE_INOTIFY_PROPERTY_CHANGING;HAVE_ISET;HAVE_LINQ;HAVE_METHOD_IMPL_ATTRIBUTE;HAVE_NON_SERIALIZED_ATTRIBUTE;HAVE_READ_ONLY_COLLECTIONS;HAVE_REFLECTION_BINDER;HAVE_SERIALIZATION_BINDER_BIND_TO_NAME;HAVE_STRING_JOIN_WITH_ENUMERABLE;HAVE_TIME_SPAN_PARSE_WITH_CULTURE;HAVE_TIME_SPAN_TO_STRING_WITH_CULTURE;HAVE_TIME_ZONE_INFO;HAVE_TYPE_DESCRIPTOR;HAVE_UNICODE_SURROGATE_DETECTION;HAVE_VARIANT_TYPE_PARAMETERS;HAVE_VERSION_TRY_PARSE;HAVE_XLINQ;HAVE_OBSOLETE_FORMATTER_ASSEMBLY_STYLE;HAVE_XML_DOCUMENT;HAVE_CONCURRENT_DICTIONARY;HAVE_ICONVERTIBLE;$(AdditionalConstants)</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="'$(APPCENTER_BUILD)' != ''">
<DefineConstants>$(DefineConstants);IS_APPCENTER_BUILD</DefineConstants>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1379,7 +1379,7 @@ protected virtual IValueProvider CreateMemberValueProvider(MemberInfo member)
// warning - this method use to cause errors with Intellitrace. Retest in VS Ultimate after changes
IValueProvider valueProvider;

#if !(PORTABLE40 || PORTABLE || DOTNET || ANDROID || iOS || MAC)
#if !(PORTABLE40 || PORTABLE || DOTNET || ANDROID || iOS || MAC || UNITY)
if (DynamicCodeGeneration)
{
valueProvider = new DynamicValueProvider(member);
Expand All @@ -1388,7 +1388,7 @@ protected virtual IValueProvider CreateMemberValueProvider(MemberInfo member)
{
valueProvider = new ReflectionValueProvider(member);
}
#elif !PORTABLE40
#elif !(PORTABLE40 || UNITY)
valueProvider = new ExpressionValueProvider(member);
#else
valueProvider = new ReflectionValueProvider(member);
Expand Down Expand Up @@ -1704,4 +1704,4 @@ public string GetResolvedPropertyName(string propertyName)
return ResolvePropertyName(propertyName);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
// OTHER DEALINGS IN THE SOFTWARE.
#endregion

#if !(NET20 || NET35)
#if !(NET20 || NET35 || UNITY)

using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -118,4 +118,4 @@ public object GetValue(object target)
}
}

#endif
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -515,17 +515,20 @@ public static ReflectionDelegateFactory ReflectionDelegateFactory
{
get
{
#if !(PORTABLE40 || PORTABLE || DOTNET || ANDROID || iOS || MAC)
#if !(PORTABLE40 || PORTABLE || DOTNET || ANDROID || iOS || MAC || UNITY)
if (DynamicCodeGeneration)
{
return DynamicReflectionDelegateFactory.Instance;
}

return LateBoundReflectionDelegateFactory.Instance;

#elif UNITY
return LateBoundReflectionDelegateFactory.Instance;
#else
return ExpressionReflectionDelegateFactory.Instance;
return ExpressionReflectionDelegateFactory.Instance;
#endif
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
// OTHER DEALINGS IN THE SOFTWARE.
#endregion

#if !(NET20 || NET35)
#if !(NET20 || NET35 || UNITY)

using System.Collections.Generic;
using System.Linq;
Expand Down Expand Up @@ -380,4 +380,4 @@ private Expression EnsureCastExpression(Expression expression, Type targetType,
}
}

#endif
#endif
123 changes: 123 additions & 0 deletions tests/Microsoft.Identity.Test.Performance/JsonTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Configs;
using Microsoft.Identity.Client.Instance.Discovery;
using Microsoft.Identity.Client.OAuth2;
using Microsoft.Identity.Client.Utils;
using Microsoft.Identity.Test.Unit;

namespace Microsoft.Identity.Test.Performance
{
public class JsonTests
{
private readonly string _msalTokenResponseJson;
private readonly MsalTokenResponse _msalTokenResponse;

private readonly string _instanceDiscoveryResponseJson;
private readonly InstanceDiscoveryResponse _instanceDiscoveryResponse;

private readonly string _oAuth2ResponseBaseJson;
private readonly OAuth2ResponseBase _oAuth2ResponseBase;

public JsonTests()
{
_oAuth2ResponseBase = InitOAuth2ResponseBase(new OAuth2ResponseBase());
_oAuth2ResponseBaseJson = JsonHelper.SerializeToJson<OAuth2ResponseBase>(_oAuth2ResponseBase);

_msalTokenResponse = InitMsalTokenResponse(new MsalTokenResponse());
_msalTokenResponseJson = JsonHelper.SerializeToJson<MsalTokenResponse>(_msalTokenResponse);

_instanceDiscoveryResponse = InitInstanceDiscoveryResponse(new InstanceDiscoveryResponse());
_instanceDiscoveryResponseJson = JsonHelper.SerializeToJson<InstanceDiscoveryResponse>(_instanceDiscoveryResponse);
}

private InstanceDiscoveryResponse InitInstanceDiscoveryResponse(InstanceDiscoveryResponse instanceDiscoveryResponse)
{
int entries = 30;
instanceDiscoveryResponse.TenantDiscoveryEndpoint = TestConstants.DiscoveryEndPoint;
instanceDiscoveryResponse.Metadata = new InstanceDiscoveryMetadataEntry[entries];

InitOAuth2ResponseBase(instanceDiscoveryResponse);

for (int i = 0; i < entries; i++)
{
instanceDiscoveryResponse.Metadata[i] = new InstanceDiscoveryMetadataEntry
{
Aliases = new[] { "login.windows.net", "login.microsoftonline.com" },
PreferredCache = "login.windows.net",
PreferredNetwork = "login.microsoftonline.com"
};
}

return instanceDiscoveryResponse;
}

private OAuth2ResponseBase InitOAuth2ResponseBase(OAuth2ResponseBase oAuth2ResponseBase)
{
oAuth2ResponseBase.Error = "OAuth error";
oAuth2ResponseBase.SubError = "OAuth suberror";
oAuth2ResponseBase.ErrorDescription = "OAuth error description";
oAuth2ResponseBase.ErrorCodes = new[] { "error1", "error2", "error3" };
oAuth2ResponseBase.CorrelationId = "1234-123-1234";
oAuth2ResponseBase.Claims = "claim1 claim2";

return oAuth2ResponseBase;
}

private MsalTokenResponse InitMsalTokenResponse(MsalTokenResponse msalTokenResponse)
{
msalTokenResponse.TokenType = "token type";
msalTokenResponse.AccessToken = "access token";
msalTokenResponse.RefreshToken = "refresh token";
msalTokenResponse.Scope = "scope scope";
msalTokenResponse.ClientInfo = "client info";
msalTokenResponse.IdToken = "id token";
msalTokenResponse.ExpiresIn = 123;
msalTokenResponse.ExtendedExpiresIn = 12345;
msalTokenResponse.RefreshIn = 12333;
msalTokenResponse.FamilyId = "family id";

InitOAuth2ResponseBase(msalTokenResponse);

return msalTokenResponse;
}

[Benchmark]
public string Serialize_MsalTokenResponse_Test()
{
return JsonHelper.SerializeToJson<MsalTokenResponse>(_msalTokenResponse);
}

[Benchmark]
public string Deserialize_MsalTokenResponse_Test()
{
return JsonHelper.DeserializeFromJson<MsalTokenResponse>(_msalTokenResponseJson).CorrelationId;
}

[Benchmark]
public string Serialize_InstanceDiscoveryResponse_Test()
{
return JsonHelper.SerializeToJson<InstanceDiscoveryResponse>(_instanceDiscoveryResponse);
}

[Benchmark]
public string Deserialize_InstanceDiscoveryResponse_Test()
{
return JsonHelper.DeserializeFromJson<InstanceDiscoveryResponse>(_instanceDiscoveryResponseJson).CorrelationId;
}

[Benchmark]
public string Serialize_OAuth2ResponseBase_Test()
{
return JsonHelper.SerializeToJson<OAuth2ResponseBase>(_oAuth2ResponseBase);
}

[Benchmark]
public string Deserialize_OAuth2ResponseBase_Test()
{
return JsonHelper.DeserializeFromJson<OAuth2ResponseBase>(_oAuth2ResponseBaseJson).CorrelationId;
}
}
}
2 changes: 1 addition & 1 deletion tests/Microsoft.Identity.Test.Performance/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Program
{
static void Main(string[] args)
{
BenchmarkRunner.Run<CryptoManagerTests>(
BenchmarkRunner.Run<JsonTests>(
DefaultConfig.Instance
.WithOptions(ConfigOptions.DontOverwriteResults)
.AddJob(
Expand Down