mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-04-23 16:58:27 +02:00
Support basepath from servers (#675)
* Support basepath from servers * Refactor BasePath * Comments applied
This commit is contained in:
@@ -26,22 +26,22 @@ namespace WireMock.Net.OpenApiParser.Mappers
|
|||||||
_exampleValueGenerator = new ExampleValueGenerator(settings);
|
_exampleValueGenerator = new ExampleValueGenerator(settings);
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerable<MappingModel> ToMappingModels(OpenApiPaths paths)
|
public IEnumerable<MappingModel> ToMappingModels(OpenApiPaths paths, IList<OpenApiServer> servers)
|
||||||
{
|
{
|
||||||
return paths.Select(p => MapPath(p.Key, p.Value)).SelectMany(x => x);
|
return paths.Select(p => MapPath(p.Key, p.Value, servers)).SelectMany(x => x);
|
||||||
}
|
}
|
||||||
|
|
||||||
private IEnumerable<MappingModel> MapPaths(OpenApiPaths paths)
|
private IEnumerable<MappingModel> MapPaths(OpenApiPaths paths, IList<OpenApiServer> servers)
|
||||||
{
|
{
|
||||||
return paths.Select(p => MapPath(p.Key, p.Value)).SelectMany(x => x);
|
return paths.Select(p => MapPath(p.Key, p.Value, servers)).SelectMany(x => x);
|
||||||
}
|
}
|
||||||
|
|
||||||
private IEnumerable<MappingModel> MapPath(string path, OpenApiPathItem pathItem)
|
private IEnumerable<MappingModel> MapPath(string path, OpenApiPathItem pathItem, IList<OpenApiServer> servers)
|
||||||
{
|
{
|
||||||
return pathItem.Operations.Select(o => MapOperationToMappingModel(path, o.Key.ToString().ToUpperInvariant(), o.Value));
|
return pathItem.Operations.Select(o => MapOperationToMappingModel(path, o.Key.ToString().ToUpperInvariant(), o.Value, servers));
|
||||||
}
|
}
|
||||||
|
|
||||||
private MappingModel MapOperationToMappingModel(string path, string httpMethod, OpenApiOperation operation)
|
private MappingModel MapOperationToMappingModel(string path, string httpMethod, OpenApiOperation operation, IList<OpenApiServer> servers)
|
||||||
{
|
{
|
||||||
var queryParameters = operation.Parameters.Where(p => p.In == ParameterLocation.Query);
|
var queryParameters = operation.Parameters.Where(p => p.In == ParameterLocation.Query);
|
||||||
var pathParameters = operation.Parameters.Where(p => p.In == ParameterLocation.Path);
|
var pathParameters = operation.Parameters.Where(p => p.In == ParameterLocation.Path);
|
||||||
@@ -69,7 +69,7 @@ namespace WireMock.Net.OpenApiParser.Mappers
|
|||||||
Request = new RequestModel
|
Request = new RequestModel
|
||||||
{
|
{
|
||||||
Methods = new[] { httpMethod },
|
Methods = new[] { httpMethod },
|
||||||
Path = MapPathWithParameters(path, pathParameters),
|
Path = MapBasePath(servers) + MapPathWithParameters(path, pathParameters),
|
||||||
Params = MapQueryParameters(queryParameters),
|
Params = MapQueryParameters(queryParameters),
|
||||||
Headers = MapRequestHeaders(headers)
|
Headers = MapRequestHeaders(headers)
|
||||||
},
|
},
|
||||||
@@ -230,6 +230,22 @@ namespace WireMock.Net.OpenApiParser.Mappers
|
|||||||
return newPath;
|
return newPath;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private string MapBasePath(IList<OpenApiServer> servers)
|
||||||
|
{
|
||||||
|
if (servers == null || servers.Count == 0)
|
||||||
|
{
|
||||||
|
return string.Empty;
|
||||||
|
}
|
||||||
|
|
||||||
|
OpenApiServer server = servers.First();
|
||||||
|
Uri uriResult;
|
||||||
|
if (Uri.TryCreate(server.Url, UriKind.RelativeOrAbsolute, out uriResult))
|
||||||
|
{
|
||||||
|
return uriResult.IsAbsoluteUri ? uriResult.AbsolutePath : uriResult.ToString();
|
||||||
|
}
|
||||||
|
return string.Empty;
|
||||||
|
}
|
||||||
|
|
||||||
private JToken MapOpenApiAnyToJToken(IOpenApiAny any)
|
private JToken MapOpenApiAnyToJToken(IOpenApiAny any)
|
||||||
{
|
{
|
||||||
if (any == null)
|
if (any == null)
|
||||||
|
|||||||
@@ -62,7 +62,7 @@ namespace WireMock.Net.OpenApiParser
|
|||||||
[PublicAPI]
|
[PublicAPI]
|
||||||
public IEnumerable<MappingModel> FromDocument(OpenApiDocument openApiDocument, WireMockOpenApiParserSettings settings = null)
|
public IEnumerable<MappingModel> FromDocument(OpenApiDocument openApiDocument, WireMockOpenApiParserSettings settings = null)
|
||||||
{
|
{
|
||||||
return new OpenApiPathsMapper(settings).ToMappingModels(openApiDocument.Paths);
|
return new OpenApiPathsMapper(settings).ToMappingModels(openApiDocument.Paths, openApiDocument.Servers);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user