OpenApiLINQPadDriver 0.0.10-alpha
dotnet add package OpenApiLINQPadDriver --version 0.0.10-alpha
NuGet\Install-Package OpenApiLINQPadDriver -Version 0.0.10-alpha
<PackageReference Include="OpenApiLINQPadDriver" Version="0.0.10-alpha" />
<PackageVersion Include="OpenApiLINQPadDriver" Version="0.0.10-alpha" />
<PackageReference Include="OpenApiLINQPadDriver" />
paket add OpenApiLINQPadDriver --version 0.0.10-alpha
#r "nuget: OpenApiLINQPadDriver, 0.0.10-alpha"
#:package OpenApiLINQPadDriver@0.0.10-alpha
#addin nuget:?package=OpenApiLINQPadDriver&version=0.0.10-alpha&prerelease
#tool nuget:?package=OpenApiLINQPadDriver&version=0.0.10-alpha&prerelease
OpenApiLINQPadDriver for LINQPad 7/8
Description
OpenApiLINQPadDriver is LINQPad 7/8/9 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
Websites
- This project
- Original project for LINQPad 5
- UI is heavily inspired by CsvLINQPadDriver
Downloads
generation of lpx6 files is on the roadmap, for now we only support instalation via nuget
Prerequisites
Installation
LINQPad 7/8
NuGet
- Open LINQPad.
- 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
Usage
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();
}
Refreshing client
- Right click on the connection and click
Refresh - Or use shortcut
Shift+Alt+D
Configuration Options
Client Generation
- 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
Misc
- 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.
PrepareRequestFunction
- 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>");
}
Custom HTTP transport
The driver provides OpenApiHttpClient, which allows the underlying HTTP transport to be configured before the first request is sent.
This can be used for non-TCP transports, such as the Docker Desktop Windows named pipe.
using System.IO.Pipes;
using System.Net;
using System.Net.Http;
async Task Main()
{
PrepareRequestFunction = (httpClient, request, _) =>
{
if (httpClient is not OpenApiHttpClient openApiHttpClient)
{
throw new InvalidOperationException("The driver did not provide an OpenApiHttpClient.");
}
openApiHttpClient.ConfigureTransportOnce(
() => new SocketsHttpHandler
{
UseProxy = false,
// actual API URI provided from settings is ignored so it can be set to some dummy value like http://docker
ConnectCallback = async (_, cancellationToken) =>
{
var pipe = new NamedPipeClientStream(
serverName: ".",
pipeName: "docker_engine",
direction: PipeDirection.InOut,
options: PipeOptions.Asynchronous);
try
{
await pipe.ConnectAsync(cancellationToken)
.ConfigureAwait(false);
return pipe;
}
catch
{
await pipe.DisposeAsync()
.ConfigureAwait(false);
throw;
}
}
});
request.Version = HttpVersion.Version11;
request.VersionPolicy = HttpVersionPolicy.RequestVersionExact;
};
await this.ContainerListAsync(all: true).Dump();
}
ConfigureTransportOnce configures the transport only if the first request has not started yet. Subsequent calls have no effect, allowing PrepareRequestFunction to run for every request without recreating the transport.
Credits
Tools
Libraries
- NJsonSchema
- NSwag
- Newtonsoft.Json - required by NSwag, included to bump version
Development
- OpenApiLINQPadDriver.csproj contains special
Debug_Publish_To_LINQPad_Folderdebug build configuration, if it is chosen, code will be build only targetingnet10.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
Roadmap
- 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)
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0-windows7.0 is compatible. net9.0-windows was computed. net10.0-windows was computed. net10.0-windows7.0 is compatible. |
-
net10.0-windows7.0
- LINQPad.Reference (>= 1.3.1)
- Microsoft.CodeAnalysis.CSharp (>= 5.6.0)
- NJsonSchema (>= 11.6.1)
- NJsonSchema.CodeGeneration (>= 11.6.1)
- NJsonSchema.CodeGeneration.CSharp (>= 11.6.1)
- NSwag.CodeGeneration (>= 14.7.1)
- NSwag.CodeGeneration.CSharp (>= 14.7.1)
- NSwag.Core (>= 14.7.1)
- NSwag.Core.Yaml (>= 14.7.1)
- Prism.Core (>= 9.0.537)
-
net8.0-windows7.0
- LINQPad.Reference (>= 1.3.1)
- Microsoft.CodeAnalysis.CSharp (>= 5.6.0)
- NJsonSchema (>= 11.6.1)
- NJsonSchema.CodeGeneration (>= 11.6.1)
- NJsonSchema.CodeGeneration.CSharp (>= 11.6.1)
- NSwag.CodeGeneration (>= 14.7.1)
- NSwag.CodeGeneration.CSharp (>= 14.7.1)
- NSwag.Core (>= 14.7.1)
- NSwag.Core.Yaml (>= 14.7.1)
- Prism.Core (>= 9.0.537)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 0.0.10-alpha | 60 | 7/15/2026 |
| 0.0.9-alpha | 77 | 4/23/2026 |
| 0.0.8-alpha | 295 | 6/1/2024 |
| 0.0.7-alpha | 198 | 2/5/2024 |
| 0.0.6-alpha | 178 | 1/14/2024 |
| 0.0.5-alpha | 177 | 12/27/2023 |
| 0.0.4-alpha | 200 | 9/10/2023 |
| 0.0.3-alpha | 187 | 9/10/2023 |
| 0.0.2-alpha | 184 | 9/8/2023 |
| 0.0.1-alpha | 182 | 9/8/2023 |