OpenAPI request guide

Test a generated .NET HttpClient.

Generate a typed client from an OpenAPI contract. Give it a test handler and watch the same method handle a payment and a 404.

Call the generated clientProgram.cs
var payment = await client.GetPaymentAsync("pay_42");
Handler response
Recorded HTTP testGET /payments/pay_42200 OK
{ "id": "pay_42", "amount": 12.5 }
Decoded as Payment

Run it in your repository

The companion contains the OpenAPI input, generated sources, assertions and reproduction script.

  1. Define the contract

    GET /payments/{id} returns a Payment with an id and amount.

  2. Generate the client

    Pin the tool and compile the generated DefaultClient with .NET SDK 10.0.105.

  3. Exercise both paths

    The handler checks the method and URL. The second call must throw HttpRequestException.

From the extracted companion · C# tool 1.0.2
bash verify.sh /tmp/metaengine-request-check
Download ZIP
Read the complete walkthrough

You have a Swagger/OpenAPI document and want to call one endpoint from a .NET application. The useful checkpoint is a compiled client making the expected request and returning a typed result. This example generates that client, injects an HTTP test handler, and checks the success and failure paths without needing a running API.

Use the complete companion example. It contains the OpenAPI input, unchanged generated files, a project file, executable assertions, and an isolated reproduction script. The example uses OpenAPI 3.0.3, C# records and System.Text.Json. A Swagger UI's HTML page is not the input: obtain its OpenAPI JSON or YAML document first. This walkthrough does not establish Swagger 2.0 compatibility.

Start with the request contract

The fixture defines an untagged getPayment operation on GET /payments/{id}. Its required path parameter is a string; a 200 response contains a Payment with required id and numeric amount fields. Here is the response shape from api.json:

{
  "Payment": {
    "type": "object",
    "required": ["id", "amount"],
    "properties": {
      "id": { "type": "string" },
      "amount": { "type": "number", "format": "double" }
    }
  }
}

The full document supplies the openapi, info, paths, response content and component references; this fragment alone is not a generator input. The endpoint and payload are synthetic so you can run them without credentials or private schema data.

Pin and generate

The verified environment is .NET SDK 10.0.105 on macOS arm64. The companion global.json requires that SDK. The companion pins version 1.0.2 of the .NET tool to reproduce the code shown here.

From the companion directory, the generator commands are:

dotnet tool install --tool-path ./tools MetaEngine.CSharp.OpenApi.HttpClient.Tool --version 1.0.2
DOTNET_ROLL_FORWARD=Major ./tools/metaengine-openapi-csharp-httpclient api.json ./Generated Guide.Payments

The packaged tool targets .NET 8. The explicit roll-forward setting allows this pinned tool to run with the verified .NET 10 runtime; it does not alter the generated client. The full reproduction script keeps tools, NuGet downloads and build artifacts in the scratch directory you provide.

With this untagged fixture, the generated service is Guide.Payments.Client.DefaultClient. Its actual constructor accepts HttpClient, an optional base URL and optional JsonSerializerOptions; the generated method is:

Task<Payment> GetPaymentAsync(string id, CancellationToken cancellationToken = default)

Operation tags can affect grouping and names. Check your generated files rather than substituting a service name from a different specification.

Inject the transport and make a request

The application call in Program.cs is small:

using var successHandler = new PaymentHandler(HttpStatusCode.OK);
using var http = new HttpClient(successHandler);
var client = new DefaultClient(http, "https://api.example.test");
var payment = await client.GetPaymentAsync("pay_42");

PaymentHandler is an ordinary HttpMessageHandler. It checks the method and complete URL, then returns {"id":"pay_42","amount":12.5} as JSON. Assertions check the typed values and exactly one request. A second client receives a 404 and must throw HttpRequestException with StatusCode == NotFound. A wrong path, unexpected payload or missing exception fails the process.

For a real server, inject an application-managed HttpClient instead of the test handler and configure the real base URL. Keep credentials out of the OpenAPI file and source tree; this sample does not configure authentication, retries or middleware.

Run the complete check

Unzip the companion, open the extracted folder in a terminal, and choose a new absolute scratch path.

bash verify.sh /tmp/metaengine-request-check

The script refuses an existing directory, installs the exact tool, generates into scratch, compares every generated file with the checked-in output, then compiles and runs the assertions. The observed successful output includes:

PASS GET /payments/pay_42 -> Payment(pay_42, 12.5)
PASS 404 -> HttpRequestException

The build and request test are local contract evidence. They do not test your API's availability, authentication or production response compatibility. The generated 1.0.2 method interpolates this path parameter directly; the fixture intentionally uses path-safe identifiers. Verify escaping behavior before passing identifiers containing /, ? or #. Required generated model properties also do not replace business validation of values.

To try your own input, open the OpenAPI to C# converter. For the CLI options and installation choices, use the matching C# HttpClient package page. Keep your first request assertion when you replace this fixture with your own contract.