Skip to content

Commit aa56004

Browse files
Copilotphilnach
andcommitted
Address code review feedback: handle leading slashes and reduce code duplication
Co-authored-by: philnach <[email protected]>
1 parent f247d72 commit aa56004

File tree

2 files changed

+38
-18
lines changed

2 files changed

+38
-18
lines changed

Extensions/Cosmos/Cosmos.DataTransfer.CosmosExtension.UnitTests/CosmosDataSinkExtensionTests.cs

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,13 @@ public static bool HasProperty(object obj, string name)
367367
return values.ContainsKey(name);
368368
}
369369

370+
private static string? InvokeGetPropertyValue(ExpandoObject item, string propertyName)
371+
{
372+
var method = typeof(CosmosDataSinkExtension).GetMethod("GetPropertyValue",
373+
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static);
374+
return method?.Invoke(null, new object[] { item, propertyName }) as string;
375+
}
376+
370377
[TestMethod]
371378
public void GetPropertyValue_WithSimpleProperty_ReturnsValue()
372379
{
@@ -376,10 +383,8 @@ public void GetPropertyValue_WithSimpleProperty_ReturnsValue()
376383
dict["id"] = "test-id-123";
377384
dict["name"] = "test-name";
378385

379-
// Act - Use reflection to call the private GetPropertyValue method
380-
var method = typeof(CosmosDataSinkExtension).GetMethod("GetPropertyValue",
381-
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static);
382-
var result = method?.Invoke(null, new object[] { expando, "id" });
386+
// Act
387+
var result = InvokeGetPropertyValue(expando, "id");
383388

384389
// Assert
385390
Assert.IsNotNull(result);
@@ -402,10 +407,8 @@ public void GetPropertyValue_WithNestedProperty_ReturnsValue()
402407

403408
dict["partitionkeyvalue1"] = nestedExpando;
404409

405-
// Act - Use reflection to call the private GetPropertyValue method
406-
var method = typeof(CosmosDataSinkExtension).GetMethod("GetPropertyValue",
407-
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static);
408-
var result = method?.Invoke(null, new object[] { expando, "partitionkeyvalue1/partitionkeyvalue2" });
410+
// Act
411+
var result = InvokeGetPropertyValue(expando, "partitionkeyvalue1/partitionkeyvalue2");
409412

410413
// Assert
411414
Assert.IsNotNull(result);
@@ -435,9 +438,7 @@ public void GetPropertyValue_WithDeeplyNestedProperty_ReturnsValue()
435438
dict["level1"] = level1;
436439

437440
// Act
438-
var method = typeof(CosmosDataSinkExtension).GetMethod("GetPropertyValue",
439-
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static);
440-
var result = method?.Invoke(null, new object[] { expando, "level1/level2/level3/finalValue" });
441+
var result = InvokeGetPropertyValue(expando, "level1/level2/level3/finalValue");
441442

442443
// Assert
443444
Assert.IsNotNull(result);
@@ -459,9 +460,7 @@ public void GetPropertyValue_WithMissingNestedProperty_ReturnsNull()
459460
dict["parent"] = nestedExpando;
460461

461462
// Act
462-
var method = typeof(CosmosDataSinkExtension).GetMethod("GetPropertyValue",
463-
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static);
464-
var result = method?.Invoke(null, new object[] { expando, "parent/nonExistentKey" });
463+
var result = InvokeGetPropertyValue(expando, "parent/nonExistentKey");
465464

466465
// Assert
467466
Assert.IsNull(result);
@@ -477,12 +476,32 @@ public void GetPropertyValue_WithNullIntermediateValue_ReturnsNull()
477476
dict["parent"] = null;
478477

479478
// Act
480-
var method = typeof(CosmosDataSinkExtension).GetMethod("GetPropertyValue",
481-
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static);
482-
var result = method?.Invoke(null, new object[] { expando, "parent/child" });
479+
var result = InvokeGetPropertyValue(expando, "parent/child");
483480

484481
// Assert
485482
Assert.IsNull(result);
486483
}
484+
485+
[TestMethod]
486+
public void GetPropertyValue_WithLeadingSlash_WorksCorrectly()
487+
{
488+
// Arrange - Test that paths with leading slash work (defensive programming)
489+
var expando = new ExpandoObject();
490+
var dict = (IDictionary<string, object?>)expando;
491+
dict["id"] = "test-id";
492+
493+
var nestedExpando = new ExpandoObject();
494+
var nestedDict = (IDictionary<string, object?>)nestedExpando;
495+
nestedDict["partitionkey"] = "pk-value";
496+
497+
dict["parent"] = nestedExpando;
498+
499+
// Act - Path with leading slash (though TrimStart is called in actual code)
500+
var result = InvokeGetPropertyValue(expando, "/parent/partitionkey");
501+
502+
// Assert
503+
Assert.IsNotNull(result);
504+
Assert.AreEqual("pk-value", result);
505+
}
487506
}
488507
}

Extensions/Cosmos/Cosmos.DataTransfer.CosmosExtension/CosmosDataSinkExtension.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,8 @@ private static MemoryStream CreateItemStream(ExpandoObject item)
287287
private static string? GetPropertyValue(ExpandoObject item, string propertyName)
288288
{
289289
// Handle nested property paths (e.g., "property1/property2/property3")
290-
var pathSegments = propertyName.Split('/');
290+
// Split and remove empty entries to handle paths starting with '/' (e.g., "/property/nested")
291+
var pathSegments = propertyName.Split('/', StringSplitOptions.RemoveEmptyEntries);
291292
object? current = item;
292293

293294
foreach (var segment in pathSegments)

0 commit comments

Comments
 (0)