Skip to content

Commit

Permalink
Additional Write API take a Stream (#2698)
Browse files Browse the repository at this point in the history
* new api takes stream

* rename method

* remove usings

* void when takes a stream, comment strings

* add testing, add api to public namespace

* review feedback
  • Loading branch information
westin-m authored Jul 12, 2024
1 parent 1db51db commit 323dda3
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Text.Json.Serialization;
using System.Threading;
using Microsoft.IdentityModel.Abstractions;
Expand Down Expand Up @@ -81,7 +82,7 @@ public static OpenIdConnectConfiguration Create(string json)
/// </summary>
/// <param name="configuration"><see cref="OpenIdConnectConfiguration"/> object to serialize.</param>
/// <returns>json string representing the configuration object.</returns>
/// <exception cref="ArgumentNullException">Thrown if <paramref name="configuration"/> is null.</exception>
/// <exception cref="ArgumentNullException">Thrown if <paramref name="configuration"/> is <see langword="null"/>.</exception>
public static string Write(OpenIdConnectConfiguration configuration)
{
if (configuration == null)
Expand All @@ -93,6 +94,27 @@ public static string Write(OpenIdConnectConfiguration configuration)
return OpenIdConnectConfigurationSerializer.Write(configuration);
}

/// <summary>
/// Writes an <see cref="OpenIdConnectConfiguration"/> as JSON to the <paramref name="stream"/>.
/// </summary>
/// <param name="configuration">The <see cref="OpenIdConnectConfiguration"/> to serialize.</param>
/// <param name="stream">The <see cref="Stream"/> to write to.</param>
/// <remarks>Because a <see cref="Stream"/> is provided, this method does not return a value.</remarks>
/// <exception cref="ArgumentNullException">Thrown if <paramref name="configuration"/> or <paramref name="stream"/> is <see langword="null"/>.</exception>
public static void Write(OpenIdConnectConfiguration configuration, Stream stream)
{
if (configuration == null)
throw LogHelper.LogArgumentNullException(nameof(configuration));

if (stream == null)
throw LogHelper.LogArgumentNullException(nameof(stream));

if (LogHelper.IsEnabled(EventLogLevel.Verbose))
LogHelper.LogVerbose(LogMessages.IDX21809);

OpenIdConnectConfigurationSerializer.Write(configuration, stream);
}

/// <summary>
/// Initializes an new instance of <see cref="OpenIdConnectConfiguration"/>.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,20 @@ public static string Write(OpenIdConnectConfiguration OpenIdConnectConfiguration
}
}

public static void Write(OpenIdConnectConfiguration OpenIdConnectConfiguration, Stream stream)
{
Utf8JsonWriter writer = null;
try
{
writer = new Utf8JsonWriter(stream, new JsonWriterOptions { Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping });
Write(ref writer, OpenIdConnectConfiguration);
}
finally
{
writer?.Dispose();
}
}

public static void Write(ref Utf8JsonWriter writer, OpenIdConnectConfiguration config)
{
writer.WriteStartObject();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
using System.Text.Json;
using Microsoft.IdentityModel.TestUtils;
using Microsoft.IdentityModel.Tokens;
Expand Down Expand Up @@ -276,6 +279,32 @@ public void RoundTripFromJson()
TestUtilities.AssertFailIfErrors(context);
}

[Fact]
public void RoundTripFromJsonWithStream()
{
using MemoryStream stream = new();

var context = new CompareContext { Title = "RoundTripFromJson" };
var oidcConfig1 = OpenIdConnectConfiguration.Create(OpenIdConfigData.JsonAllValues);
var oidcConfig2 = new OpenIdConnectConfiguration(OpenIdConfigData.JsonAllValues);

OpenIdConnectConfiguration.Write(oidcConfig1, stream);
var oidcJson1 = Encoding.UTF8.GetString(stream.ToArray());
var oidcConfig3 = OpenIdConnectConfiguration.Create(oidcJson1);
stream.SetLength(0);

OpenIdConnectConfiguration.Write(oidcConfig2, stream);
var oidcJson2 = Encoding.UTF8.GetString(stream.ToArray());
var oidcConfig4 = new OpenIdConnectConfiguration(oidcJson2);

IdentityComparer.AreEqual(oidcConfig1, oidcConfig2, context);
IdentityComparer.AreEqual(oidcConfig1, oidcConfig3, context);
IdentityComparer.AreEqual(oidcConfig1, oidcConfig4, context);
IdentityComparer.AreEqual(oidcJson1, oidcJson2, context);

TestUtilities.AssertFailIfErrors(context);
}

[Fact]
public void EmptyCollectionSerialization()
{
Expand All @@ -289,6 +318,22 @@ public void EmptyCollectionSerialization()
TestUtilities.AssertFailIfErrors(context);
}

[Fact]
public void EmptyCollectionSerializationWithStream()
{
using MemoryStream stream = new();

var context = new CompareContext {Title = "EmptyCollectionSerialization"};
// Initialize an OpenIdConnectConfiguration object with all collections empty.
var oidcWithEmptyCollections = new OpenIdConnectConfiguration();
OpenIdConnectConfiguration.Write(oidcWithEmptyCollections, stream);
var emptyCollectionBytes = Encoding.UTF8.GetBytes("{}");

IdentityComparer.AreEqual(stream.ToArray(), emptyCollectionBytes, context);

TestUtilities.AssertFailIfErrors(context);
}

[Fact]
public void NonemptyCollectionSerialization()
{
Expand Down Expand Up @@ -345,5 +390,23 @@ public void NonemptyCollectionSerialization()
}
TestUtilities.AssertFailIfErrors(context);
}

[Fact]
public void NonemptyCollectionSerializationWithStream()
{
using MemoryStream stream = new();

var context = new CompareContext { Title = "NonemptyCollectionSerialization" };
// Initialize an OpenIdConnectConfiguration object that has at least one element in each Collection.
var oidcWithAllCollections = OpenIdConnectConfiguration.Create(OpenIdConfigData.JsonAllValues);
var oidcWithAllCollectionsJson = OpenIdConnectConfiguration.Write(oidcWithAllCollections);
var oidcWithAllCollectionsBytes = Encoding.UTF8.GetBytes(oidcWithAllCollectionsJson);

OpenIdConnectConfiguration.Write(oidcWithAllCollections, stream);

IdentityComparer.AreBytesEqual(oidcWithAllCollectionsBytes, stream.GetBuffer(), context);

TestUtilities.AssertFailIfErrors(context);
}
}
}

0 comments on commit 323dda3

Please sign in to comment.