Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/fsharp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ jobs:
- name: Build the F# solution
run: dotnet build wolverine_fsharp.slnx -c "$config" --nologo

- name: Compile-gate + fsharp-coverage
# The gate test regenerates Generated.fs, recompiles the F# fixture (retrying once on the
- name: Compile-gate + fsharp-coverage (Core + Http surfaces)
# Each gate test regenerates its Generated.fs, recompiles the F# fixture (retrying once on the
# transient FS0193 internal-compiler crash), and runs the fsharp-coverage report.
run: dotnet test src/Testing/Wolverine.Core.FSharpTests/Wolverine.Core.FSharpTests.csproj -c "$config" --nologo
run: dotnet test wolverine_fsharp.slnx -c "$config" --nologo
13 changes: 13 additions & 0 deletions src/Http/Wolverine.Http/Resources/StringResourceWriterPolicy.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using JasperFx.CodeGeneration;
using JasperFx.CodeGeneration.Frames;
using JasperFx.CodeGeneration.Model;
using JasperFx.Core.Reflection;

namespace Wolverine.Http.Resources;

Expand Down Expand Up @@ -36,5 +37,17 @@ public override void GenerateCode(GeneratedMethod method, ISourceWriter writer)

Next?.GenerateCode(method, writer);
}

public override void GenerateFSharpCode(GeneratedMethod method, ISourceWriter writer)
{
// HttpHandler.WriteString is static, so it resolves cleanly in F# (no `this`).
var call =
$"{typeof(HttpHandler).FSharpName()}.{nameof(HttpHandler.WriteString)}(httpContext, {_result.Usage})";

// Inside a `task { }` body await it; otherwise it IS the trailing Task expression.
writer.Write(method.AsyncMode == AsyncMode.AsyncTask ? $"do! {call}" : call);

Next?.GenerateFSharpCode(method, writer);
}
}
}
28 changes: 28 additions & 0 deletions src/Testing/Wolverine.Http.FSharpContracts/Endpoints.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
namespace Wolverine.Http.FSharpContracts;

/// <summary>The JSON body bound by <see cref="ThingEndpoints.Create" />.</summary>
public record CreateThing(string Name);

/// <summary>The JSON result returned by <see cref="ThingEndpoints.Create" />.</summary>
public record ThingCreated(string Name);

