Skip to content

Commit

Permalink
dotnet format
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Omondi committed Aug 5, 2024
1 parent b28d524 commit 8919b34
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 115 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using System.ComponentModel;

#if NET5_0_OR_GREATER
using System.Diagnostics.CodeAnalysis;
Expand All @@ -27,7 +27,7 @@ public static partial class KiotaJsonSerializer
[EditorBrowsable(EditorBrowsableState.Never)]
public static Stream SerializeAsStream<T>(T value) where T : IParsable
=> KiotaSerializer.SerializeAsStream(_jsonContentType, value);

/// <summary>
/// Serializes the given object into a string based on the content type.
/// </summary>
Expand All @@ -48,9 +48,9 @@ public static string SerializeAsString<T>(T value) where T : IParsable
/// <returns>The serialized representation as a string.</returns>
[Obsolete("This method is obsolete, use the extension methods in Microsoft.Kiota.Serialization.Json.IParsableExtensions instead")]
[EditorBrowsable(EditorBrowsableState.Never)]
public static Task<string> SerializeAsStringAsync<T>(T value, CancellationToken cancellationToken) where T : IParsable
public static Task<string> SerializeAsStringAsync<T>(T value, CancellationToken cancellationToken) where T : IParsable
=> KiotaSerializer.SerializeAsStringAsync(_jsonContentType, value, true, cancellationToken);

/// <summary>
/// Serializes the given object into a string based on the content type.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using System.ComponentModel;


#if NET5_0_OR_GREATER
Expand Down
10 changes: 5 additions & 5 deletions src/serialization/form/IParsableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public static class IParsableExtensions
/// <returns>The serialized representation as a stream.</returns>
public static Stream SerializeAsFormStream<T>(this T value, bool serializeOnlyChangedValues = true) where T : IParsable
=> KiotaSerializer.SerializeAsStream(_formContentType, value, serializeOnlyChangedValues);

