Skip to content

Commit b3f797b

Browse files
committed
Fix merging arrays with different lengths
1 parent aebcda3 commit b3f797b

File tree

3 files changed

+65
-3
lines changed

3 files changed

+65
-3
lines changed

dotnet-json.Tests/Commands/MergeCommandTests.cs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,68 @@ await File.WriteAllTextAsync(Path.Join(_tmpDir, "b.json"), @"{
181181
}");
182182
}
183183

184+
[Fact]
185+
public async Task MergeWithArraysWorkCorrectly()
186+
{
187+
await File.WriteAllTextAsync(Path.Join(_tmpDir, "a.json"), @"{
188+
""Array"": [
189+
""item 1"",
190+
""item 2""
191+
]
192+
}");
193+
194+
await File.WriteAllTextAsync(Path.Join(_tmpDir, "b.json"), @"{
195+
""Array"": [
196+
""1 item"",
197+
null,
198+
""3 item""
199+
]
200+
}");
201+
202+
var (exitCode, console) = await RunCommand(
203+
Path.Join(_tmpDir, "a.json"),
204+
Path.Join(_tmpDir, "b.json"));
205+
206+
exitCode.Should().Be(0);
207+
208+
var content = await File.ReadAllTextAsync(Path.Join(_tmpDir, "a.json"));
209+
content.Should().Be(@"{
210+
""Array"": [
211+
""1 item"",
212+
null,
213+
""3 item""
214+
]
215+
}");
216+
}
217+
218+
[Fact]
219+
public async Task MergeWithNewArrayWorkCorrectly()
220+
{
221+
await File.WriteAllTextAsync(Path.Join(_tmpDir, "a.json"), @"{
222+
}");
223+
224+
await File.WriteAllTextAsync(Path.Join(_tmpDir, "b.json"), @"{
225+
""Array"": [
226+
""item 1"",
227+
""item 2""
228+
]
229+
}");
230+
231+
var (exitCode, console) = await RunCommand(
232+
Path.Join(_tmpDir, "a.json"),
233+
Path.Join(_tmpDir, "b.json"));
234+
235+
exitCode.Should().Be(0);
236+
237+
var content = await File.ReadAllTextAsync(Path.Join(_tmpDir, "a.json"));
238+
content.Should().Be(@"{
239+
""Array"": [
240+
""item 1"",
241+
""item 2""
242+
]
243+
}");
244+
}
245+
184246
private async Task<(int exitCode, IConsole console)> RunCommand(params string[] args)
185247
{
186248
var command = new MergeCommand();

dotnet-json/Core/JsonDocument.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,10 @@ internal static IEnumerable<KeyValuePair<string, JToken>> AllTokens(JToken token
121121
if (kv.Key == key)
122122
return kv.Value;
123123

124-
if (key.StartsWith(kv.Key) && kv.Value is JObject jObject && kv.Key.Length > parentKey.Length)
124+
if (key.StartsWith(kv.Key) && kv.Key.Length > parentKey.Length && (kv.Value is JObject or JArray))
125125
{
126126
parentKey = kv.Key;
127-
bestParent = jObject;
127+
bestParent = kv.Value;
128128
}
129129
}
130130

dotnet-json/dotnet-json.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<ToolCommandName>dotnet-json</ToolCommandName>
1515
<PackageOutputPath>./nupkg</PackageOutputPath>
1616

17-
<Version>1.1.0</Version>
17+
<Version>1.1.1</Version>
1818
<PackageId>dotnet-json</PackageId>
1919
<Authors>sleeuwen</Authors>
2020
<RepositoryUrl>https://github.com/sleeuwen/dotnet-json</RepositoryUrl>

0 commit comments

Comments
 (0)