Skip to content

Commit

Permalink
- applies feedback from issue discussion
Browse files Browse the repository at this point in the history
  • Loading branch information
baywet committed Oct 31, 2023
1 parent e0b5c36 commit 6752388
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,20 @@ public class DeserializationHelpersTests
[Fact]
public void DefensiveObject()
{
Assert.Throws<ArgumentNullException>(() => DeserializationHelpers.Deserialize<TestEntity>(null, (Stream)null, null));
Assert.Throws<ArgumentNullException>(() => DeserializationHelpers.Deserialize<TestEntity>(_jsonContentType, (Stream)null, null));
Assert.Throws<ArgumentNullException>(() => KiotaSerializer.Deserialize<TestEntity>(null, (Stream)null, null));
Assert.Throws<ArgumentNullException>(() => KiotaSerializer.Deserialize<TestEntity>(_jsonContentType, (Stream)null, null));
using var stream = new MemoryStream();
Assert.Throws<ArgumentNullException>(() => DeserializationHelpers.Deserialize<TestEntity>(_jsonContentType, stream, null));
Assert.Throws<ArgumentNullException>(() => DeserializationHelpers.Deserialize<TestEntity>(_jsonContentType, "", null));
Assert.Throws<ArgumentNullException>(() => KiotaSerializer.Deserialize<TestEntity>(_jsonContentType, stream, null));
Assert.Throws<ArgumentNullException>(() => KiotaSerializer.Deserialize<TestEntity>(_jsonContentType, "", null));
}
[Fact]
public void DefensiveObjectCollection()
{
Assert.Throws<ArgumentNullException>(() => DeserializationHelpers.DeserializeCollection<TestEntity>(null, (Stream)null, null));
Assert.Throws<ArgumentNullException>(() => DeserializationHelpers.DeserializeCollection<TestEntity>(_jsonContentType, (Stream)null, null));
Assert.Throws<ArgumentNullException>(() => KiotaSerializer.DeserializeCollection<TestEntity>(null, (Stream)null, null));
Assert.Throws<ArgumentNullException>(() => KiotaSerializer.DeserializeCollection<TestEntity>(_jsonContentType, (Stream)null, null));
using var stream = new MemoryStream();
Assert.Throws<ArgumentNullException>(() => DeserializationHelpers.DeserializeCollection<TestEntity>(_jsonContentType, stream, null));
Assert.Throws<ArgumentNullException>(() => DeserializationHelpers.DeserializeCollection<TestEntity>(_jsonContentType, "", null));
Assert.Throws<ArgumentNullException>(() => KiotaSerializer.DeserializeCollection<TestEntity>(_jsonContentType, stream, null));
Assert.Throws<ArgumentNullException>(() => KiotaSerializer.DeserializeCollection<TestEntity>(_jsonContentType, "", null));
}
[Fact]
public void DeserializesObjectWithoutReflection()
Expand All @@ -43,7 +43,7 @@ public void DeserializesObjectWithoutReflection()
mockJsonParseNodeFactory.Setup(x => x.ValidContentType).Returns(_jsonContentType);
ParseNodeFactoryRegistry.DefaultInstance.ContentTypeAssociatedFactories[_jsonContentType] = mockJsonParseNodeFactory.Object;

var result = DeserializationHelpers.Deserialize(_jsonContentType, strValue, TestEntity.CreateFromDiscriminatorValue);
var result = KiotaSerializer.Deserialize(_jsonContentType, strValue, TestEntity.CreateFromDiscriminatorValue);

Assert.NotNull(result);
}
Expand All @@ -61,7 +61,7 @@ public void DeserializesObjectWithReflection()
mockJsonParseNodeFactory.Setup(x => x.ValidContentType).Returns(_jsonContentType);
ParseNodeFactoryRegistry.DefaultInstance.ContentTypeAssociatedFactories[_jsonContentType] = mockJsonParseNodeFactory.Object;