/// <summary>
/// Serializes the given object into a form url encoded string.
/// </summary>
Expand All @@ -32,7 +32,7 @@ public static Stream SerializeAsFormStream<T>(this T value, bool serializeOnlyCh
/// <returns>The serialized representation as a string.</returns>
public static Task<string> SerializeAsFormAsync<T>(this T value, bool serializeOnlyChangedValues = true, CancellationToken cancellationToken = default) where T : IParsable
=> KiotaSerializer.SerializeAsStringAsync(_formContentType, value, serializeOnlyChangedValues, cancellationToken);

/// <summary>
/// Serializes the given object into stream with form url encoded content type.
/// </summary>
Expand All @@ -42,7 +42,7 @@ public static Task<string> SerializeAsFormAsync<T>(this T value, bool serializeO
/// <returns>The serialized representation as a stream.</returns>
public static Stream SerializeAsFormStream<T>(this IEnumerable<T> value, bool serializeOnlyChangedValues = true) where T : IParsable
=> KiotaSerializer.SerializeAsStream(_formContentType, value, serializeOnlyChangedValues);

/// <summary>
/// Serializes the given object into a form url encoded string.
/// </summary>
Expand All @@ -63,7 +63,7 @@ public static Task<string> SerializeAsFormAsync<T>(this IEnumerable<T> value, bo
/// <returns>The serialized representation as a stream.</returns>
public static Stream SerializeAsFormStream<T>(this T[] value, bool serializeOnlyChangedValues = true) where T : IParsable
=> KiotaSerializer.SerializeAsStream(_formContentType, value, serializeOnlyChangedValues);

/// <summary>
/// Serializes the given object into a form url encoded string.
/// </summary>
Expand All @@ -74,5 +74,5 @@ public static Stream SerializeAsFormStream<T>(this T[] value, bool serializeOnly
/// <returns>The serialized representation as a string.</returns>
public static Task<string> SerializeAsFormAsync<T>(this T[] value, bool serializeOnlyChangedValues = true, CancellationToken cancellationToken = default) where T : IParsable
=> KiotaSerializer.SerializeAsStringAsync(_formContentType, value, serializeOnlyChangedValues, cancellationToken);

}
10 changes: 5 additions & 5 deletions src/serialization/json/IParsableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public static class IParsableExtensions
/// <returns>The serialized representation as a stream.</returns>
public static Stream SerializeAsJsonStream<T>(this T value, bool serializeOnlyChangedValues = true) where T : IParsable
=> KiotaSerializer.SerializeAsStream(_jsonContentType, value, serializeOnlyChangedValues);

/// <summary>
/// Serializes the given object into a json string.
/// </summary>
Expand All @@ -32,7 +32,7 @@ public static Stream SerializeAsJsonStream<T>(this T value, bool serializeOnlyCh
/// <returns>The serialized representation as a string.</returns>
public static Task<string> SerializeAsJsonAsync<T>(this T value, bool serializeOnlyChangedValues = true, CancellationToken cancellationToken = default) where T : IParsable
=> KiotaSerializer.SerializeAsStringAsync(_jsonContentType, value, serializeOnlyChangedValues, cancellationToken);

/// <summary>
/// Serializes the given object into stream with json content type.
/// </summary>
Expand All @@ -42,7 +42,7 @@ public static Task<string> SerializeAsJsonAsync<T>(this T value, bool serializeO
/// <returns>The serialized representation as a stream.</returns>
public static Stream SerializeAsJsonStream<T>(this IEnumerable<T> value, bool serializeOnlyChangedValues = true) where T : IParsable
=> KiotaSerializer.SerializeAsStream(_jsonContentType, value, serializeOnlyChangedValues);

/// <summary>
/// Serializes the given object into a json string.
/// </summary>
Expand All @@ -63,7 +63,7 @@ public static Task<string> SerializeAsJsonAsync<T>(this IEnumerable<T> value, bo
/// <returns>The serialized representation as a stream.</returns>
public static Stream SerializeAsJsonStream<T>(this T[] value, bool serializeOnlyChangedValues = true) where T : IParsable
=> KiotaSerializer.SerializeAsStream(_jsonContentType, value, serializeOnlyChangedValues);

/// <summary>
/// Serializes the given object into a json string.
/// </summary>
Expand All @@ -74,5 +74,5 @@ public static Stream SerializeAsJsonStream<T>(this T[] value, bool serializeOnly
/// <returns>The serialized representation as a string.</returns>
public static Task<string> SerializeAsJsonAsync<T>(this T[] value, bool serializeOnlyChangedValues = true, CancellationToken cancellationToken = default) where T : IParsable
=> KiotaSerializer.SerializeAsStringAsync(_jsonContentType, value, serializeOnlyChangedValues, cancellationToken);

}
150 changes: 75 additions & 75 deletions tests/serialization/json/IParsableExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,94 +6,94 @@

namespace Microsoft.Kiota.Serialization.Json.Tests
{
public class IParsableExtensionsTests
{
private const string _jsonContentType = "application/json";
private readonly SerializationWriterFactoryRegistry _serializationWriterFactoryRegistry;

public IParsableExtensionsTests()
public class IParsableExtensionsTests
{
_serializationWriterFactoryRegistry = new SerializationWriterFactoryRegistry();
_serializationWriterFactoryRegistry.ContentTypeAssociatedFactories.TryAdd(_jsonContentType, new BackingStoreSerializationWriterProxyFactory(new JsonSerializationWriterFactory()));
}
private const string _jsonContentType = "application/json";
private readonly SerializationWriterFactoryRegistry _serializationWriterFactoryRegistry;

[Theory]
[InlineData(null)]
[InlineData(true)]
[InlineData(false)]
public void GetSerializationWriter_RetunsJsonSerializationWriter(bool? serializeOnlyChangedValues)
{
// Arrange
public IParsableExtensionsTests()
{
_serializationWriterFactoryRegistry = new SerializationWriterFactoryRegistry();
_serializationWriterFactoryRegistry.ContentTypeAssociatedFactories.TryAdd(_jsonContentType, new BackingStoreSerializationWriterProxyFactory(new JsonSerializationWriterFactory()));
}

// Act
using var writer = serializeOnlyChangedValues.HasValue
? _serializationWriterFactoryRegistry.GetSerializationWriter(_jsonContentType, serializeOnlyChangedValues.Value)
: _serializationWriterFactoryRegistry.GetSerializationWriter(_jsonContentType);
[Theory]
[InlineData(null)]
[InlineData(true)]
[InlineData(false)]
public void GetSerializationWriter_RetunsJsonSerializationWriter(bool? serializeOnlyChangedValues)
{
// Arrange

// Assert
Assert.NotNull(writer);
Assert.IsType<JsonSerializationWriter>(writer);
}
// Act
using var writer = serializeOnlyChangedValues.HasValue
? _serializationWriterFactoryRegistry.GetSerializationWriter(_jsonContentType, serializeOnlyChangedValues.Value)
: _serializationWriterFactoryRegistry.GetSerializationWriter(_jsonContentType);

[Fact]
public void GetSerializationWriterSerializedChangedTrue_RetunsEmptyJson()
{
// Arrange
var testUser = new BackedTestEntity { Id = "1", Name = "testUser" };
testUser.BackingStore.InitializationCompleted = true;
using var writer = _serializationWriterFactoryRegistry.GetSerializationWriter(_jsonContentType, true);
// Assert
Assert.NotNull(writer);
Assert.IsType<JsonSerializationWriter>(writer);
}

// Act
writer.WriteObjectValue(null, testUser);
using var stream = writer.GetSerializedContent();
var serializedContent = GetStringFromStream(stream);
[Fact]
public void GetSerializationWriterSerializedChangedTrue_RetunsEmptyJson()
{
// Arrange
var testUser = new BackedTestEntity { Id = "1", Name = "testUser" };
testUser.BackingStore.InitializationCompleted = true;
using var writer = _serializationWriterFactoryRegistry.GetSerializationWriter(_jsonContentType, true);

// Assert
Assert.NotNull(serializedContent);
Assert.Equal("{}", serializedContent);
}
// Act
writer.WriteObjectValue(null, testUser);
using var stream = writer.GetSerializedContent();
var serializedContent = GetStringFromStream(stream);

[Fact]
public void GetSerializationWriterSerializedChangedTrue_ChangedName_ReturnsJustName()
{
// Arrange
var testUser = new BackedTestEntity { Id = "1", Name = "testUser" };
testUser.BackingStore.InitializationCompleted = true;
testUser.Name = "Stephan";
using var writer = _serializationWriterFactoryRegistry.GetSerializationWriter(_jsonContentType, true);
// Assert
Assert.NotNull(serializedContent);
Assert.Equal("{}", serializedContent);
}

// Act
writer.WriteObjectValue(null, testUser);
using var stream = writer.GetSerializedContent();
var serializedContent = GetStringFromStream(stream);
[Fact]
public void GetSerializationWriterSerializedChangedTrue_ChangedName_ReturnsJustName()
{
// Arrange
var testUser = new BackedTestEntity { Id = "1", Name = "testUser" };
testUser.BackingStore.InitializationCompleted = true;
testUser.Name = "Stephan";
using var writer = _serializationWriterFactoryRegistry.GetSerializationWriter(_jsonContentType, true);

// Assert
Assert.NotNull(serializedContent);
Assert.Equal("{\"name\":\"Stephan\"}", serializedContent);
}
// Act
writer.WriteObjectValue(null, testUser);
using var stream = writer.GetSerializedContent();
var serializedContent = GetStringFromStream(stream);

[Fact]
public void GetSerializationWriterSerializedChangedFalse_SerializesEntireObject()
{
// Arrange
var testUser = new BackedTestEntity { Id = "1", Name = "testUser" };
testUser.BackingStore.InitializationCompleted = true;
using var writer = _serializationWriterFactoryRegistry.GetSerializationWriter(_jsonContentType, false);
// Assert
Assert.NotNull(serializedContent);
Assert.Equal("{\"name\":\"Stephan\"}", serializedContent);
}

// Act
writer.WriteObjectValue(null, testUser);
using var stream = writer.GetSerializedContent();
var serializedContent = GetStringFromStream(stream);
[Fact]
public void GetSerializationWriterSerializedChangedFalse_SerializesEntireObject()
{
// Arrange
var testUser = new BackedTestEntity { Id = "1", Name = "testUser" };
testUser.BackingStore.InitializationCompleted = true;
using var writer = _serializationWriterFactoryRegistry.GetSerializationWriter(_jsonContentType, false);

// Assert
Assert.NotNull(serializedContent);
Assert.Equal("{\"id\":\"1\",\"name\":\"testUser\"}", serializedContent);
}
// Act
writer.WriteObjectValue(null, testUser);
using var stream = writer.GetSerializedContent();
var serializedContent = GetStringFromStream(stream);

private static string GetStringFromStream(Stream stream)
{
using var reader = new StreamReader(stream);
return reader.ReadToEnd();
// Assert
Assert.NotNull(serializedContent);
Assert.Equal("{\"id\":\"1\",\"name\":\"testUser\"}", serializedContent);
}

private static string GetStringFromStream(Stream stream)
{
using var reader = new StreamReader(stream);
return reader.ReadToEnd();
}
}
}
}
50 changes: 25 additions & 25 deletions tests/serialization/json/Mocks/BackedTestEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,36 @@

namespace Microsoft.Kiota.Serialization.Json.Tests.Mocks
{
public class BackedTestEntity : IParsable, IBackedModel
{
public BackedTestEntity()
public class BackedTestEntity : IParsable, IBackedModel
{
BackingStore = new InMemoryBackingStore();
}
public BackedTestEntity()
{
BackingStore = new InMemoryBackingStore();
}

public IBackingStore BackingStore { get; private set; }
public IBackingStore BackingStore { get; private set; }

public string Id
{
get { return BackingStore?.Get<string>("id"); }
set { BackingStore?.Set("id", value); }
}
public string Name
{
get { return BackingStore?.Get<string>("name"); }
set { BackingStore?.Set("name", value); }
}
public string Id
{
get { return BackingStore?.Get<string>("id"); }
set { BackingStore?.Set("id", value); }
}
public string Name
{
get { return BackingStore?.Get<string>("name"); }
set { BackingStore?.Set("name", value); }
}

public IDictionary<string, Action<IParseNode>> GetFieldDeserializers() =>
new Dictionary<string, Action<IParseNode>> {
public IDictionary<string, Action<IParseNode>> GetFieldDeserializers() =>
new Dictionary<string, Action<IParseNode>> {
{ "id", n => { Id = n.GetStringValue(); } },
{ "name", n => { Name = n.GetStringValue(); } },
};
public void Serialize(ISerializationWriter writer)
{
_ = writer ?? throw new ArgumentNullException(nameof(writer));
writer.WriteStringValue("id", Id);
writer.WriteStringValue("name", Name);
};
public void Serialize(ISerializationWriter writer)
{
_ = writer ?? throw new ArgumentNullException(nameof(writer));
writer.WriteStringValue("id", Id);
writer.WriteStringValue("name", Name);
}
}
}
}

0 comments on commit 8919b34

Please sign in to comment.