Skip to content

Commit

Permalink
Merge pull request #632 from carolynvs/parse-token-without-culture
Browse files Browse the repository at this point in the history
  • Loading branch information
carolynvs committed Sep 20, 2016
2 parents 683932f + f3fb8f1 commit 7f10530
Show file tree
Hide file tree
Showing 15 changed files with 64 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public ServiceOperationFailedException(IEnumerable<ServiceError> errors)
/// <param name="context">The <see cref="T:System.Runtime.Serialization.StreamingContext" /> that contains contextual information about the source or destination.</param>
private ServiceOperationFailedException(SerializationInfo info, StreamingContext context) : base(info, context)
{
Errors = OpenStackNet.Configuration.FlurlHttpSettings.JsonSerializer.Deserialize<IEnumerable<ServiceError>>(info.GetString("service_errors"));
Errors = OpenStackNet.Deserialize<IEnumerable<ServiceError>>(info.GetString("service_errors"));
}

/// <summary>
Expand All @@ -48,7 +48,7 @@ private ServiceOperationFailedException(SerializationInfo info, StreamingContext
public override void GetObjectData(SerializationInfo info, StreamingContext context)
{
// ReSharper disable once ExceptionNotDocumented
info.AddValue("service_errors", OpenStackNet.Configuration.FlurlHttpSettings.JsonSerializer.Serialize(Errors));
info.AddValue("service_errors", OpenStackNet.Serialize(Errors));
base.GetObjectData(info, context);
}
}
Expand Down
8 changes: 5 additions & 3 deletions src/corelib/Core/Providers/OpenStackIdentityProvider.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using Newtonsoft.Json;
using OpenStack;
using OpenStack.Authentication;

Expand Down Expand Up @@ -109,11 +110,12 @@ public override UserAccess GetUserAccess(CloudIdentity identity, bool forceCache
if (response == null || response.Data == null)
return null;

JToken userAccessObject = response.Data["access"];
if (userAccessObject == null)
// The defalut json serialization is helpfully formatting the expires date string. Use our custom serializer for this part to prevent chaos of timezone proportions.
var rawJson = response.Data["access"]?.ToString(Formatting.None);
if (rawJson == null)
return null;

UserAccess access = userAccessObject.ToObject<UserAccess>();
UserAccess access = OpenStackNet.Deserialize<UserAccess>(rawJson);
if (access == null || access.Token == null)
return null;

Expand Down
20 changes: 20 additions & 0 deletions src/corelib/OpenStackNet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,26 @@ public static void ResetDefaults()
}
}

/// <summary>
/// Deserializes an object from a json string representation.
/// </summary>
/// <typeparam name="T">The object type.</typeparam>
/// <param name="json">The json string.</param>
public static T Deserialize<T>(string json)
{
return Configuration.FlurlHttpSettings.JsonSerializer.Deserialize<T>(json);
}

/// <summary>
/// Serializes an object to a json string representation
/// </summary>
/// <param name="obj">The object.</param>
/// <returns>The json string representation of the object.</returns>
public static string Serialize(object obj)
{
return Configuration.FlurlHttpSettings.JsonSerializer.Serialize(obj);
}

/// <summary>
/// Provides global point for programmatically configuraing tracing
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion src/corelib/Testing/HttpTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ private void SetTestMode(OpenStackNetConfigurationOptions options)
ResponseQueue.Enqueue(new HttpResponseMessage
{
StatusCode = (HttpStatusCode)status,
Content = new CapturedJsonContent(OpenStackNet.Configuration.FlurlHttpSettings.JsonSerializer.Serialize(data))
Content = new CapturedJsonContent(OpenStackNet.Serialize(data))
});
return this;
}
Expand Down
2 changes: 1 addition & 1 deletion src/testing/unit/Compute/v2_1/KeyPairTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void DeserializeKeyPairCollection()
]
}").ToString();

var results = OpenStackNet.Configuration.FlurlHttpSettings.JsonSerializer.Deserialize<KeyPairSummaryCollection>(json);
var results = OpenStackNet.Deserialize<KeyPairSummaryCollection>(json);
Assert.NotNull(results);
Assert.Equal(2, results.Count());
var result = results.First();
Expand Down
2 changes: 1 addition & 1 deletion src/testing/unit/Compute/v2_1/SecurityGroupTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public void DeserializeSecurityGroupRule()
},
'id': '55d75417-37df-48e2-96aa-20ba53a82900'
}}";
var result = OpenStackNet.Configuration.FlurlHttpSettings.JsonSerializer.Deserialize<SecurityGroupRule>(JObject.Parse(json).ToString());
var result = OpenStackNet.Deserialize<SecurityGroupRule>(JObject.Parse(json).ToString());
Assert.Equal("0.0.0.0 / 24", result.CIDR);
}

