OpenApiLINQPadDriver is LINQPad 7/8 dynamic data context driver for creating C# clients based on Open API/Swagger specifications
- Specification is read using NJsonSchema and clients are generated using NSwag
- This project
- Original project for LINQPad 5
- UI is heavily inspired by CsvLINQPadDriver
generation of lpx6 files is on the roadmap, for now we only support instalation via nuget
- Open LINQPad 7/8.
- Click
Add connectionlink. - Click button
View more drivers... - Click radio button
Show all driversand typeOpenApiLINQPadDriver(for now it is also required to checkInclude Prereleasecheckbox) - Install.
- In case of working in environments without internet access it is possible to manually download nuget package and configure
Package Sourceto point to a folder where it is located
Open API Connection can be added the same way as any other connection.
- Click
Add Connection - Select
OpenApi Driver - Enter
Open Api/Swagger Urior clickGet from diskand pick it from file - Manually enter
API Urior clickGet from Open Api document, if servers are found in the specification then uri of the first one will be picked - Set settings
- Click
OK - Client should start generation, you can use it by right clicking on it and choosing
Use in Current Queryor by picking it fromConnectionselect - It is possible to drag method name from the tree view on the left to the query
- Example code using PetStore API
async Task Main()
{
var newPetId = System.Random.Shared.NextInt64();
await PetClient.AddPetAsync(new Pet()
{
Id = newPetId,
Name = "Dino",
Category = new Category
{
Id = 123,
Name = "Dog"
}
});
await PetClient.GetPetByIdAsync(newPetId).Dump();
}- Right click on the connection and click
Refresh - Or use shortcut
Shift+Alt+D
- Endpoint grouping - how methods will be grouped in generated client
Multiple clients from first tag and operationName- usually first tag corresponds to ASP.NET controller, so this will group methods by controllerSingle client from OperationId and OperationName- this will put all endpoints in one class
- Json library - library used in generated client for serialization/deserialization, for specification reading NJsonSchema uses
Newstonsoft.JsonSystem.Text.JsonNewstonsoft.Json
- Class style
POCOs (Plain Old C# Objects)Classes implementing the INotifyPropertyChanged interfaceClasses implementing the Prism base classRecords - read only POCOs (Plain Old C# Objects)
- Generate sync methods - by default sync methods will not be generated
- Build in Release - Build generated code in Release, default:
false
- Debug info: show additional driver debug info, e.g. generated data context sources, add
Execution Timesexplorer item with execution times of parts of the generation pipeline and will add warnings from the compilation if any were present - Remember this connection: connection will be available on next run.
- Contains production data: files contain production data.
- Each generated client has
PrepareRequestFunctionFunc, forMultiple clients from first tag and operationNamemode, helper set only Func is also generted to set them all at once - This Func will be run on each method exectuion before making a http request, it is run in
PrepareRequestpartial methods generated by NSwag - It can be used to set additional headers or other http client settings
- Example usage
async Task Main()
{
PrepareRequestFunction = (httpClient, requestMessage, url) =>
{
requestMessage.Headers.Add("UserId", "9");
requestMessage.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", "<token>");
};
}async Task Main()
{
PrepareRequestFunction = PrepareRequest;
}
private void PrepareRequest(HttpClient httpClient, HttpRequestMessage requestMessage, string url)
{
requestMessage.Headers.Add("UserId", "9");
requestMessage.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", "<token>");
}- NJsonSchema
- NSwag
- Newtonsoft.Json - required by NSwag, included to bump version
- OpenApiLINQPadDriver.csproj contains special
Debug_Publish_To_LINQPad_Folderdebug build configuration, if it is chosen, code will be build only targetingnet8.0-windowswith additional properties - LINQPad can pick drivers from
\LINQPad\Drivers\DataContext\NetCorefolder - Additionaly when exceptions will be thrown it will be possible to attach a debugger
- Allow injection of own httpClient
- Unit tests
PrepareRequestwith string builder overloadProcessResponsePrepareRequestandProcessResponseasync overload that could be set via a setting- Methods parameters and responses in tree view
- Auto dump response
- Auth helper methods eg.
SetBearerToken - When multiple servers are found allow selection
- LINQPad 5 support
- Examples (include in the nuget) - possibly the same ones could be used in testing
- Expose JsonSerializerSettings setter on multi client setup
- Expose ReadResponseAsString on multi client setup
- Treat warnings as errors in generated code (
generalDiagnosticOption: ReportDiagnostic.Error)