WoofWare.Whippet.Plugin.Swagger 0.1.5

dotnet add package WoofWare.Whippet.Plugin.Swagger --version 0.1.5                
NuGet\Install-Package WoofWare.Whippet.Plugin.Swagger -Version 0.1.5                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="WoofWare.Whippet.Plugin.Swagger" Version="0.1.5">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add WoofWare.Whippet.Plugin.Swagger --version 0.1.5                
#r "nuget: WoofWare.Whippet.Plugin.Swagger, 0.1.5"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install WoofWare.Whippet.Plugin.Swagger as a Cake Addin
#addin nuget:?package=WoofWare.Whippet.Plugin.Swagger&version=0.1.5

// Install WoofWare.Whippet.Plugin.Swagger as a Cake Tool
#tool nuget:?package=WoofWare.Whippet.Plugin.Swagger&version=0.1.5                

WoofWare.Whippet.Plugin.Swagger

This is a Whippet plugin for defining strongly-typed HTTP clients according to Swagger schemas.

It is a copy of the corresponding Myriad HttpClient plugin in WoofWare.Myriad, taken from commit d59ebdfccb87a06579fb99008a15f58ea8be394e.

Usage

Save a Swagger schema as my-swagger-schema.json.

In your fsproj:

<Project>
    <ItemGroup>
        <None Include="my-swagger-schema.json" />
        <Compile Include="Client.fs">
            <WhippetFile>my-swagger-schema.json</WhippetFile>
            <WhippetParamClassName>GiteaClient</WhippetParamClassName>
        </Compile>
    </ItemGroup>
    <ItemGroup>
        <PackageReference Include="WoofWare.Whippet.Plugin.HttpClient.Attributes" Version="" />
        <PackageReference Include="WoofWare.Whippet.Plugin.Json.Attributes" Version="" />

        
        <PackageReference Include="WoofWare.Whippet.Plugin.Swagger" WhippetPlugin="true" Version="" />
        <PackageReference Include="WoofWare.Whippet" Version="" PrivateAssets="all" />
    </ItemGroup>
</Project>

(Note that you must supply <WhippetParamClassName>SomeClassName</WhippetParamClassName> to tell the generator what to name the type it produces.)

The generator produces code like this. Notice that it adds JsonParse and JsonSerialize attributes (i.e. it assumes you have access to WoofWare.Whippet.Plugin.Json.Attributes), and also the HttpClient attribute (i.e. it assumes you have access to WoofWare.Whippet.Plugin.HttpClient.Attributes).

/// A type which was defined in the Swagger spec
[<JsonParse true ; JsonSerialize true>]
type SwaggerType1 =
    {
        [<System.Text.Json.Serialization.JsonExtensionData>]
        AdditionalProperties : System.Collections.Generic.Dictionary<string, System.Text.Json.Nodes.JsonNode>
        Message : string
    }

/// Documentation from the Swagger spec
[<HttpClient false ; RestEase.BasePath "/api/v1">]
type IGitea =
    /// Returns the Person actor for a user
    [<RestEase.Get "/activitypub/user/{username}">]
    abstract ActivitypubPerson :
        [<RestEase.Path "username">] username : string * ?ct : System.Threading.CancellationToken ->
            ActivityPub System.Threading.Tasks.Task

That means if you choose to, you can chain other Whippet generators off this one, to generate JSON serde methods and HTTP REST clients:

<Project>
    <ItemGroup>
        <None Include="my-swagger-schema.json" />
        <Compile Include="Client.fs">
            <WhippetFile>my-swagger-schema.json</WhippetFile>
            <WhippetParamClassName>GiteaClient</WhippetParamClassName>
        </Compile>
        <Compile Include="GeneratedClientMockAndJson.fs">
            <WhippetFile>Client.fs</WhippetFile>
            <WhippetParamIGiteaClient>InterfaceMock</WhippetParamIGiteaClient>
        </Compile>
        <Compile Include="GeneratedClient.fs">
            <WhippetFile>Client.fs</WhippetFile>
            <WhippetParamIGiteaClient>HttpClient</WhippetParamIGiteaClient>
            
            <WhippetSuppressPlugin>JsonParseGenerator,JsonSourceGenerator</WhippetSuppressPlugin>
        </Compile>
    </ItemGroup>
    <ItemGroup>
        <PackageReference Include="WoofWare.Whippet.Plugin.HttpClient.Attributes" Version="" />
        <PackageReference Include="WoofWare.Whippet.Plugin.Json.Attributes" Version="" />

        <PackageReference Include="WoofWare.Whippet.Plugin.HttpClient" WhippetPlugin="true" Version="" />
        <PackageReference Include="WoofWare.Whippet.Plugin.Json" WhippetPlugin="true" Version="" />
        <PackageReference Include="WoofWare.Whippet.Plugin.Swagger" WhippetPlugin="true" Version="" />
        <PackageReference Include="WoofWare.Whippet.Plugin.InterfaceMock" WhippetPlugin="true" Version="" />
        <PackageReference Include="WoofWare.Whippet" Version="" PrivateAssets="all" />
    </ItemGroup>
</Project>

The <WhippetParamClassName /> key tells us what to name the resulting interface (it gets an I prepended for you).

What's the point?

SwaggerProvider is absolutely magical, but it's kind of witchcraft. I fear no man, but that thing… it scares me.

Also, builds using SwaggerProvider appear to be inherently nondeterministic, even if the data source doesn't change.

Limitations

Swagger API specs appear to be pretty cowboy in the wild. I try to cope with invalid schemas I have seen, but I can't guarantee I do so correctly. Definitely do perform integration tests and let me know of weird specs you encounter, and bits of the (very extensive) Swagger spec I have omitted!

I have only attempted to deal with Swagger v2.0 so far.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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.1.5 93 10/9/2024
0.1.4 87 10/8/2024
0.1.3 81 10/8/2024
0.1.2 82 10/8/2024
0.1.1 80 10/8/2024