Expand Down
4 changes: 2 additions & 2 deletions src/testing/unit/Compute/v2_1/ServerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public void SerializeServerWithSchedulerHints()
server.SchedulerHints = new SchedulerHints();
server.SchedulerHints.Add("group", "groupId");

var json = OpenStackNet.Configuration.FlurlHttpSettings.JsonSerializer.Serialize(server);
var json = OpenStackNet.Serialize(server);
Assert.Equal(expectedJson, json);
}

Expand All @@ -40,7 +40,7 @@ public void SerializeServerWithoutSchedulerHints()
.ToString(Formatting.None);
var server = new ServerCreateDefinition("name", Guid.Empty, Guid.Empty);

var json = OpenStackNet.Configuration.FlurlHttpSettings.JsonSerializer.Serialize(server);
var json = OpenStackNet.Serialize(server);
Assert.Equal(expectedJson, json);
}

Expand Down
2 changes: 1 addition & 1 deletion src/testing/unit/Compute/v2_1/VolumeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public VolumeTests()
public void DeserializeVolumeWithEmptyAttachment()
{
var json = JObject.Parse(@"{'volume': {'attachments': [{}]}}").ToString();
var result = OpenStackNet.Configuration.FlurlHttpSettings.JsonSerializer.Deserialize<Volume>(json);
var result = OpenStackNet.Deserialize<Volume>(json);
Assert.Empty(result.Attachments);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public void SerializeTimeToLiveToSeconds()
{
var cache = new ServiceCache("cache", TimeSpan.FromSeconds(60));

var json = OpenStackNet.Configuration.FlurlHttpSettings.JsonSerializer.Serialize(cache);
var json = OpenStackNet.Serialize(cache);

var result = JObject.Parse(json);
Assert.Equal(60, result.Value<double>("ttl"));
Expand All @@ -22,9 +22,9 @@ public void SerializeTimeToLiveToSeconds()
public void DeserializeTimeToLiveFromSeconds()
{
var cache = new ServiceCache("cache", TimeSpan.FromSeconds(60));
var json = OpenStackNet.Configuration.FlurlHttpSettings.JsonSerializer.Serialize(cache);
var json = OpenStackNet.Serialize(cache);

var result = OpenStackNet.Configuration.FlurlHttpSettings.JsonSerializer.Deserialize<ServiceCache>(json);
var result = OpenStackNet.Deserialize<ServiceCache>(json);

Assert.Equal(60, result.TimeToLive.TotalSeconds);
}
Expand Down
8 changes: 4 additions & 4 deletions src/testing/unit/ContentDeliveryNetworks/v1/ServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ public void SerializeServiceCollection()
Items = {new Service {Id = "service-id"}},
Links = {new PageLink("next", "http://api.com/next")}
};
string json = OpenStackNet.Configuration.FlurlHttpSettings.JsonSerializer.Serialize(services);
string json = OpenStackNet.Serialize(services);
Assert.Contains("\"services\"", json);
Assert.DoesNotContain("\"service\"", json);

var result = OpenStackNet.Configuration.FlurlHttpSettings.JsonSerializer.Deserialize<ServiceCollection>(json);
var result = OpenStackNet.Deserialize<ServiceCollection>(json);
Assert.NotNull(result);
Assert.Equal(1, result.Count());
Assert.Equal(1, result.Items.Count());
Expand All @@ -48,9 +48,9 @@ public void SerializeServiceCollection()
public void SerializePageLink()
{
var link = new PageLink("next", "http://api.com/next");
string json = OpenStackNet.Configuration.FlurlHttpSettings.JsonSerializer.Serialize(link);
string json = OpenStackNet.Serialize(link);

var result = OpenStackNet.Configuration.FlurlHttpSettings.JsonSerializer.Deserialize<PageLink>(json);
var result = OpenStackNet.Deserialize<PageLink>(json);
Assert.NotNull(result);
Assert.True(result.IsNextPage);
}
Expand Down
8 changes: 4 additions & 4 deletions src/testing/unit/IdentifierTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public void Serialize()
var rawId = Guid.NewGuid();
var id = (Identifier)rawId;

var result = OpenStackNet.Configuration.FlurlHttpSettings.JsonSerializer.Serialize(id);
var result = OpenStackNet.Serialize(id);

Assert.Equal(string.Format("\"{0}\"", rawId.ToString("D")), result);
}
Expand All @@ -27,7 +27,7 @@ public void Serialize_WithinAClass()
{
var thing = new Thing {Id = Guid.NewGuid()};

var result = OpenStackNet.Configuration.FlurlHttpSettings.JsonSerializer.Serialize(thing);
var result = OpenStackNet.Serialize(thing);

Assert.Equal(string.Format("{{\"Id\":\"{0}\"}}", thing.Id), result);
}
Expand All @@ -37,8 +37,8 @@ public void Deserialize()
{
var id = new Identifier(Guid.NewGuid());

var json = OpenStackNet.Configuration.FlurlHttpSettings.JsonSerializer.Serialize(id);
var result = OpenStackNet.Configuration.FlurlHttpSettings.JsonSerializer.Deserialize<Identifier>(json);
var json = OpenStackNet.Serialize(id);
var result = OpenStackNet.Deserialize<Identifier>(json);

Assert.Equal(id, result);
}
Expand Down
8 changes: 4 additions & 4 deletions src/testing/unit/Networking/v2/DHCPOptionConverterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public void Serialize()
}
};