var result = DeserializationHelpers.Deserialize<TestEntity>(_jsonContentType, strValue);
var result = KiotaSerializer.Deserialize<TestEntity>(_jsonContentType, strValue);

Assert.NotNull(result);
}
Expand All @@ -81,7 +81,7 @@ public void DeserializesCollectionOfObject()
mockJsonParseNodeFactory.Setup(x => x.ValidContentType).Returns(_jsonContentType);
ParseNodeFactoryRegistry.DefaultInstance.ContentTypeAssociatedFactories[_jsonContentType] = mockJsonParseNodeFactory.Object;

var result = DeserializationHelpers.DeserializeCollection(_jsonContentType, strValue, TestEntity.CreateFromDiscriminatorValue);
var result = KiotaSerializer.DeserializeCollection(_jsonContentType, strValue, TestEntity.CreateFromDiscriminatorValue);

Assert.NotNull(result);
Assert.Single(result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ public class SerializationHelpersTests
[Fact]
public void DefensiveObject()
{
Assert.Throws<ArgumentNullException>(() => SerializationHelpers.SerializeAsStream(null, (TestEntity)null));
Assert.Throws<ArgumentNullException>(() => SerializationHelpers.SerializeAsStream(_jsonContentType, (TestEntity)null));
Assert.Throws<ArgumentNullException>(() => KiotaSerializer.SerializeAsStream(null, (TestEntity)null));
Assert.Throws<ArgumentNullException>(() => KiotaSerializer.SerializeAsStream(_jsonContentType, (TestEntity)null));
}
[Fact]
public void DefensiveObjectCollection()
{
Assert.Throws<ArgumentNullException>(() => SerializationHelpers.SerializeAsStream(null, (IEnumerable<TestEntity>)null));
Assert.Throws<ArgumentNullException>(() => SerializationHelpers.SerializeAsStream(_jsonContentType, (IEnumerable<TestEntity>)null));
Assert.Throws<ArgumentNullException>(() => KiotaSerializer.SerializeAsStream(null, (IEnumerable<TestEntity>)null));
Assert.Throws<ArgumentNullException>(() => KiotaSerializer.SerializeAsStream(_jsonContentType, (IEnumerable<TestEntity>)null));
}
[Fact]
public void SerializesObject()
Expand All @@ -33,7 +33,7 @@ public void SerializesObject()
mockSerializationWriterFactory.Setup(x => x.GetSerializationWriter(It.IsAny<string>())).Returns(mockSerializationWriter.Object);
SerializationWriterFactoryRegistry.DefaultInstance.ContentTypeAssociatedFactories[_jsonContentType] = mockSerializationWriterFactory.Object;

var result = SerializationHelpers.SerializeAsString(_jsonContentType, new TestEntity()
var result = KiotaSerializer.SerializeAsString(_jsonContentType, new TestEntity()
{
Id = "123"
});
Expand All @@ -53,7 +53,7 @@ public void SerializesObjectCollection()
mockSerializationWriterFactory.Setup(x => x.GetSerializationWriter(It.IsAny<string>())).Returns(mockSerializationWriter.Object);
SerializationWriterFactoryRegistry.DefaultInstance.ContentTypeAssociatedFactories[_jsonContentType] = mockSerializationWriterFactory.Object;

var result = SerializationHelpers.SerializeAsString(_jsonContentType, new List<TestEntity> {
var result = KiotaSerializer.SerializeAsString(_jsonContentType, new List<TestEntity> {
new()
{
Id = "123"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Microsoft.Kiota.Abstractions.Serialization;
/// <summary>
/// Set of helper methods for JSON serialization
/// </summary>
public static class JsonDeserializationHelpers
public static partial class KiotaJsonSerializer
{
private const string _jsonContentType = "application/json";
/// <summary>
Expand All @@ -22,14 +22,14 @@ public static class JsonDeserializationHelpers
/// <param name="parsableFactory">The factory to create the object.</param>
/// <param name="serializedRepresentation">The serialized representation of the object.</param>
public static T? Deserialize<T>(string serializedRepresentation, ParsableFactory<T> parsableFactory) where T : IParsable
=> DeserializationHelpers.Deserialize(_jsonContentType, serializedRepresentation, parsableFactory);
=> KiotaSerializer.Deserialize(_jsonContentType, serializedRepresentation, parsableFactory);
/// <summary>
/// Deserializes the given stream into an object.
/// </summary>
/// <param name="stream">The stream to deserialize.</param>
/// <param name="parsableFactory">The factory to create the object.</param>
public static T? Deserialize<T>(Stream stream, ParsableFactory<T> parsableFactory) where T : IParsable
=> DeserializationHelpers.Deserialize(_jsonContentType, stream, parsableFactory);
=> KiotaSerializer.Deserialize(_jsonContentType, stream, parsableFactory);
/// <summary>
/// Deserializes the given stream into an object.
/// </summary>
Expand All @@ -39,7 +39,7 @@ public static class JsonDeserializationHelpers
#else
public static T? Deserialize<T>(Stream stream) where T : IParsable
#endif
=> DeserializationHelpers.Deserialize<T>(_jsonContentType, stream);
=> KiotaSerializer.Deserialize<T>(_jsonContentType, stream);
/// <summary>
/// Deserializes the given stream into an object.
/// </summary>
Expand All @@ -49,21 +49,21 @@ public static class JsonDeserializationHelpers
#else
public static T? Deserialize<T>(string serializedRepresentation) where T : IParsable
#endif
=> DeserializationHelpers.Deserialize<T>(_jsonContentType, serializedRepresentation);
=> KiotaSerializer.Deserialize<T>(_jsonContentType, serializedRepresentation);
/// <summary>
/// Deserializes the given stream into a collection of objects based on the content type.
/// </summary>
/// <param name="stream">The stream to deserialize.</param>
/// <param name="parsableFactory">The factory to create the object.</param>
public static IEnumerable<T> DeserializeCollection<T>(Stream stream, ParsableFactory<T> parsableFactory) where T : IParsable
=> DeserializationHelpers.DeserializeCollection(_jsonContentType, stream, parsableFactory);
=> KiotaSerializer.DeserializeCollection(_jsonContentType, stream, parsableFactory);
/// <summary>
/// Deserializes the given stream into a collection of objects based on the content type.
/// </summary>
/// <param name="serializedRepresentation">The serialized representation of the objects.</param>
/// <param name="parsableFactory">The factory to create the object.</param>
public static IEnumerable<T> DeserializeCollection<T>(string serializedRepresentation, ParsableFactory<T> parsableFactory) where T : IParsable
=> DeserializationHelpers.DeserializeCollection(_jsonContentType, serializedRepresentation, parsableFactory);
=> KiotaSerializer.DeserializeCollection(_jsonContentType, serializedRepresentation, parsableFactory);
/// <summary>
/// Deserializes the given stream into a collection of objects based on the content type.
/// </summary>
Expand All @@ -73,7 +73,7 @@ public static IEnumerable<T> DeserializeCollection<T>(string serializedRepresent
#else
public static IEnumerable<T> DeserializeCollection<T>(Stream stream) where T : IParsable
#endif
=> DeserializationHelpers.DeserializeCollection<T>(_jsonContentType, stream);
=> KiotaSerializer.DeserializeCollection<T>(_jsonContentType, stream);
/// <summary>
/// Deserializes the given stream into a collection of objects based on the content type.
/// </summary>
Expand All @@ -83,5 +83,5 @@ public static IEnumerable<T> DeserializeCollection<T>(Stream stream) where T : I
#else
public static IEnumerable<T> DeserializeCollection<T>(string serializedRepresentation) where T : IParsable
#endif
=> DeserializationHelpers.DeserializeCollection<T>(_jsonContentType, serializedRepresentation);
=> KiotaSerializer.DeserializeCollection<T>(_jsonContentType, serializedRepresentation);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,16 @@

namespace Microsoft.Kiota.Abstractions.Serialization;

/// <summary>
/// Set of helper methods for JSON serialization
/// </summary>
public static class JsonSerializationHelpers
public static partial class KiotaJsonSerializer
{
private const string _jsonContentType = "application/json";
/// <summary>
/// Serializes the given object into a string based on the content type.
/// </summary>
/// <typeparam name="T">Type of the object to serialize</typeparam>
/// <param name="value">The object to serialize.</param>
/// <returns>The serialized representation as a stream.</returns>
public static Stream SerializeAsStream<T>(T value) where T : IParsable

Check warning on line 21 in src/serialization/KiotaJsonSerializer.Serialization.cs

View workflow job for this annotation

GitHub Actions / Build

All 'SerializeAsStream' method overloads should be adjacent. (https://rules.sonarsource.com/csharp/RSPEC-4136)
=> SerializationHelpers.SerializeAsStream(_jsonContentType, value);
=> KiotaSerializer.SerializeAsStream(_jsonContentType, value);

/// <summary>
/// Serializes the given object into a string based on the content type.
Expand All @@ -32,7 +28,7 @@ public static Stream SerializeAsStream<T>(T value) where T : IParsable
/// <param name="value">The object to serialize.</param>
/// <returns>The serialized representation as a string.</returns>
public static string SerializeAsString<T>(T value) where T : IParsable

Check warning on line 30 in src/serialization/KiotaJsonSerializer.Serialization.cs

View workflow job for this annotation

GitHub Actions / Build

All 'SerializeAsString' method overloads should be adjacent. (https://rules.sonarsource.com/csharp/RSPEC-4136)
=> SerializationHelpers.SerializeAsString(_jsonContentType, value);
=> KiotaSerializer.SerializeAsString(_jsonContentType, value);

/// <summary>
/// Serializes the given object into a string based on the content type.
Expand All @@ -41,7 +37,7 @@ public static string SerializeAsString<T>(T value) where T : IParsable
/// <param name="value">The object to serialize.</param>
/// <returns>The serialized representation as a stream.</returns>
public static Stream SerializeAsStream<T>(IEnumerable<T> value) where T : IParsable
=> SerializationHelpers.SerializeAsStream(_jsonContentType, value);
=> KiotaSerializer.SerializeAsStream(_jsonContentType, value);

/// <summary>
/// Serializes the given object into a string based on the content type.
Expand All @@ -50,6 +46,6 @@ public static Stream SerializeAsStream<T>(IEnumerable<T> value) where T : IParsa
/// <param name="value">The object to serialize.</param>
/// <returns>The serialized representation as a string.</returns>
public static string SerializeAsString<T>(IEnumerable<T> value) where T : IParsable
=> SerializationHelpers.SerializeAsString(_jsonContentType, value);
=> KiotaSerializer.SerializeAsString(_jsonContentType, value);

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
#if NET5_0_OR_GREATER
using System.Diagnostics.CodeAnalysis;
Expand All @@ -14,10 +13,7 @@

namespace Microsoft.Kiota.Abstractions.Serialization;

/// <summary>
/// Set of helper methods for serialization
/// </summary>
public static class DeserializationHelpers
public static partial class KiotaSerializer
{
/// <summary>
/// Deserializes the given stream into an object based on the content type.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace Microsoft.Kiota.Abstractions.Serialization;
/// <summary>
/// Set of helper methods for serialization
/// </summary>
public static class SerializationHelpers
public static partial class KiotaSerializer
{
/// <summary>
/// Serializes the given object into a string based on the content type.
Expand Down

0 comments on commit 6752388

Please sign in to comment.