Skip to content

Commit ad0fe63

Browse files
Add comprehensive tests for registerRouteHandlersByTag
Co-authored-by: jasonblanchard <1238532+jasonblanchard@users.noreply.github.com>
1 parent 151ab06 commit ad0fe63

File tree

2 files changed

+308
-8
lines changed

2 files changed

+308
-8
lines changed

.github/copilot-instructions.md

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ OpenAPI Spec → [CLI] → Generated Server Interface → [Your API Implementati
1717
```
1818

1919
Key insight: Your handler return values mirror OpenAPI response structure:
20+
2021
```typescript
2122
return {
2223
content: {
@@ -32,6 +33,7 @@ return {
3233
### Generated Code Pattern
3334

3435
CLI generates per-operation:
36+
3537
- `<OperationId>Args<Req, Res>` interface with typed parameters, requestBody, and framework objects
3638
- `<OperationId>Result` type union of all response variants
3739
- `<OperationId>Unimplemented()` stub throwing `NotImplementedError`
@@ -49,19 +51,21 @@ CLI generates per-operation:
4951
## Development Workflow
5052

5153
### Initial Setup (Required Order)
54+
5255
```bash
5356
node --version # Must be >= 23.6.0
5457
npm ci # 30 seconds - NEVER CANCEL
5558
npm run build:packages # 5 seconds - builds .cjs files
5659
npm run gen:examples # 5 seconds - validates CLI works
5760
npm run format:check # 2 seconds
58-
npm run typecheck # 3 seconds
61+
npm run typecheck # 3 seconds
5962
npm test # 1 second
6063
```
6164

6265
**NEVER** run `npm ci` with short timeout. Set to 60+ minutes or it will fail.
6366

6467
### After Code Changes (Validation Sequence)
68+
6569
```bash
6670
npm run format # Auto-fix formatting (required before commit)
6771
npm run build:packages # Re-build if CLI or adapters changed
@@ -71,6 +75,7 @@ npm test # All tests must pass
7175
```
7276

7377
### Testing Generated Code Locally
78+
7479
```bash
7580
cd examples/docs
7681
npm run start # Runs: node --watch cmd/index.ts (port 8080)
@@ -83,6 +88,7 @@ curl -X POST http://localhost:8080/api/v3/speak/123 \
8388
```
8489

8590
### Regenerating After OpenAPI Changes
91+
8692
```bash
8793
# In example directory or your project:
8894
openapi-typescript ./openapi.yaml --output ./gen/schema.d.ts
@@ -113,9 +119,9 @@ openapi-typescript-server ./openapi.yaml --types ./schema.d.ts --output ./gen/se
113119
```typescript
114120
registerRoutes(routes, app, {
115121
serializers: {
116-
"application/xml": (content) => json2xml(JSON.stringify(content))
117-
}
118-
})
122+
"application/xml": (content) => json2xml(JSON.stringify(content)),
123+
},
124+
});
119125
```
120126

121127
### Error Handling Patterns
@@ -136,6 +142,7 @@ See `examples/docs/api.ts` for error pattern with `"default"` response variant.
136142
- Run: `npm test` at root (runs across all workspaces) or per-package
137143

138144
Example pattern (`examples/docs/app.test.ts`):
145+
139146
```typescript
140147
import { describe, it } from "node:test";
141148
import assert from "node:assert";
@@ -163,12 +170,12 @@ packages/
163170
index.ts # Entry point, arg parsing
164171
generate.ts # Core codegen logic (creates Server interface)
165172
bin/cli/index.cjs # Built binary (created by build:packages)
166-
173+
167174
openapi-typescript-server-runtime/ # Shared types (Route, errors)
168175
src/
169176
route.ts # Route, HandlerInput, HandlerResponse types
170177
errors.ts # NotImplementedError
171-
178+
172179
openapi-typescript-server-express/ # Express adapter
173180
src/
174181
index.ts # registerRoutes() function
@@ -189,19 +196,22 @@ examples/
189196
## Key Workflows for Common Tasks
190197

191198
### Adding Support for New Response Pattern
199+
192200
1. Update `packages/openapi-typescript-server/src/cli/generate.ts` response variant logic
193201
2. Update `packages/openapi-typescript-server-express/src/index.ts` to handle new variant
194202
3. Add test case to `packages/openapi-typescript-server-express/src/index.test.ts`
195203
4. Run `npm run build:packages && npm run gen:examples && npm test`
196204

197205
### Adding New Framework Adapter
206+
198207
1. Create `packages/openapi-typescript-server-<framework>/`
199-
2. Implement `registerRoutes(routes: Route[], app: FrameworkApp)`
208+
2. Implement `registerRoutes(routes: Route[], app: FrameworkApp)`
200209
3. Handle parameter extraction, content negotiation, serialization per framework conventions
201210
4. Reference `openapi-typescript-server-express` as template
202211
5. Add integration tests using framework's test utilities
203212

204213
### Debugging Type Errors After Regeneration
214+
205215
1. Check OpenAPI spec changed: Compare old/new `openapi.yaml`
206216
2. Check generated types: `git diff gen/schema.d.ts gen/server.ts`
207217
3. Restart TS Server: VS Code may cache old types
@@ -211,6 +221,7 @@ examples/
211221
## CI/CD Pipeline (`.github/workflows/test.yaml`)
212222

213223
Runs on every commit:
224+
214225
```yaml
215226
npm ci
216227
npm run format:check # Fails if code not formatted
@@ -235,7 +246,7 @@ npm test
235246
## Common Mistakes
236247

237248
- ❌ Forgetting to run `npm run build:packages` after CLI changes (tests import stale code)
238-
- ❌ Not regenerating examples after CLI changes (CI will fail on diff check)
249+
- ❌ Not regenerating examples after CLI changes (CI will fail on diff check)
239250
- ❌ Using structural typing instead of explicit `Server<Req, Res>` type (loses inference)
240251
- ❌ Mounting routes without base path (OpenAPI `servers.url` ignored by adapter)
241252
- ❌ Returning `"default"` response without `status` field (throws error at runtime)

0 commit comments

Comments
 (0)