Skip to content

Commit

Permalink
feat: v3/v4 pact support
Browse files Browse the repository at this point in the history
  • Loading branch information
YOU54F committed Aug 30, 2024
1 parent d8e70d7 commit dbba26e
Show file tree
Hide file tree
Showing 11 changed files with 1,463 additions and 47 deletions.
316 changes: 307 additions & 9 deletions src/Explore.Cli/PactMappingHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public static string getPactVersion(string json)
}


public static Connection MapPactInteractionToExploreConnection(PactV3.Interaction pactInteraction)
public static Connection MapPactV2InteractionToExploreConnection(PactV2.Interaction pactInteraction)
{
return new Connection()
{
Expand All @@ -73,7 +73,7 @@ public static Connection MapPactInteractionToExploreConnection(PactV3.Interactio
Url = ""
}
},
Paths = CreatePathsDictionary(pactInteraction.Request),
Paths = CreatePactV2PathsDictionary(pactInteraction.Request),
},
Settings = new Settings()
{
Expand All @@ -84,16 +84,171 @@ public static Connection MapPactInteractionToExploreConnection(PactV3.Interactio
},
};
}
public static Connection MapPactV3InteractionToExploreConnection(PactV3.Interaction pactInteraction)
{
return new Connection()
{
Type = "ConnectionRequest",
Name = "REST",
Schema = "OpenAPI",
SchemaVersion = "3.0.1",
ConnectionDefinition = new ConnectionDefinition()
{
Servers = new List<Server>()
{
new Server()
{
Url = ""
}
},
Paths = CreatePactV3PathsDictionary(pactInteraction.Request),
},
Settings = new Settings()
{
Type = "RestConnectionSettings",
ConnectTimeout = 30,
FollowRedirects = true,
EncodeUrl = true
},
};
}

public static Connection MapPactV4InteractionToExploreConnection(PactV4.Interaction pactInteraction)
{
return new Connection()
{
Type = "ConnectionRequest",
Name = "REST",
Schema = "OpenAPI",
SchemaVersion = "3.0.1",
ConnectionDefinition = new ConnectionDefinition()
{
Servers = new List<Server>()
{
new Server()
{
Url = ""
}
},
Paths = CreatePactV4PathsDictionary(pactInteraction.Request),
},
Settings = new Settings()
{
Type = "RestConnectionSettings",
ConnectTimeout = 30,
FollowRedirects = true,
EncodeUrl = true
},
};
}

public static Dictionary<string, object> CreatePactV2PathsDictionary(PactV2.Request? request)
{

if(request?.Path != null)
{
var pathsContent = new PathsContent()
{
Parameters = MapV2HeaderAndQueryParams(request)
};

// //add request body
if(request.Body != null)
{
var examplesJson = new Dictionary<string, object>
{
{ "examples", MapEntryBodyToContentExamples(request.Body.ToString()) }
};

var contentJson = new Dictionary<string, object>
{
{ "*/*", examplesJson }
};

pathsContent.RequestBody = new RequestBody()
{
Content = contentJson
};
}


// add header and query params
if(request.Method != null)
{
var methodJson = new Dictionary<string, object>
{
{ request.Method?.ToString()?.Replace("Method", string.Empty) ?? string.Empty, pathsContent }
};

var json = new Dictionary<string, object>
{
{ request.Path, methodJson }
};

return json;
}
}

return new Dictionary<string, object>();
}