/// <summary>
/// The "smallest viable" Wolverine.Http endpoints for the F# code-generation audit
/// (issue GH-2969, Phase C): a GET returning a string and a POST binding a JSON body and
/// returning a JSON result. The driver renders each endpoint's real <c>HttpChain</c> to F#, and
/// the fixture compiles the generated <c>HttpHandler</c> adapters against these public types.
/// </summary>
public class ThingEndpoints
{
[WolverineGet("/fsharp/hello")]
public string Hello()
{
return "hello from F#";
}

[WolverinePost("/fsharp/things")]
public ThingCreated Create(CreateThing command)
{
return new ThingCreated(command.Name);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

<!--
Shared C# HTTP endpoint contracts for the Wolverine.Http F# code-generation surface
(issue GH-2969, Phase C). The C# driver (Wolverine.Http.FSharpTests) builds HttpChains from
these endpoints and the F# fixture (Wolverine.Http.FSharpFixture) compiles the generated
adapters against them.
-->

<PropertyGroup>
<TargetFrameworks>net9.0</TargetFrameworks>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\Http\Wolverine.Http\Wolverine.Http.csproj" />
</ItemGroup>

</Project>
25 changes: 25 additions & 0 deletions src/Testing/Wolverine.Http.FSharpFixture/Generated.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// <auto-generated/>

namespace Internal.Generated.WolverineHandlers

open Microsoft.AspNetCore.Http
open Microsoft.AspNetCore.Routing
open System
open System.Linq
open System.Threading.Tasks
open Wolverine.Http

type GET_fsharp_hello(wolverineHttpOptions: Wolverine.Http.WolverineHttpOptions) =
inherit Wolverine.Http.HttpHandler(wolverineHttpOptions)
let _wolverineHttpOptions = wolverineHttpOptions

override _.Handle(httpContext: Microsoft.AspNetCore.Http.HttpContext) : System.Threading.Tasks.Task =
task {
let thingEndpoints = Wolverine.Http.FSharpContracts.ThingEndpoints()

// The actual HTTP request handler execution
let result_of_Hello = thingEndpoints.Hello()

do! Wolverine.Http.HttpHandler.WriteString(httpContext, result_of_Hello)
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<Project Sdk="Microsoft.NET.Sdk">

<!--
Checked-in F# fixture for the Wolverine.Http F# code-generation surface (issue GH-2969,
Phase C). Generated.fs (the emitted HttpHandler adapters) is produced by the driver
(Wolverine.Http.FSharpTests) and committed; the compile gate regenerates it and `dotnet build`s
this project to prove the emitted F# compiles. Mirrors the Core surface fixture.
-->

<PropertyGroup>
<TargetFrameworks>net9.0</TargetFrameworks>
<IsPackable>false</IsPackable>

<!-- Relax the C#-oriented props inherited from Directory.Build.props for F#. -->
<LangVersion></LangVersion>
<ImplicitUsings>false</ImplicitUsings>
<Nullable>disable</Nullable>
<GenerateDocumentationFile>false</GenerateDocumentationFile>
<TreatWarningsAsErrors>false</TreatWarningsAsErrors>

<!-- Pin FSharp.Core to the centrally-managed version; see Wolverine.Core.FSharpFixture. -->
<DisableImplicitFSharpCoreReference>true</DisableImplicitFSharpCoreReference>
</PropertyGroup>

<ItemGroup>
<Compile Include="Generated.fs" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Wolverine.Http.FSharpContracts\Wolverine.Http.FSharpContracts.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="FSharp.Core" />
</ItemGroup>

</Project>
63 changes: 63 additions & 0 deletions src/Testing/Wolverine.Http.FSharpTests/HttpFSharpCodegenSample.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using System.Runtime.CompilerServices;
using System.Text.Json;
using JasperFx;
using JasperFx.CodeGeneration;
using JasperFx.CodeGeneration.Model;
using JasperFx.CodeGeneration.Services;
using Microsoft.Extensions.DependencyInjection;
using Wolverine.Http.FSharpContracts;

namespace Wolverine.Http.FSharpTests;

/// <summary>
/// Renders the Phase C Wolverine.Http endpoints (issue GH-2969) as F#. Builds each endpoint's
/// real <see cref="HttpChain" /> via <see cref="HttpChain.ChainFor{T}" /> (no web host needed),
/// assembles them into one <see cref="GeneratedAssembly" />, and emits the generated
/// <c>HttpHandler</c> adapters as F# via <see cref="GeneratedAssembly.GenerateFSharpCode" /> —
/// mirroring the C# preview path but swapping <c>GenerateCode</c> for <c>GenerateFSharpCode</c>.
/// </summary>
public static class HttpFSharpCodegenSample
{
public static string GenerateCode()
{
// No-host container setup mirroring Wolverine.Http.Tests.initializing_endpoints_from_method_call.
var registry = new ServiceCollection();
registry.AddSingleton<JsonSerializerOptions>();

var container = new ServiceContainer(registry, registry.BuildServiceProvider());
var httpGraph = new HttpGraph(new WolverineOptions { ApplicationAssembly = typeof(ThingEndpoints).Assembly }, container);

// Phase C increment 1 renders the static-response GET endpoint. The JSON POST endpoint
// (ReadJsonBody / WriteJsonFrame) calls *instance* HttpHandler methods unqualified, which F#
// cannot resolve from a `member _.Handle` body — blocked on the JasperFx F# self-identifier
// gap (tracked upstream; same gap as RecordMessageCausationFrame). Add it back once JasperFx
// emits a named self for generated members.
var chains = new[]
{
HttpChain.ChainFor<ThingEndpoints>(x => x.Hello(), httpGraph)
};

var generatedAssembly = httpGraph.StartAssembly(httpGraph.Rules);
foreach (var chain in chains)
{
((ICodeFile)chain).AssembleTypes(generatedAssembly);
}

var serviceVariableSource = new ServiceCollectionServerVariableSource(container);
return generatedAssembly.GenerateFSharpCode(serviceVariableSource);
}

public static string DefaultGeneratedFilePath([CallerFilePath] string thisFile = "")
{
var testProjectDir = Path.GetDirectoryName(thisFile)!;
var srcTestingDir = Path.GetDirectoryName(testProjectDir)!;
return Path.Combine(srcTestingDir, "Wolverine.Http.FSharpFixture", "Generated.fs");
}

public static string FixtureProjectPath([CallerFilePath] string thisFile = "")
{
var testProjectDir = Path.GetDirectoryName(thisFile)!;
var srcTestingDir = Path.GetDirectoryName(testProjectDir)!;
return Path.Combine(srcTestingDir, "Wolverine.Http.FSharpFixture", "Wolverine.Http.FSharpFixture.fsproj");
}
}
74 changes: 74 additions & 0 deletions src/Testing/Wolverine.Http.FSharpTests/HttpFSharpCompileGate.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using System.Diagnostics;
using Shouldly;
using Wolverine.Diagnostics;
using Xunit;
using Xunit.Abstractions;

namespace Wolverine.Http.FSharpTests;

/// <summary>
/// The Phase C acceptance gate (issue GH-2969): regenerates the fixture's <c>Generated.fs</c> from
/// real Wolverine.Http endpoint chains, then shells <c>dotnet build</c> on the checked-in F# fixture
/// and asserts a clean build. Mirrors the Core surface's compile gate.
/// </summary>
public class HttpFSharpCompileGate
{
private readonly ITestOutputHelper _output;

public HttpFSharpCompileGate(ITestOutputHelper output)
{
_output = output;
}

[Fact]
public void generated_fsharp_compiles_via_dotnet_build()
{
var code = HttpFSharpCodegenSample.GenerateCode();
var generatedFile = HttpFSharpCodegenSample.DefaultGeneratedFilePath();
File.WriteAllText(generatedFile, code);

File.Exists(generatedFile).ShouldBeTrue();
_output.WriteLine(code);

var fixtureProject = HttpFSharpCodegenSample.FixtureProjectPath();
var (exitCode, output) = RunDotnet($"build \"{fixtureProject}\" -c Debug --nologo");

// Retry once on the transient FS0193 internal-compiler crash (see the Core surface gate).
if (exitCode != 0 && (output.Contains("FS0193") || output.Contains("internal error")))
{
(exitCode, output) = RunDotnet($"build \"{fixtureProject}\" -c Debug --nologo");
}

_output.WriteLine(output);
exitCode.ShouldBe(0);
}

[Fact]
public async Task fsharp_coverage_includes_http_frames()
{
// Run from this assembly so the loaded Wolverine.* set includes Wolverine.Http; surfaces the
// HTTP frame coverage tally (implemented / skipped / remaining) in CI test output.
var command = new WolverineDiagnosticsCommand();
var result = await command.Execute(new WolverineDiagnosticsInput { Action = "fsharp-coverage" });
result.ShouldBeTrue();
}

private static (int ExitCode, string Output) RunDotnet(string arguments)
{
var info = new ProcessStartInfo("dotnet", arguments)
{
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false
};
info.Environment["DOTNET_CLI_USE_MSBUILD_SERVER"] = "0";
info.Environment["MSBUILDDISABLENODEREUSE"] = "1";

using var process = Process.Start(info)!;
var stdout = process.StandardOutput.ReadToEndAsync();
var stderr = process.StandardError.ReadToEndAsync();
process.WaitForExit();

return (process.ExitCode, stdout.GetAwaiter().GetResult() + stderr.GetAwaiter().GetResult());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<Project Sdk="Microsoft.NET.Sdk">

<!--
C# driver + compile-gate for the Wolverine.Http F# code-generation surface (issue GH-2969,
Phase C). Builds real HttpChains from the contract endpoints, renders them as F# via
GenerateFSharpCode(), writes Generated.fs into Wolverine.Http.FSharpFixture, and shells
`dotnet build` on that fixture to prove the emitted F# compiles.
-->

<PropertyGroup>
<TargetFrameworks>net9.0</TargetFrameworks>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="Shouldly" />
<PackageReference Include="xunit" />
<PackageReference Include="xunit.runner.visualstudio">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="GitHubActionsTestLogger" PrivateAssets="All" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Wolverine.Http.FSharpContracts\Wolverine.Http.FSharpContracts.csproj" />
<ProjectReference Include="..\..\Http\Wolverine.Http\Wolverine.Http.csproj" />
</ItemGroup>

</Project>
4 changes: 4 additions & 0 deletions wolverine_fsharp.slnx
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@
<Project Path="src/Testing/Wolverine.Core.FSharpContracts/Wolverine.Core.FSharpContracts.csproj" />
<Project Path="src/Testing/Wolverine.Core.FSharpFixture/Wolverine.Core.FSharpFixture.fsproj" />
<Project Path="src/Testing/Wolverine.Core.FSharpTests/Wolverine.Core.FSharpTests.csproj" />
<Project Path="src/Testing/Wolverine.Http.FSharpContracts/Wolverine.Http.FSharpContracts.csproj" />
<Project Path="src/Testing/Wolverine.Http.FSharpFixture/Wolverine.Http.FSharpFixture.fsproj" />
<Project Path="src/Testing/Wolverine.Http.FSharpTests/Wolverine.Http.FSharpTests.csproj" />
</Folder>
<Folder Name="/Wolverine/">
<Project Path="src/Wolverine/Wolverine.csproj" />
<Project Path="src/Http/Wolverine.Http/Wolverine.Http.csproj" />
</Folder>
</Solution>