string result = OpenStackNet.Configuration.FlurlHttpSettings.JsonSerializer.Serialize(input);
string result = OpenStackNet.Serialize(input);

string expectedJson = JObject.Parse("{'port':{'extra_dhcp_opts':[{'opt_name':'a','opt_value':'stuff'},{'opt_name':'b','opt_value':'things'}]}}").ToString(Formatting.None);
Assert.Equal(expectedJson, result);
Expand All @@ -30,7 +30,7 @@ public void Deserialize()
{
string json = JObject.Parse("{'port':{'extra_dhcp_opts':[{'opt_name':'a','opt_value':'stuff'},{'opt_name':'b','opt_value':'things'}]}}").ToString(Formatting.None);

var result = OpenStackNet.Configuration.FlurlHttpSettings.JsonSerializer.Deserialize<PortCreateDefinition>(json).DHCPOptions;
var result = OpenStackNet.Deserialize<PortCreateDefinition>(json).DHCPOptions;

Assert.NotNull(result);
Assert.Equal(2, result.Count);
Expand All @@ -52,8 +52,8 @@ public void OpenStackNet_UsesDHCPOptionConverter()
}
};

var json = OpenStackNet.Configuration.FlurlHttpSettings.JsonSerializer.Serialize(port);
var result = OpenStackNet.Configuration.FlurlHttpSettings.JsonSerializer.Deserialize<Port>(json);
var json = OpenStackNet.Serialize(port);
var result = OpenStackNet.Deserialize<Port>(json);

