OpenAPI request guide

Drive an Angular request with a signal.

Give a generated httpResource a reactive payment ID. Follow the request and the typed value as the selection changes.

Bind the resourcetest.ts
const id = signal<string | undefined>(undefined);
const resource = TestBed.runInInjectionContext(() =>
  TestBed.inject(DefaultService).getPaymentResource(id));
Selected ID
Recorded HTTP testNo HTTP requestWaiting for an ID
undefined
The request starts when the signal has a value.

Run it in your repository

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

  1. Define the contract

    Use one untagged OpenAPI operation to look up a payment by ID.

  2. Generate the resource

    Pin generator 1.4.1 and create the signal-aware method in an Angular injection context.

  3. Change the signal

    The test observes both requests, flushes each payment and verifies there are no outstanding requests.

From the extracted companion · Angular 20.1.0
bash verify.sh /tmp/metaengine-angular-check
Download ZIP
Read the complete walkthrough

A details screen often starts without a selected record, then loads a record when its ID becomes available and reloads when that ID changes. An OpenAPI-generated Angular client should fit that flow without a subscription for each change. This example generates a signal-aware method and tests its requests deterministically with Angular's HTTP testing backend.

The complete companion example contains a real synthetic OpenAPI document, unchanged generator output, pinned dependencies and a Node test environment. It needs no server, browser or API credentials.

Define the small API before the UI

api.json declares OpenAPI 3.0.3 with an untagged getPayment operation on GET /payments/{id}. The required id path parameter is a string. A 200 JSON response references Payment, which requires string id and numeric amount.

The operation ID gives the generated method its name. The fixture is deliberately untagged, so its service is DefaultService; grouping changes may produce a different class for your contract. The input is a JSON specification, not a Swagger UI HTML URL.

Generate the resource method

The verified toolchain is Node 24.18.0, npm 11.16.0, .NET SDK 10.0.105, Angular 20.1.0 and TypeScript 5.8.3. The companion lockfile fixes test dependencies. The companion pins version 1.4.1 of the npm generator.

DOTNET_ROLL_FORWARD=Major npx --yes @metaengine/openapi-angular@1.4.1 api.json ./src/api --http-resource --inject-function --provided-in root

Run that command from the companion directory. The npm package invokes its bundled .NET runner; the explicit roll-forward setting permits the verified .NET 10 environment. Angular's httpResource API arrived after the earliest Angular versions supported by the general package, so a generic minimum-Angular statement is insufficient for this mode. This example proves the Angular 20.1.0 combination only.

The actual generated signature is:

getPaymentResource(id: Signal<string | undefined>): HttpResourceRef<Payment | undefined>

Its request callback reads id(), returns undefined when no ID exists, and otherwise builds /payments/${idValue}. The generated service also retains an Observable-based getPayment(id: string) method. You can inspect both in default.service.ts.

Create the resource in an injection context

Configure the generated base URL token and Angular HTTP providers at the application boundary:

providers: [
  provideHttpClient(),
  { provide: BASE_URL, useValue: 'https://api.example.test' },
]

A component can inject DefaultService, keep id = signal<string | undefined>(undefined), and create payment = service.getPaymentResource(id) in its field initializer. Setting id.set('pay_42') supplies the first request input; setting another ID supplies the next. Read payment.value(), payment.isLoading() and payment.error() in the UI according to your application's states. Use a base URL without a trailing slash for this generated URL builder.

Resources need an Angular injection context and lifecycle owner. The companion test uses TestBed.runInInjectionContext and explicitly destroys its resource. Angular's httpResource guide describes reactive request inputs and resource behavior.

Test the request and the resulting signal

The test in test.ts provides provideHttpClient() followed by provideHttpClientTesting(), then injects HttpTestingController. That order lets the testing backend replace the transport. It first checks that an undefined ID produces no request. For each of two IDs, it drives Angular's scheduler, expects one GET with the exact URL and flushes the payload:

id.set(nextId);
TestBed.tick();
const request = http.expectOne({
  method: 'GET', url: `https://api.example.test/payments/${nextId}`,
});
request.flush({ id: nextId, amount: 12.5 });
await Promise.resolve();
TestBed.tick();
assert.deepEqual(resource.value(), { id: nextId, amount: 12.5 });

The final http.verify() rejects outstanding requests. The test sets up pinned jsdom because Angular's browser testing platform needs a document even though no browser is launched. There are no timing sleeps or real network calls in these assertions. See Angular's HTTP testing documentation for request matching and flushing.

Reproduce generation, compilation and assertions

Unzip the companion and open the extracted folder in a terminal.

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

Use a new absolute directory. The script regenerates and compares the exact output, installs lockfile dependencies with install scripts disabled, type-checks the project and runs the Node test. Its observed assertion output is:

PASS undefined ID -> no request
PASS reactive ID pay_42 -> mocked GET and resource value
PASS reactive ID pay_43 -> mocked GET and resource value
PASS no outstanding HTTP requests

This tests reactive IDs, request URLs and response values. It does not test a rendered component, SSR, live authentication, error UI, cancellation during an in-flight request or runtime payload validation. TypeScript types alone do not validate server JSON. The fixture uses path-safe IDs; check the generated interpolation before accepting IDs with reserved URL characters.

Try your own contract in the OpenAPI to Angular converter, and use the matching Angular package page for installation and options. Keep the mocked request test when your API and generated service change together.