Skip to content

Commit

Permalink
Fixes matching paths in ApiCenterOnboardingPlugin (dotnet#727)
Browse files Browse the repository at this point in the history
  • Loading branch information
waldekmastykarz authored May 17, 2024
1 parent 9ab4177 commit e1d593c
Showing 1 changed file with 7 additions and 16 deletions.
23 changes: 7 additions & 16 deletions dev-proxy-plugins/RequestLogs/ApiCenterOnboardingPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -441,22 +441,13 @@ async Task ImportApiDefinition(string apiDefinitionId, string openApiSpecFilePat
{
_logger?.LogDebug("Path {urlPath} contains parameters and will be converted to Regex", urlPathFromSpec);

// we need this check due to a bug/limitation in OpenAPI.NET
// which doesn't include parameters defined by $ref in the
// Parameters collection, so we can have parameters in the path
// that are not in the Parameters collection
if (urlPathFromSpec.Count(c => c == '{') == path.Value.Parameters.Count)
{
foreach (var parameter in path.Value.Parameters)
{
urlPathFromSpec = urlPathFromSpec.Replace($"{{{parameter.Name}}}", $"([^/]+)");
}
}
else
{
// force replace all parameters with regex
urlPathFromSpec = Regex.Replace(urlPathFromSpec, @"\{[^}]+\}", $"([^/]+)");
}
// force replace all parameters with regex
// this is more robust than replacing parameters by name
// because it's possible to define parameters both on the path
// and operations and sometimes, parameters are defined only
// on the operation. This way, we cover all cases, and we don't
// care about the parameter anyway here
urlPathFromSpec = Regex.Replace(urlPathFromSpec, @"\{[^}]+\}", $"([^/]+)");

_logger?.LogDebug("Converted path to Regex: {urlPath}", urlPathFromSpec);
var regex = new Regex($"^{urlPathFromSpec}$");
Expand Down

0 comments on commit e1d593c

Please sign in to comment.