Mapping headers in OpenAPI (#644)

* Support edge case: first object, next an array.

* Add mapping to header parameters.

* Refactor the method MapHeadersParameters to MapHeaders
This commit is contained in:
Daniel L. Romero
2021-10-07 12:15:26 -05:00
committed by GitHub
parent 34083d826e
commit 267f0b7b6d

View File

@@ -45,6 +45,8 @@ namespace WireMock.Net.OpenApiParser.Mappers
{
var queryParameters = operation.Parameters.Where(p => p.In == ParameterLocation.Query);
var pathParameters = operation.Parameters.Where(p => p.In == ParameterLocation.Path);
var headers = operation.Parameters.Where(p => p.In == ParameterLocation.Header);
var response = operation.Responses.FirstOrDefault();
TryGetContent(response.Value?.Content, out OpenApiMediaType responseContent, out string responseContentType);
@@ -65,7 +67,8 @@ namespace WireMock.Net.OpenApiParser.Mappers
{
Methods = new[] { httpMethod },
Path = MapPathWithParameters(path, pathParameters),
Params = MapQueryParameters(queryParameters)
Params = MapQueryParameters(queryParameters),
Headers = MapHeaders(headers)
},
Response = new ResponseModel
{
@@ -251,6 +254,26 @@ namespace WireMock.Net.OpenApiParser.Mappers
return list.Any() ? list : null;
}
private IList<HeaderModel> MapHeaders(IEnumerable<OpenApiParameter> headers)
{
var list = headers
.Select(qp => new HeaderModel
{
Name = qp.Name,
Matchers = new[]
{
new MatcherModel
{
Name = "ExactMatcher",
Pattern = GetDefaultValueAsStringForSchemaType(qp.Schema)
}
}
})
.ToList();
return list.Any() ? list : null;
}
private string GetDefaultValueAsStringForSchemaType(OpenApiSchema schema)
{
var value = _exampleValueGenerator.GetExampleValue(schema);