feat(openapi): document constrained route params

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
chrnx committed 2026-08-02 20:47:18 -05:00
1 parent 9a61ba8646
commit a2acec69bf
4 files changed
+56 -13

No files matched your search

+25 -2
View File
@@ -1,7 +1,7 @@
import 'reflect-metadata';
import { describe, it, expect } from 'vitest';
import { createApp } from '../src/app';
import { Route, Get, Post, Ws, Module } from '../src/metadata';
import { Route, Get, Head, Options, Post, Ws, Module } from '../src/metadata';
import { param, query, headers, body } from '../src/params';
const numSchema = {
@@ -14,7 +14,13 @@ const numSchema = {
@Route('/users')
class Users {
@Get('/:id') get(@param('id') _id: string, @query('expand') _e: string, @headers('x-trace') _t: string) {
@Get('/:id(\\d+)') get(@param('id') _id: string, @query('expand') _e: string, @headers('x-trace') _t: string) {
return {};
}
@Head('/:id(\\d+)') head() {
return {};
}
@Options('/:id(\\d+)') options() {
return {};
}
@Post('/') create(@body() _b: unknown) {
@@ -52,6 +58,23 @@ describe('app.openapi()', () => {
expect(byName).toEqual({ id: 'path', expand: 'query', 'x-trace': 'header' });
});
it('projects a constrained param into schema.pattern', () => {
const op = doc.paths['/api/users/{id}'].get as any;
expect(op.parameters).toContainEqual({
name: 'id',
in: 'path',
required: true,
schema: { type: 'string', pattern: '\\d+' },
});
});
it('includes explicit HEAD and OPTIONS without inventing automatic operations', () => {
expect(doc.paths['/api/users/{id}'].head).toBeDefined();
expect(doc.paths['/api/users/{id}'].options).toBeDefined();
expect(doc.paths['/api/users/search'].head).toBeUndefined();
expect(doc.paths['/api/users/search'].options).toBeUndefined();
});
it('marks routes that take a body with requestBody', () => {
const op = doc.paths['/api/users'].post as any;
expect(op.requestBody.content['application/json']).toBeDefined();
+5
View File
@@ -17,6 +17,11 @@ const route = (method: string, pattern: string): RouteDef => ({
});
describe('matchPattern', () => {
it('matches the root pattern only at root', () => {
expect(matchPattern('/', '/')).toEqual({});
expect(matchPattern('/', '/users')).toBeUndefined();
});
it('matches static and :param segments, decoding params', () => {
expect(matchPattern('/users/:id', '/users/a%20b')).toEqual({ id: 'a b' });
expect(matchPattern('/users/:id', '/users')).toBeUndefined();