public static Dictionary<string, object> CreatePactV3PathsDictionary(PactV3.Request? request)
{

if(request?.Path != null)
{
var pathsContent = new PathsContent()
{
Parameters = MapV3HeaderAndQueryParams(request)
};

// //add request body
if(request.Body != null)
{
var examplesJson = new Dictionary<string, object>
{
{ "examples", MapEntryBodyToContentExamples(request.Body.ToString()) }
};

var contentJson = new Dictionary<string, object>
{
{ "*/*", examplesJson }
};

pathsContent.RequestBody = new RequestBody()
{
Content = contentJson
};
}


// add header and query params
if(request.Method != null)
{
var methodJson = new Dictionary<string, object>
{
{ request.Method?.ToString()?.Replace("Method", string.Empty) ?? string.Empty, pathsContent }
};

var json = new Dictionary<string, object>
{
{ request.Path, methodJson }
};

return json;
}
}

public static Dictionary<string, object> CreatePathsDictionary(PactV3.Request? request)
return new Dictionary<string, object>();
}
public static Dictionary<string, object> CreatePactV4PathsDictionary(PactV4.Request? request)
{

if(request?.Path != null)
{
var pathsContent = new PathsContent()
{
Parameters = MapHeaderAndQueryParams(request)
Parameters = MapV4HeaderAndQueryParams(request)
};

// //add request body
Expand Down Expand Up @@ -121,7 +276,7 @@ public static Dictionary<string, object> CreatePathsDictionary(PactV3.Request? r
{
var methodJson = new Dictionary<string, object>
{
{ request.Method.ToString().Replace("Method", string.Empty), pathsContent }
{ request.Method?.ToString()?.Replace("Method", string.Empty) ?? string.Empty, pathsContent }
};

var json = new Dictionary<string, object>
Expand All @@ -147,7 +302,7 @@ public static Examples MapEntryBodyToContentExamples(string? rawBody)
};
}

public static List<Parameter> MapHeaderAndQueryParams(PactV3.Request? request)
public static List<Parameter> MapV2HeaderAndQueryParams(PactV2.Request? request)
{
List<Parameter> parameters = new List<Parameter>();

Expand Down Expand Up @@ -197,17 +352,18 @@ public static List<Parameter> MapHeaderAndQueryParams(PactV3.Request? request)
{
if(request.Query != null)
{
foreach(var param in request.Query)

foreach(var param in request.Query.Split("&"))
{
parameters.Add(new Parameter()
{
In = "query",
Name = param.Key,
Name = param.Split("=")[0],
Examples = new Examples()
{
Example = new Example()
{
Value = param.Value.ToString()
Value = param.Split("=")[1]
}
}
});
Expand All @@ -218,5 +374,147 @@ public static List<Parameter> MapHeaderAndQueryParams(PactV3.Request? request)
return parameters;
}

public static List<Parameter> MapV3HeaderAndQueryParams(PactV3.Request? request)
{
List<Parameter> parameters = new List<Parameter>();

if (request?.Headers != null && request.Headers.Any())
{
// map the headers
foreach (var hdr in request.Headers)
{
parameters.Add(new Parameter()
{
In = "header",
Name = hdr.Key,
Examples = new Examples()
{
Example = new Example()
{
Value = hdr.Value.ToString()
}
}
});
}
}

// parse and map the query string
if (request?.Query != null)
{
if (request.Query != null)
{
foreach (var param in request.Query)
{
if (param.Value.Length > 1)
{
foreach (var value in param.Value)
{
parameters.Add(new Parameter()
{
In = "query",
Name = $"{param.Key}[]",
Examples = new Examples()
{
Example = new Example()
{
Value = value.ToString()
}
}
});
}
}
else
{
parameters.Add(new Parameter()
{
In = "query",
Name = param.Key,
Examples = new Examples()
{
Example = new Example()
{
Value = param.Value.First().ToString()
}
}
});
}
}
}
}

return parameters;
}

public static List<Parameter> MapV4HeaderAndQueryParams(PactV4.Request? request)
{
List<Parameter> parameters = new List<Parameter>();

if (request?.Headers != null && request.Headers.Any())
{
// map the headers
foreach (var hdr in request.Headers)
{
parameters.Add(new Parameter()
{
In = "header",
Name = hdr.Key,
Examples = new Examples()
{
Example = new Example()
{
Value = string.Join(",", hdr.Value.Select(x => x.ToString()))
}
}
});
}
}

// parse and map the query string
if (request?.Query != null)
{
if (request.Query != null)
{
foreach (var param in request.Query)
{
if (param.Value.Length > 1)
{
foreach (var value in param.Value)
{
parameters.Add(new Parameter()
{
In = "query",
Name = $"{param.Key}[]",
Examples = new Examples()
{
Example = new Example()
{
Value = value.ToString()
}
}
});
}
}
else
{
parameters.Add(new Parameter()
{
In = "query",
Name = param.Key,
Examples = new Examples()
{
Example = new Example()
{
Value = param.Value.First().ToString()
}
}
});
}
}
}
}

return parameters;
}

}

2 changes: 1 addition & 1 deletion src/Explore.Cli/PactV3Contract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public partial class Request


[JsonPropertyName("query")]
public Dictionary<string, object> Query { get; set; }
public Dictionary<string, string[]> Query { get; set; }
}

public partial class RequestGenerators
Expand Down
Loading

0 comments on commit dbba26e

Please sign in to comment.