Skip to content

Commit a657ef5

Browse files
authored
Merge pull request #343 from grippstick/main
The call to ApiClientBuilder.EnableBackingStoreForParseNodeFactory wo…
2 parents 5155bd7 + 66c890b commit a657ef5

File tree

4 files changed

+87
-3
lines changed

4 files changed

+87
-3
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
## [1.12.2] - 2024-08-23
11+
12+
### Changed
13+
14+
- Fixed a bug where calls to ApiClientBuilder.EnableBackingStoreForParseNodeFactory and ApiClientBuilder.EnableBackingStoreForSerializationWriterFactory would enable a BackingStore around BackingStores. [#2563] (https://github.com/microsoftgraph/msgraph-sdk-dotnet/issues/2563) [#2588] (https://github.com/microsoftgraph/msgraph-sdk-dotnet/issues/2588)
15+
1016
## [1.12.1] - 2024-08-21
1117

1218
### Changed

Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project>
22
<!-- Common default project properties for ALL projects-->
33
<PropertyGroup>
4-
<VersionPrefix>1.12.1</VersionPrefix>
4+
<VersionPrefix>1.12.2</VersionPrefix>
55
<VersionSuffix></VersionSuffix>
66
<!-- This is overidden in test projects by setting to true-->
77
<IsTestProject>false</IsTestProject>

src/abstractions/ApiClientBuilder.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ public static ISerializationWriterFactory EnableBackingStoreForSerializationWrit
5050
if(registry != SerializationWriterFactoryRegistry.DefaultInstance)// if the registry is the default instance, we already enabled it above. No need to do it twice
5151
EnableBackingStoreForSerializationRegistry(SerializationWriterFactoryRegistry.DefaultInstance);
5252
}
53+
if(result is BackingStoreSerializationWriterProxyFactory)
54+
//We are already enabled so use it.
55+
return result;
5356
else
5457
result = new BackingStoreSerializationWriterProxyFactory(original);
5558

@@ -69,6 +72,9 @@ public static IParseNodeFactory EnableBackingStoreForParseNodeFactory(IParseNode
6972
if(registry != ParseNodeFactoryRegistry.DefaultInstance)// if the registry is the default instance, we already enabled it above. No need to do it twice
7073
EnableBackingStoreForParseNodeRegistry(ParseNodeFactoryRegistry.DefaultInstance);
7174
}
75+
if(result is BackingStoreParseNodeFactory)
76+
//We are already enabled so use it.
77+
return result;
7278
else
7379
result = new BackingStoreParseNodeFactory(original);
7480

