Skip to content

Commit 838c6a8

Browse files
committed
Fix construction of path in OpenApiParser
1 parent be55022 commit 838c6a8

File tree

8 files changed

+86
-14
lines changed

8 files changed

+86
-14
lines changed

src/WireMock.Net.OpenApiParser/Mappers/OpenApiPathsMapper.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,8 @@ private MappingModel MapOperationToMappingModel(string path, string httpMethod,
102102
Guid = Guid.NewGuid(),
103103
Request = new RequestModel
104104
{
105-
Methods = new[] { httpMethod },
106-
Path = MapBasePath(servers) + MapPathWithParameters(path, pathParameters),
105+
Methods = [httpMethod],
106+
Path = PathUtils.Combine(MapBasePath(servers), MapPathWithParameters(path, pathParameters)),
107107
Params = MapQueryParameters(queryParameters),
108108
Headers = MapRequestHeaders(headers),
109109
Body = requestBodyModel
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Copyright © WireMock.Net
2+
3+
using System.Runtime.CompilerServices;
4+
5+
[assembly: InternalsVisibleTo("WireMock.Net.Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100e138ec44d93acac565953052636eb8d5e7e9f27ddb030590055cd1a0ab2069a5623f1f77ca907d78e0b37066ca0f6d63da7eecc3fcb65b76aa8ebeccf7ebe1d11264b8404cd9b1cbbf2c83f566e033b3e54129f6ef28daffff776ba7aebbc53c0d635ebad8f45f78eb3f7e0459023c218f003416e080f96a1a3c5ffeb56bee9e")]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright © WireMock.Net
2+
3+
namespace WireMock.Net.OpenApiParser.Utils;
4+
5+
internal static class PathUtils
6+
{
7+
internal static string Combine(params string[] paths)
8+
{
9+
if (paths.Length == 0)
10+
{
11+
return string.Empty;
12+
}
13+
14+
var result = paths[0].Trim().TrimEnd('/');
15+
16+
for (int i = 1; i < paths.Length; i++)
17+
{
18+
var nextPath = paths[i].Trim().TrimStart('/').TrimEnd('/');
19+
if (!string.IsNullOrEmpty(nextPath))
20+
{
21+
result += '/' + nextPath;
22+
}
23+
}
24+
25+
return result;
26+
}
27+
}

src/WireMock.Net/Handlers/LocalFileSystemHandler.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -84,20 +84,20 @@ public virtual void WriteMappingFile(string path, string text)
8484
public virtual byte[] ReadResponseBodyAsFile(string path)
8585
{
8686
Guard.NotNullOrEmpty(path);
87-
path = PathUtils.CleanPath(path)!;
87+
path = FilePathUtils.CleanPath(path)!;
8888
// If the file exists at the given path relative to the MappingsFolder, then return that.
8989
// Else the path will just be as-is.
90-
return File.ReadAllBytes(File.Exists(PathUtils.Combine(GetMappingFolder(), path)) ? PathUtils.Combine(GetMappingFolder(), path) : path);
90+
return File.ReadAllBytes(File.Exists(FilePathUtils.Combine(GetMappingFolder(), path)) ? FilePathUtils.Combine(GetMappingFolder(), path) : path);
9191
}
9292

9393
/// <inheritdoc cref="IFileSystemHandler.ReadResponseBodyAsString"/>
9494
public virtual string ReadResponseBodyAsString(string path)
9595
{
9696
Guard.NotNullOrEmpty(path);
97-
path = PathUtils.CleanPath(path)!;
97+
path = FilePathUtils.CleanPath(path)!;
9898
// In case the path is a filename, the path will be adjusted to the MappingFolder.
9999
// Else the path will just be as-is.
100-
return File.ReadAllText(File.Exists(PathUtils.Combine(GetMappingFolder(), path)) ? PathUtils.Combine(GetMappingFolder(), path) : path);
100+
return File.ReadAllText(File.Exists(FilePathUtils.Combine(GetMappingFolder(), path)) ? FilePathUtils.Combine(GetMappingFolder(), path) : path);
101101
}
102102

103103
/// <inheritdoc cref="IFileSystemHandler.FileExists"/>
@@ -124,7 +124,7 @@ public virtual void WriteFile(string folder, string filename, byte[] bytes)
124124
Guard.NotNullOrEmpty(filename);
125125
Guard.NotNull(bytes);
126126

127-
File.WriteAllBytes(PathUtils.Combine(folder, filename), bytes);
127+
File.WriteAllBytes(FilePathUtils.Combine(folder, filename), bytes);
128128
}
129129

130130
/// <inheritdoc cref="IFileSystemHandler.DeleteFile"/>

src/WireMock.Net/Util/PathUtils.cs src/WireMock.Net/Util/FilePathUtils.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
namespace WireMock.Util;
77

8-
internal static class PathUtils
8+
internal static class FilePathUtils
99
{
1010
/// <summary>
1111
/// Robust handling of the user defined path.
@@ -23,7 +23,7 @@ internal static class PathUtils
2323
/// <param name="path">The path to remove the loading DirectorySeparatorChars</param>
2424
public static string? RemoveLeadingDirectorySeparators(string? path)
2525
{
26-
return path?.TrimStart(new[] { Path.DirectorySeparatorChar });
26+
return path?.TrimStart(Path.DirectorySeparatorChar);
2727
}
2828

2929
/// <summary>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright © WireMock.Net
2+
3+
#if !(NET452 || NET461 || NETCOREAPP3_1)
4+
using FluentAssertions;
5+
using WireMock.Net.OpenApiParser.Utils;
6+
using Xunit;
7+
8+
namespace WireMock.Net.Tests.OpenApiParser;
9+
10+
public class PathUtilsTests
11+
{
12+
[Theory]
13+
[InlineData(new string[] { }, "")]
14+
[InlineData(new[] { "path1" }, "path1")]
15+
[InlineData(new[] { "/path1" }, "/path1")]
16+
[InlineData(new[] { "/path1/" }, "/path1")]
17+
public void Combine_ShouldReturnCombinedPathTest1(string[] paths, string expected)
18+
{
19+
// Act
20+
var result = PathUtils.Combine(paths);
21+
22+
// Assert
23+
result.Should().Be(expected);
24+
}
25+
26+
[Theory]
27+
[InlineData("/path1", "path2")]
28+
[InlineData("/path1/", "path2")]
29+
[InlineData("/path1", "/path2")]
30+
[InlineData("/path1", "path2/")]
31+
public void Combine_ShouldReturnCombinedPathTest2(params string[] paths)
32+
{
33+
// Act
34+
var result = PathUtils.Combine(paths);
35+
36+
// Assert
37+
result.Should().Be("/path1/path2");
38+
}
39+
}
40+
#endif

test/WireMock.Net.Tests/OpenApiParser/payroc-openapi-spec.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ info:
88
url: https://docs.payroc.com/api
99
email: helpdesk@payroc.com
1010
servers:
11-
- url: https://api.payroc.com/v1
11+
- url: https://api.payroc.com/v1/
1212
description: External URL
1313
security:
1414
- bearerAuth: []

test/WireMock.Net.Tests/Util/PathUtilsTests.cs test/WireMock.Net.Tests/Util/FilePathUtilsTests.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@
77

88
namespace WireMock.Net.Tests.Util;
99

10-
public class PathUtilsTests
10+
public class FilePathUtilsTests
1111
{
1212
[Theory]
1313
[InlineData(@"subdirectory/MyXmlResponse.xml")]
1414
[InlineData(@"subdirectory\MyXmlResponse.xml")]
1515
public void PathUtils_CleanPath(string path)
1616
{
1717
// Act
18-
var cleanPath = PathUtils.CleanPath(path);
18+
var cleanPath = FilePathUtils.CleanPath(path);
1919

2020
// Assert
2121
Check.That(cleanPath).Equals("subdirectory" + Path.DirectorySeparatorChar + "MyXmlResponse.xml");
@@ -34,10 +34,10 @@ public void PathUtils_CleanPath(string path)
3434
public void PathUtils_CleanPath_RemoveLeadingDirectorySeparators(string path, string expected)
3535
{
3636
// Arrange
37-
var cleanPath = PathUtils.CleanPath(path);
37+
var cleanPath = FilePathUtils.CleanPath(path);
3838

3939
// Act
40-
var withoutDirectorySeparators = PathUtils.RemoveLeadingDirectorySeparators(cleanPath);
40+
var withoutDirectorySeparators = FilePathUtils.RemoveLeadingDirectorySeparators(cleanPath);
4141

4242
// Assert
4343
Check.That(withoutDirectorySeparators).Equals(expected);

0 commit comments

Comments
 (0)