Assert.NotNull(result.DHCPOptions);
Assert.Equal(1, result.DHCPOptions.Count);
Expand Down
6 changes: 3 additions & 3 deletions src/testing/unit/Serialization/EmptyEnumerableTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ public ExampleThing()
public void WhenDeserializingNullCollection_ItShouldUseAnEmptyCollection()
{
var thing = new ExampleThing{Messages = null};
string json = OpenStackNet.Configuration.FlurlHttpSettings.JsonSerializer.Serialize(thing);
string json = OpenStackNet.Serialize(thing);
Assert.DoesNotContain("messages", json);

var result = OpenStackNet.Configuration.FlurlHttpSettings.JsonSerializer.Deserialize<ExampleThing>(json);
var result = OpenStackNet.Deserialize<ExampleThing>(json);

Assert.NotNull(result.Messages);
Assert.Empty(result.Messages);
Expand All @@ -35,7 +35,7 @@ public void WhenSerializingEmptyCollection_ItShouldBeIgnored()
{
var thing = new ExampleThing { Messages = new List<string>() };

string json = OpenStackNet.Configuration.FlurlHttpSettings.JsonSerializer.Serialize(thing);
string json = OpenStackNet.Serialize(thing);

Assert.DoesNotContain("messages", json);
}
Expand Down
16 changes: 8 additions & 8 deletions src/testing/unit/Serialization/RootWrapperConverterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class ThingCollection : List<Thing>
[Fact]
public void Serialize()
{
var json = OpenStackNet.Configuration.FlurlHttpSettings.JsonSerializer.Serialize(new Thing());
var json = OpenStackNet.Serialize(new Thing());

var jsonObj = JObject.Parse(json);
JProperty rootProperty = jsonObj.Properties().FirstOrDefault();
Expand All @@ -34,23 +34,23 @@ public void Serialize()
[Fact]
public void Deserialize()
{
var json = OpenStackNet.Configuration.FlurlHttpSettings.JsonSerializer.Serialize(new Thing {Id = "thing-id"});
var thing = OpenStackNet.Configuration.FlurlHttpSettings.JsonSerializer.Deserialize<Thing>(json);
var json = OpenStackNet.Serialize(new Thing {Id = "thing-id"});
var thing = OpenStackNet.Deserialize<Thing>(json);
Assert.Equal("thing-id", thing.Id);
}

[Fact]
public void SerializeWhenNotRoot()
{
var json = OpenStackNet.Configuration.FlurlHttpSettings.JsonSerializer.Serialize(new List<Thing>{ new Thing() });
var json = OpenStackNet.Serialize(new List<Thing>{ new Thing() });

Assert.DoesNotContain("\"thing\"", json);
}

[Fact]
public void SerializeWhenNested()
{
var json = OpenStackNet.Configuration.FlurlHttpSettings.JsonSerializer.Serialize(new ThingCollection { new Thing() });
var json = OpenStackNet.Serialize(new ThingCollection { new Thing() });

Assert.DoesNotContain("\"thing\"", json);
}
Expand All @@ -59,7 +59,7 @@ public void SerializeWhenNested()
public void DeserializeWhenNotRoot()
{
var json = JArray.Parse("[{'id':'thing-id'}]").ToString();
var things = OpenStackNet.Configuration.FlurlHttpSettings.JsonSerializer.Deserialize<List<Thing>>(json);
var things = OpenStackNet.Deserialize<List<Thing>>(json);
Assert.Equal(1, things.Count);
Assert.Equal("thing-id", things[0].Id);
}
Expand All @@ -68,7 +68,7 @@ public void DeserializeWhenNotRoot()
public void DeserializeWhenNested()
{
var json = JObject.Parse("{'things':[{'id':'thing-id'}]}").ToString();
var things = OpenStackNet.Configuration.FlurlHttpSettings.JsonSerializer.Deserialize<ThingCollection>(json);
var things = OpenStackNet.Deserialize<ThingCollection>(json);
Assert.NotNull(things);
Assert.Equal(1, things.Count);
Assert.Equal("thing-id", things[0].Id);
Expand All @@ -78,7 +78,7 @@ public void DeserializeWhenNested()
public void ShouldIgnoreUnexpectedRootProperties()
{
var json = JObject.Parse("{'links': [{'name': 'next', 'link': 'http://nextlink'}], 'thing': {'id': 'thing-id'}}").ToString();
var thing = OpenStackNet.Configuration.FlurlHttpSettings.JsonSerializer.Deserialize<Thing>(json);
var thing = OpenStackNet.Deserialize<Thing>(json);
Assert.NotNull(thing);
Assert.Equal("thing-id", thing.Id);
}
Expand Down
10 changes: 5 additions & 5 deletions src/testing/unit/Serialization/TolerantEnumConverterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,39 +25,39 @@ enum StuffStatus
[Fact]
public void WhenValueIsRecognized_MatchToValue()
{
var result = OpenStackNet.Configuration.FlurlHttpSettings.JsonSerializer.Deserialize<ThingStatus>("\"ACTIVE\"");
var result = OpenStackNet.Deserialize<ThingStatus>("\"ACTIVE\"");

Assert.Equal(ThingStatus.Active, result);
}

[Fact]
public void WhenAttributedValueIsRecognized_MatchToValue()
{
var result = OpenStackNet.Configuration.FlurlHttpSettings.JsonSerializer.Deserialize<ThingStatus>("\"REMOVE_FAILED\"");
var result = OpenStackNet.Deserialize<ThingStatus>("\"REMOVE_FAILED\"");

Assert.Equal(ThingStatus.RemoveFailed, result);
}

[Fact]
public void WhenValueIsUnrecognized_MatchToUnknownValue()
{
var result = OpenStackNet.Configuration.FlurlHttpSettings.JsonSerializer.Deserialize<ThingStatus>("\"bad-enum-value\"");
var result = OpenStackNet.Deserialize<ThingStatus>("\"bad-enum-value\"");

Assert.Equal(ThingStatus.Unknown, result);
}

[Fact]
public void WhenValueIsUnrecognized_AndUnknownIsNotPresent_MatchToFirstValue()
{
var result = OpenStackNet.Configuration.FlurlHttpSettings.JsonSerializer.Deserialize<StuffStatus>("\"bad-enum-value\"");
var result = OpenStackNet.Deserialize<StuffStatus>("\"bad-enum-value\"");

Assert.Equal(StuffStatus.Missing, result);
}

[Fact]
public void WhenValueIsUnrecognized_AndDestinationIsNullable_UseNull()
{
var result = OpenStackNet.Configuration.FlurlHttpSettings.JsonSerializer.Deserialize<StuffStatus?>("\"bad-enum-value\"");
var result = OpenStackNet.Deserialize<StuffStatus?>("\"bad-enum-value\"");

Assert.Null(result);
}
Expand Down

0 comments on commit 7f10530

Please sign in to comment.