@@ -80,7 +86,7 @@ private static void EnableBackingStoreForParseNodeRegistry(ParseNodeFactoryRegis
8086
var keysToUpdate = new List<string>();
8187
foreach(var entry in registry.ContentTypeAssociatedFactories)
8288
{
83-
if(entry.Value is not (BackingStoreSerializationWriterProxyFactory or SerializationWriterFactoryRegistry))
89+
if(entry.Value is not BackingStoreParseNodeFactory)
8490
{
8591
keysToUpdate.Add(entry.Key);
8692
}
@@ -97,7 +103,7 @@ private static void EnableBackingStoreForSerializationRegistry(SerializationWrit
97103
var keysToUpdate = new List<string>();
98104
foreach(var entry in registry.ContentTypeAssociatedFactories)
99105
{
100-
if(entry.Value is not (BackingStoreSerializationWriterProxyFactory or SerializationWriterFactoryRegistry))
106+
if(entry.Value is not BackingStoreSerializationWriterProxyFactory)
101107
{
102108
keysToUpdate.Add(entry.Key);
103109
}

tests/abstractions/ApiClientBuilderTests.cs

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,45 @@ public void EnableBackingStoreForParseNodeFactory()
6262
Assert.IsType<BackingStoreParseNodeFactory>(parseNodeRegistry.ContentTypeAssociatedFactories[StreamContentType]);
6363
}
6464

65+
[Fact]
66+
public void EnableBackingStoreForParseNodeFactoryMultipleCallsDoesNotDoubleWrap()
67+
{
68+
// Arrange
69+
//it is not normal to test a private field, but it is the purpose of the test
70+
var concreteFieldInfo = typeof(ParseNodeProxyFactory)
71+
.GetField("_concrete", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
72+
73+
74+
var parseNodeRegistry = new ParseNodeFactoryRegistry();
75+
var mockParseNodeFactory = new Mock<IAsyncParseNodeFactory>();
76+
parseNodeRegistry.ContentTypeAssociatedFactories.TryAdd(StreamContentType, mockParseNodeFactory.Object);
77+
78+
Assert.IsNotType<BackingStoreParseNodeFactory>(parseNodeRegistry.ContentTypeAssociatedFactories[StreamContentType]);
79+
80+
// Act
81+
var firstResult = ApiClientBuilder.EnableBackingStoreForParseNodeFactory(parseNodeRegistry);
82+
var secondResult = ApiClientBuilder.EnableBackingStoreForParseNodeFactory(firstResult);
83+
var thirdResult = ApiClientBuilder.EnableBackingStoreForParseNodeFactory(secondResult);
84+
85+
86+
//make sure the original was not modifed
87+
Assert.IsNotType<BackingStoreParseNodeFactory>(parseNodeRegistry);
88+
89+
// Assert the type has changed due to backing store enabling
90+
Assert.IsType<BackingStoreParseNodeFactory>(firstResult);
91+
92+
//make sure the second call returned the original wrapper
93+
Assert.Equal(firstResult, secondResult);
94+
Assert.Equal(firstResult, thirdResult);
95+
96+
//make sure what is in the registry is a BackingStore
97+
var factory = parseNodeRegistry.ContentTypeAssociatedFactories[StreamContentType];
98+
Assert.IsType<BackingStoreParseNodeFactory>(factory);
99+
100+
//make sure the concrete version of the factory is the same as the orginal
101+
Assert.Equal(mockParseNodeFactory.Object, concreteFieldInfo!.GetValue(factory));
102+
}
103+
65104
[Fact]
66105
public void EnableBackingStoreForParseNodeFactoryAlsoEnablesForDefaultInstance()
67106
{
@@ -80,5 +119,38 @@ public void EnableBackingStoreForParseNodeFactoryAlsoEnablesForDefaultInstance()
80119
Assert.IsType<BackingStoreParseNodeFactory>(parseNodeRegistry.ContentTypeAssociatedFactories[StreamContentType]);
81120
Assert.IsType<BackingStoreParseNodeFactory>(ParseNodeFactoryRegistry.DefaultInstance.ContentTypeAssociatedFactories[StreamContentType]);
82121
}
122+
123+
[Fact]
124+
public void EnableBackingStoreForParseNodeFactoryAlsoEnablesForDefaultInstanceMultipleCallsDoesNotDoubleWrap()
125+
{
126+
// Arrange
127+
var parseNodeRegistry = new ParseNodeFactoryRegistry();
128+
var mockParseNodeFactory = new Mock<IAsyncParseNodeFactory>();
129+
parseNodeRegistry.ContentTypeAssociatedFactories.TryAdd(StreamContentType, mockParseNodeFactory.Object);
130+
ParseNodeFactoryRegistry.DefaultInstance.ContentTypeAssociatedFactories.TryAdd(StreamContentType, mockParseNodeFactory.Object);
131+
132+
Assert.IsNotType<BackingStoreParseNodeFactory>(parseNodeRegistry.ContentTypeAssociatedFactories[StreamContentType]);
133+
134+
// Act
135+
var firstResult = ApiClientBuilder.EnableBackingStoreForParseNodeFactory(parseNodeRegistry);
136+
var secondResult = ApiClientBuilder.EnableBackingStoreForParseNodeFactory(firstResult);
137+
var thirdResult = ApiClientBuilder.EnableBackingStoreForParseNodeFactory(secondResult);
138+
139+
140+
//make sure the original was not modifed
141+
Assert.IsNotType<BackingStoreParseNodeFactory>(parseNodeRegistry);
142+
143+
// Assert the type has changed due to backing store enabling
144+
Assert.IsType<BackingStoreParseNodeFactory>(firstResult);
145+
146+
//make sure the second call returned the original wrapper
147+
Assert.Equal(firstResult, secondResult);
148+
Assert.Equal(firstResult, thirdResult);
149+
150+
//make sure what is in the registry is a BackingStore, it will be a new object so we can only check the type
151+
var factory = ParseNodeFactoryRegistry.DefaultInstance.ContentTypeAssociatedFactories[StreamContentType];
152+
Assert.IsType<BackingStoreParseNodeFactory>(factory);
153+
154+
}
83155
}
84156
}

0 commit comments

Comments
 (0)