From c364c3a44c360c635525819c0b6108ff38c2a65d Mon Sep 17 00:00:00 2001 From: tada5hi Date: Sun, 22 Nov 2026 20:20:02 +0100 Subject: [PATCH 1/5] chore(deps): bump routup to ^6.5.0 --- package-lock.json | 36 +++++++++++++------------- packages/assets/package.json | 2 +- packages/basic/package.json | 2 +- packages/body/package.json | 2 +- packages/cookie/package.json | 2 +- packages/cors/package.json | 2 +- packages/docs/package.json | 2 +- packages/i18n/package.json | 2 +- packages/logger/package.json | 2 +- packages/prometheus/package.json | 2 +- packages/query/package.json | 2 +- packages/rate-limit-redis/package.json | 2 +- packages/rate-limit/package.json | 2 +- packages/swagger-ui/package.json | 2 +- 14 files changed, 31 insertions(+), 31 deletions(-) diff --git a/package-lock.json b/package-lock.json index d6d2d2bf..82fcc9a3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10178,9 +10178,9 @@ } }, "node_modules/routup": { - "version": "6.4.0", - "resolved": "https://registry.npmjs.org/routup/-/routup-6.4.0.tgz", - "integrity": "sha512-RPhJ6PofKYxMrMGPtARasEKlhyk+Hf9bXUMTqYVT+rfIM6KCrpXbYdIBPbGjCJq3DFrJzCI2G2Zk6YxALDmF+w==", + "version": "6.5.0", + "resolved": "https://registry.npmjs.org/routup/-/routup-6.5.0.tgz", + "integrity": "sha512-Ldxgptw5BkHQ6LLdOedtyU7paB1j0vsb+7KKlsj4lC1f40M6XZLJftsnzb5vNMI92vV30fLITSc+31wT+CNtfg==", "license": "MIT", "workspaces": [ "docs" @@ -12898,7 +12898,7 @@ "version": "6.0.0", "license": "MIT", "devDependencies": { - "routup": "^6.3.0" + "routup": "^6.5.0" }, "peerDependencies": { "routup": "^6.3.0" @@ -12914,7 +12914,7 @@ "@routup/query": "^4.0.3" }, "devDependencies": { - "routup": "^6.3.0" + "routup": "^6.5.0" }, "peerDependencies": { "routup": "^6.3.0" @@ -12925,7 +12925,7 @@ "version": "4.1.0", "license": "MIT", "devDependencies": { - "routup": "^6.3.0" + "routup": "^6.5.0" }, "peerDependencies": { "routup": "^6.3.0" @@ -12939,7 +12939,7 @@ "cookie-es": "^3.1.1" }, "devDependencies": { - "routup": "^6.3.0" + "routup": "^6.5.0" }, "peerDependencies": { "routup": "^6.3.0" @@ -12950,7 +12950,7 @@ "version": "2.0.2", "license": "MIT", "devDependencies": { - "routup": "^6.3.0" + "routup": "^6.5.0" }, "peerDependencies": { "routup": "^6.3.0" @@ -12970,13 +12970,13 @@ "@trapi/metadata": "^2.3.0", "@trapi/swagger": "^2.3.0", "jsonata": "^2.2.2", - "routup": "^6.3.0" + "routup": "^6.5.0" }, "peerDependencies": { "@trapi/core": "^2.3.0", "@trapi/metadata": "^2.3.0", "@trapi/swagger": "^2.3.0", - "routup": "^6.3.0" + "routup": "^6.5.0" }, "peerDependenciesMeta": { "@trapi/core": { @@ -13018,7 +13018,7 @@ "ilingo": "^6.2.1", "prom-client": "^15.1.3", "redis-extension": "^3.0.0", - "routup": "^6.4.0" + "routup": "^6.5.0" }, "peerDependencies": { "routup": "^6.3.0" @@ -13030,7 +13030,7 @@ "license": "MIT", "devDependencies": { "ilingo": "^6.2.1", - "routup": "^6.3.0" + "routup": "^6.5.0" }, "peerDependencies": { "ilingo": "^6.2.1", @@ -13042,7 +13042,7 @@ "version": "2.0.2", "license": "MIT", "devDependencies": { - "routup": "^6.3.0" + "routup": "^6.5.0" }, "peerDependencies": { "routup": "^6.3.0" @@ -13054,7 +13054,7 @@ "license": "MIT", "devDependencies": { "prom-client": "^15.1.3", - "routup": "^6.4.0" + "routup": "^6.5.0" }, "peerDependencies": { "prom-client": ">=15.0.0", @@ -13070,7 +13070,7 @@ "qs": "^6.17.1" }, "devDependencies": { - "routup": "^6.3.0" + "routup": "^6.5.0" }, "peerDependencies": { "routup": "^6.3.0" @@ -13081,7 +13081,7 @@ "version": "4.0.3", "license": "MIT", "devDependencies": { - "routup": "^6.3.0" + "routup": "^6.5.0" }, "peerDependencies": { "routup": "^6.3.0" @@ -13094,7 +13094,7 @@ "devDependencies": { "@routup/rate-limit": "^4.0.3", "redis-extension": "^3.0.0", - "routup": "^6.3.0" + "routup": "^6.5.0" }, "peerDependencies": { "@routup/rate-limit": "^4.0.3", @@ -13117,7 +13117,7 @@ }, "devDependencies": { "@types/swagger-ui-dist": "^3.30.5", - "routup": "^6.3.0" + "routup": "^6.5.0" }, "peerDependencies": { "@types/swagger-ui-dist": "^3.30.5", diff --git a/packages/assets/package.json b/packages/assets/package.json index 8f248481..b534e296 100644 --- a/packages/assets/package.json +++ b/packages/assets/package.json @@ -56,7 +56,7 @@ "routup": "^6.3.0" }, "devDependencies": { - "routup": "^6.3.0" + "routup": "^6.5.0" }, "publishConfig": { "access": "public" diff --git a/packages/basic/package.json b/packages/basic/package.json index 8a3f80fa..97d058db 100644 --- a/packages/basic/package.json +++ b/packages/basic/package.json @@ -72,7 +72,7 @@ "@routup/query": "^4.0.3" }, "devDependencies": { - "routup": "^6.3.0" + "routup": "^6.5.0" }, "gitHead": "94d729e309c1eec0401afb4d8083f65ce3aa8e0b", "publishConfig": { diff --git a/packages/body/package.json b/packages/body/package.json index af7b6cad..dfec709e 100644 --- a/packages/body/package.json +++ b/packages/body/package.json @@ -53,7 +53,7 @@ "routup": "^6.3.0" }, "devDependencies": { - "routup": "^6.3.0" + "routup": "^6.5.0" }, "publishConfig": { "access": "public" diff --git a/packages/cookie/package.json b/packages/cookie/package.json index ed1c8564..2549526e 100644 --- a/packages/cookie/package.json +++ b/packages/cookie/package.json @@ -58,7 +58,7 @@ "cookie-es": "^3.1.1" }, "devDependencies": { - "routup": "^6.3.0" + "routup": "^6.5.0" }, "gitHead": "94d729e309c1eec0401afb4d8083f65ce3aa8e0b", "publishConfig": { diff --git a/packages/cors/package.json b/packages/cors/package.json index 9ca7ac84..d98df527 100644 --- a/packages/cors/package.json +++ b/packages/cors/package.json @@ -57,7 +57,7 @@ "routup": "^6.3.0" }, "devDependencies": { - "routup": "^6.3.0" + "routup": "^6.5.0" }, "publishConfig": { "access": "public" diff --git a/packages/docs/package.json b/packages/docs/package.json index 5a7385aa..6fbd532e 100644 --- a/packages/docs/package.json +++ b/packages/docs/package.json @@ -42,7 +42,7 @@ "ilingo": "^6.2.1", "prom-client": "^15.1.3", "redis-extension": "^3.0.0", - "routup": "^6.4.0" + "routup": "^6.5.0" }, "peerDependencies": { "routup": "^6.3.0" diff --git a/packages/i18n/package.json b/packages/i18n/package.json index b4213165..36b0cab4 100644 --- a/packages/i18n/package.json +++ b/packages/i18n/package.json @@ -53,7 +53,7 @@ }, "devDependencies": { "ilingo": "^6.2.1", - "routup": "^6.3.0" + "routup": "^6.5.0" }, "gitHead": "94d729e309c1eec0401afb4d8083f65ce3aa8e0b", "publishConfig": { diff --git a/packages/logger/package.json b/packages/logger/package.json index aa29a5c7..28d07696 100644 --- a/packages/logger/package.json +++ b/packages/logger/package.json @@ -58,7 +58,7 @@ "routup": "^6.3.0" }, "devDependencies": { - "routup": "^6.3.0" + "routup": "^6.5.0" }, "publishConfig": { "access": "public" diff --git a/packages/prometheus/package.json b/packages/prometheus/package.json index 48f5d5c4..7f04e85e 100644 --- a/packages/prometheus/package.json +++ b/packages/prometheus/package.json @@ -50,7 +50,7 @@ }, "devDependencies": { "prom-client": "^15.1.3", - "routup": "^6.4.0" + "routup": "^6.5.0" }, "publishConfig": { "access": "public" diff --git a/packages/query/package.json b/packages/query/package.json index 7102aea5..db0062bb 100644 --- a/packages/query/package.json +++ b/packages/query/package.json @@ -60,7 +60,7 @@ "qs": "^6.17.1" }, "devDependencies": { - "routup": "^6.3.0" + "routup": "^6.5.0" }, "gitHead": "94d729e309c1eec0401afb4d8083f65ce3aa8e0b", "publishConfig": { diff --git a/packages/rate-limit-redis/package.json b/packages/rate-limit-redis/package.json index 126cf4b6..31b3c142 100644 --- a/packages/rate-limit-redis/package.json +++ b/packages/rate-limit-redis/package.json @@ -60,7 +60,7 @@ "devDependencies": { "@routup/rate-limit": "^4.0.3", "redis-extension": "^3.0.0", - "routup": "^6.3.0" + "routup": "^6.5.0" }, "publishConfig": { "access": "public" diff --git a/packages/rate-limit/package.json b/packages/rate-limit/package.json index 3e54c435..910bb2e7 100644 --- a/packages/rate-limit/package.json +++ b/packages/rate-limit/package.json @@ -50,7 +50,7 @@ "routup": "^6.3.0" }, "devDependencies": { - "routup": "^6.3.0" + "routup": "^6.5.0" }, "publishConfig": { "access": "public" diff --git a/packages/swagger-ui/package.json b/packages/swagger-ui/package.json index 97218e2d..639951fa 100644 --- a/packages/swagger-ui/package.json +++ b/packages/swagger-ui/package.json @@ -61,7 +61,7 @@ }, "devDependencies": { "@types/swagger-ui-dist": "^3.30.5", - "routup": "^6.3.0" + "routup": "^6.5.0" }, "publishConfig": { "access": "public" From 2a0fff280c90d90b5ef817ee5192b88ee71cf579 Mon Sep 17 00:00:00 2001 From: tada5hi Date: Sun, 22 Nov 2026 20:20:03 +0100 Subject: [PATCH 2/5] test(swagger-ui): vary the mount path with a param, as routup matches case-sensitively routup 6.5.0 matches static path segments case-sensitively, so /DoCs no longer reaches a /docs mount. --- packages/swagger-ui/src/handler.ts | 2 +- packages/swagger-ui/test/unit/ui/hardening.spec.ts | 12 +++++++----- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/packages/swagger-ui/src/handler.ts b/packages/swagger-ui/src/handler.ts index 71743ff8..c8412326 100644 --- a/packages/swagger-ui/src/handler.ts +++ b/packages/swagger-ui/src/handler.ts @@ -204,7 +204,7 @@ export function createUIHandler( let contentType : string | undefined; if (SHELL_PATHS.includes(relativePath)) { - // Computed per request: the mount path may differ in casing or params. + // Computed per request: the mount path may contain params. output = shell.replace('<% baseHref %>', () => escapeHTML(buildBaseHref(event.mountPath))); contentType = 'text/html; charset=utf-8'; } else if (relativePath === '/swagger-initializer.js') { diff --git a/packages/swagger-ui/test/unit/ui/hardening.spec.ts b/packages/swagger-ui/test/unit/ui/hardening.spec.ts index d3dd30b9..f64f2e44 100644 --- a/packages/swagger-ui/test/unit/ui/hardening.spec.ts +++ b/packages/swagger-ui/test/unit/ui/hardening.spec.ts @@ -315,15 +315,17 @@ describe('src/ui hardening', () => { describe('base href', () => { it('should compute the base href per request', async () => { - const router = createRouter(); + const router = new App(); + + router.use('/:tenant/docs', swaggerUI('test/data/swagger.json')); - let response = await router.fetch(createTestRequest('/DoCs')); + let response = await router.fetch(createTestRequest('/a/docs')); expect(response.status).toEqual(200); - expect(await response.text()).toContain(''); + expect(await response.text()).toContain(''); - response = await router.fetch(createTestRequest('/docs')); - expect(await response.text()).toContain(''); + response = await router.fetch(createTestRequest('/b/docs')); + expect(await response.text()).toContain(''); }); }); From 9ee0989494a9256d89bc664bd658a02174f1e3c3 Mon Sep 17 00:00:00 2001 From: tada5hi Date: Sun, 22 Nov 2026 20:20:08 +0100 Subject: [PATCH 3/5] feat(decorators): reject a request body @DConsumes does not list A request with a body whose content-type matches none of the class-level and method-level @DConsumes media types is answered with 415 (400 for a malformed header) before the method runs, using routup's assertRequestContentType. A request without a body is not checked. The routup peer range moves to ^6.5.0. --- packages/decorators/README.md | 2 +- packages/decorators/package.json | 4 +- packages/decorators/src/mixed/module.ts | 31 +++- packages/decorators/src/mount.ts | 18 ++- packages/decorators/src/types.ts | 12 +- packages/decorators/src/utils/meta.ts | 11 +- .../decorators/test/unit/consumes.spec.ts | 142 ++++++++++++++++++ packages/docs/examples/decorators/consumes.ts | 38 +++++ packages/docs/src/decorators/controllers.md | 26 +++- packages/docs/src/decorators/index.md | 2 +- packages/docs/src/guide/getting-started.md | 2 +- .../test/unit/examples/decorators.spec.ts | 37 +++++ 12 files changed, 308 insertions(+), 17 deletions(-) create mode 100644 packages/decorators/test/unit/consumes.spec.ts create mode 100644 packages/docs/examples/decorators/consumes.ts diff --git a/packages/decorators/README.md b/packages/decorators/README.md index 18da7137..ea25fe91 100644 --- a/packages/decorators/README.md +++ b/packages/decorators/README.md @@ -39,7 +39,7 @@ Functional handlers need no compiler flags and no class instances; decorators ne npm install @routup/decorators ``` -ESM only, Node.js >=24. Peer dependency: `routup@^6.3.0`. The decorators are legacy TypeScript decorators: set `"experimentalDecorators": true` in your `tsconfig.json`, or the legacy decorator mode of your compiler ([TypeScript settings](https://plugins.routup.dev/decorators/#typescript-settings)). `@routup/body`, `@routup/cookie` and `@routup/query` are installed as dependencies; register their plugins for `@DBody()`, `@DQuery()`, `@DCookie()` and `@DCookies()`. For OpenAPI generation, also install the optional peers `@trapi/core`, `@trapi/metadata` and `@trapi/swagger`. +ESM only, Node.js >=24. Peer dependency: `routup@^6.5.0`. The decorators are legacy TypeScript decorators: set `"experimentalDecorators": true` in your `tsconfig.json`, or the legacy decorator mode of your compiler ([TypeScript settings](https://plugins.routup.dev/decorators/#typescript-settings)). `@routup/body`, `@routup/cookie` and `@routup/query` are installed as dependencies; register their plugins for `@DBody()`, `@DQuery()`, `@DCookie()` and `@DCookies()`. For OpenAPI generation, also install the optional peers `@trapi/core`, `@trapi/metadata` and `@trapi/swagger`. ## Quick Start diff --git a/packages/decorators/package.json b/packages/decorators/package.json index 39ee92aa..ff4be5d8 100644 --- a/packages/decorators/package.json +++ b/packages/decorators/package.json @@ -58,7 +58,7 @@ "@trapi/core": "^2.3.0", "@trapi/metadata": "^2.3.0", "@trapi/swagger": "^2.3.0", - "routup": "^6.3.0" + "routup": "^6.5.0" }, "peerDependenciesMeta": { "@trapi/core": { @@ -81,7 +81,7 @@ "@trapi/metadata": "^2.3.0", "@trapi/swagger": "^2.3.0", "jsonata": "^2.2.2", - "routup": "^6.3.0" + "routup": "^6.5.0" }, "publishConfig": { "access": "public" diff --git a/packages/decorators/src/mixed/module.ts b/packages/decorators/src/mixed/module.ts index 312ffaae..ba1a9d0f 100644 --- a/packages/decorators/src/mixed/module.ts +++ b/packages/decorators/src/mixed/module.ts @@ -42,10 +42,37 @@ export function DTags(...names: string[]) { } /** - * Declare the media types a request body may have (OpenAPI only). + * Declare the media types a request body may have (method or class). + * + * At runtime a request with a body whose `content-type` matches none of the + * class-level and method-level media types is answered with `415` (`400` for + * a malformed header) before the method runs. A request without a body is + * not checked. + * + * @param mediaTypes - e.g. `'application/json'`, `'application/*+json'` or `'text/*'`. */ export function DConsumes(...mediaTypes: string[]) { - return (...args: any) => { }; + return ( + target: any, + propertyKey?: string | symbol, + ) : void => { + if (typeof propertyKey === 'undefined') { + const meta = useDecoratorMeta(target.prototype); + + meta.consumes = [...mediaTypes, ...meta.consumes]; + + return; + } + + /* istanbul ignore next */ + if (typeof propertyKey !== 'string') { + return; + } + + const options = useDecoratorResponseOptions(target, propertyKey); + + options.consumes = [...mediaTypes, ...(options.consumes ?? [])]; + }; } /** diff --git a/packages/decorators/src/mount.ts b/packages/decorators/src/mount.ts index f8b05b1c..bcd46324 100644 --- a/packages/decorators/src/mount.ts +++ b/packages/decorators/src/mount.ts @@ -1,5 +1,10 @@ import type { IApp, MethodName } from 'routup'; -import { App, defineCoreHandler, toResponse } from 'routup'; +import { + App, + assertRequestContentType, + defineCoreHandler, + toResponse, +} from 'routup'; import { buildDecoratorMethodArguments } from './method'; import type { ClassType, ControllerRoute, DecoratorMeta } from './types'; import { @@ -58,6 +63,17 @@ function buildControllerApp( method: method.method as MethodName, path: method.url, fn: async (event) => { + // The OpenAPI document lists the class and the method media + // types together, so a request may have any of them. + const consumes = [ + ...meta.consumes, + ...(meta.responses[propertyKey]?.consumes ?? []), + ]; + + if (consumes.length > 0 && event.request.body !== null) { + assertRequestContentType(event, consumes); + } + const args = await buildDecoratorMethodArguments( { event }, meta.parameters[propertyKey] ?? [], diff --git a/packages/decorators/src/types.ts b/packages/decorators/src/types.ts index fd3aa147..6ef620ce 100644 --- a/packages/decorators/src/types.ts +++ b/packages/decorators/src/types.ts @@ -17,6 +17,11 @@ export type DecoratorResponseOptions = { * Media types declared with a method-level `@DProduces()`. */ produces?: string[], + + /** + * Request body media types declared with a method-level `@DConsumes()`. + */ + consumes?: string[], }; export type DecoratorMeta = { @@ -34,7 +39,12 @@ export type DecoratorMeta = { produces: string[], /** - * Response options (`@DStatus()`, method-level `@DProduces()`) per + * Request body media types declared with a class-level `@DConsumes()`. + */ + consumes: string[], + + /** + * Response options (`@DStatus()`, method-level `@DProduces()` and `@DConsumes()`) per * method. Kept apart from `methods`, since these decorators may be * applied before the verb decorator. */ diff --git a/packages/decorators/src/utils/meta.ts b/packages/decorators/src/utils/meta.ts index f44e81e3..7dd29f60 100644 --- a/packages/decorators/src/utils/meta.ts +++ b/packages/decorators/src/utils/meta.ts @@ -38,6 +38,7 @@ export function useResolvedDecoratorMeta(target: Record) : Decorato meta.middlewares ??= []; meta.methods ??= Object.create(null); meta.produces ??= []; + meta.consumes ??= []; meta.responses ??= Object.create(null); meta.parameters ??= Object.create(null); @@ -52,6 +53,7 @@ export function useResolvedDecoratorMeta(target: Record) : Decorato middlewares: [], methods: Object.create(null), produces: [], + consumes: [], responses: Object.create(null), parameters: Object.create(null), } satisfies DecoratorMeta; @@ -80,9 +82,11 @@ function copyDecoratorMeta(meta: DecoratorMeta) : DecoratorMeta { const inputResponses : LooseEntries = meta.responses; for (const [key, response] of Object.entries(inputResponses)) { - responses[key] = response?.produces ? - { ...response, produces: [...response.produces] } : - response && { ...response }; + responses[key] = response && { + ...response, + ...(response.produces ? { produces: [...response.produces] } : {}), + ...(response.consumes ? { consumes: [...response.consumes] } : {}), + }; } const parameters : Record = Object.create(null); @@ -102,6 +106,7 @@ function copyDecoratorMeta(meta: DecoratorMeta) : DecoratorMeta { middlewares: Array.isArray(meta.middlewares) ? [...meta.middlewares] : meta.middlewares, methods: methods as DecoratorMeta['methods'], produces: [...meta.produces], + consumes: [...meta.consumes], responses: responses as DecoratorMeta['responses'], parameters: parameters as DecoratorMeta['parameters'], }; diff --git a/packages/decorators/test/unit/consumes.spec.ts b/packages/decorators/test/unit/consumes.spec.ts new file mode 100644 index 00000000..ae37b81a --- /dev/null +++ b/packages/decorators/test/unit/consumes.spec.ts @@ -0,0 +1,142 @@ +// eslint-disable-next-line max-classes-per-file +import { describe, expect, it } from 'vitest'; +import { body } from '@routup/body'; +import { App } from 'routup'; +import { + DBody, + DConsumes, + DController, + DGet, + DPost, + DPut, + decorators, + useDecoratorMeta, +} from '../../src'; + +@DController('/items') +@DConsumes('application/json') +class ItemController { + @DGet('/') + list() { + return []; + } + + @DPost('/') + create(@DBody() body: unknown) { + return body; + } + + @DPut('/') + @DConsumes('application/*+xml') + replace() { + return 'replaced'; + } +} + +@DController('/free') +class FreeController { + @DPost('/') + create() { + return 'created'; + } +} + +function createApp() { + const app = new App(); + + app.use(body()); + app.use(decorators({ controllers: [ItemController, FreeController] })); + + return app; +} + +function send(app: App, method: string, path: string, contentType?: string) { + return app.fetch(new Request(`http://localhost${path}`, { + method, + body: '{"a":1}', + headers: contentType ? { 'content-type': contentType } : {}, + })); +} + +describe('DConsumes', () => { + it('should accept a listed media type, ignoring parameters and case', async () => { + const app = createApp(); + + let response = await send(app, 'POST', '/items', 'application/json; charset=utf-8'); + + expect(response.status).toEqual(200); + expect(await response.json()).toEqual({ a: 1 }); + + response = await send(app, 'POST', '/items', 'Application/JSON'); + expect(response.status).toEqual(200); + }); + + it('should answer 415 for an unlisted media type', async () => { + const response = await send(createApp(), 'POST', '/items', 'text/plain'); + + expect(response.status).toEqual(415); + }); + + it('should answer 415 for a body without content-type', async () => { + const app = createApp(); + const response = await app.fetch(new Request('http://localhost/items', { + method: 'POST', + body: new Uint8Array([1]), + })); + + expect(response.status).toEqual(415); + }); + + it('should answer 400 for a malformed content-type', async () => { + const response = await send(createApp(), 'POST', '/items', 'json'); + + expect(response.status).toEqual(400); + }); + + it('should not check a request without a body', async () => { + const response = await createApp().fetch(new Request('http://localhost/items')); + + expect(response.status).toEqual(200); + }); + + it('should accept the class and the method media types together', async () => { + const app = createApp(); + + let response = await send(app, 'PUT', '/items', 'application/atom+xml'); + + expect(response.status).toEqual(200); + + response = await send(app, 'PUT', '/items', 'application/json'); + expect(response.status).toEqual(200); + + response = await send(app, 'PUT', '/items', 'text/xml'); + expect(response.status).toEqual(415); + }); + + it('should not check a controller without the decorator', async () => { + const response = await send(createApp(), 'POST', '/free', 'text/plain'); + + expect(response.status).toEqual(200); + }); + + it('should let a subclass add media types without changing the parent', async () => { + @DController('/child') + class ChildController extends ItemController { + @DPost('/') + @DConsumes('text/plain') + override create(@DBody() body: unknown) { + return body; + } + } + + const app = new App(); + + app.use(body()); + app.use(decorators({ controllers: [ItemController, ChildController] })); + + expect((await send(app, 'POST', '/child', 'text/plain')).status).toEqual(200); + expect((await send(app, 'POST', '/child', 'application/json')).status).toEqual(200); + expect((await send(app, 'POST', '/items', 'text/plain')).status).toEqual(415); + expect(useDecoratorMeta(ItemController.prototype).consumes).toEqual(['application/json']); + }); +}); diff --git a/packages/docs/examples/decorators/consumes.ts b/packages/docs/examples/decorators/consumes.ts new file mode 100644 index 00000000..84635340 --- /dev/null +++ b/packages/docs/examples/decorators/consumes.ts @@ -0,0 +1,38 @@ +import { App } from 'routup'; +import { body } from '@routup/body'; +import { + DBody, + DConsumes, + DController, + DGet, + DPost, + DPut, + decorators, +} from '@routup/decorators'; + +@DController('/notes') +@DConsumes('application/json') +class NoteController { + // a GET request has no body, so it is not checked + @DGet('') + list() { + return []; + } + + @DPost('') + create(@DBody() note: unknown) { + return note; + } + + // accepts application/json (class) and any text type (method) + @DPut('/:id') + @DConsumes('text/*') + replace(@DBody() note: unknown) { + return { note }; + } +} + +export const app = new App(); + +app.use(body()); +app.use(decorators({ controllers: [NoteController] })); diff --git a/packages/docs/src/decorators/controllers.md b/packages/docs/src/decorators/controllers.md index b71664c8..faaaa073 100644 --- a/packages/docs/src/decorators/controllers.md +++ b/packages/docs/src/decorators/controllers.md @@ -1,12 +1,12 @@ --- title: Decorators Controllers -description: "@DController, method decorators (@DGet, @DPost, @DPut, @DPatch, @DDelete), @DStatus, @DReturns and @DProduces." +description: "@DController, method decorators (@DGet, @DPost, @DPut, @DPatch, @DDelete), @DConsumes, @DStatus, @DReturns and @DProduces." relatedPlugins: [swagger-ui] --- # Controllers -A controller is a class whose methods answer requests. `@DController()` gives the class its path and middlewares, a method decorator turns a method into a route, and `@DStatus()` and `@DProduces()` shape a successful response. The [documentation decorators](#documentation-decorators) only feed the OpenAPI document. +A controller is a class whose methods answer requests. `@DController()` gives the class its path and middlewares, a method decorator turns a method into a route, `@DConsumes()` checks the media type of a request body, and `@DStatus()` and `@DProduces()` shape a successful response. The [documentation decorators](#documentation-decorators) only feed the OpenAPI document. ## Controller Class @@ -118,6 +118,23 @@ declare function DProduces(...mediaTypes: string[]): (target: any, propertyKey?: Any other return type, and the return type under a class-level `@DProduces()`, is kept as the schema. A schema declared with `@DReturns()` at the same status takes precedence. `Response`, `Blob` and `File` are matched by name, so do not give a payload type of your own one of these names. +## Request Media Type + +### `DConsumes()` + +Declares the media types a request body of a method, or of every method of a class, may have. + +```typescript +declare function DConsumes(...mediaTypes: string[]): (target: any, propertyKey?: string | symbol) => void; +``` + +<<< @/../examples/decorators/consumes.ts + +- **Runtime:** a request with a body is checked with routup's [`assertRequestContentType()`](https://routup.dev/api/request-helpers) before its parameters are read and the method runs: a missing or unlisted `content-type` answers `415`, a malformed one `400`. The check ignores parameters (`charset`) and case, and a media type may be a pattern such as `text/*` or `application/*+json`. A request without a body, such as a `GET`, is not checked. +- A route accepts the class-level and the method-level media types together, as the OpenAPI document lists them. +- Middlewares run before the check, so an authentication middleware still answers first. +- **OpenAPI:** the media types are the request body media types of the operation. + ## Middleware A middleware is a class with a `run(event)` method, the [`HandlerInterface`](/decorators/helpers#types). Return `event.next()` to continue to the route, or return a value or throw to answer the request yourself. The [middleware recipe](/decorators/recipes#a-controller-with-middleware) shows both kinds. @@ -137,10 +154,10 @@ A controller can extend a decorated class: <<< @/../examples/decorators/inheritance.ts -- **What is inherited.** A subclass inherits the routes of its parent with their parameter decorators, `@DStatus()` and `@DProduces()`, and the controller path, middlewares and class-level `@DProduces()`. +- **What is inherited.** A subclass inherits the routes of its parent with their parameter decorators, `@DConsumes()`, `@DStatus()` and `@DProduces()`, and the controller path, middlewares and class-level `@DConsumes()` and `@DProduces()`. - **Its own copy.** On first use a subclass gets its own copy of that metadata. Its decorators add to or overwrite the copy as on a class without a parent; the built-in decorators and the `create*` factories never change the parent or another subclass: - an override with a method decorator (`@DGet()`, ...) replaces the route of that method, including its inherited method middlewares (repeat them on the override), and keeps its inherited parameter decorators; the override's own are added, and at an index the parent used only the override's parameter decorator is applied - - its `@DStatus()` overwrites the inherited status, and its `@DProduces()` media types come before the inherited ones, so they are sent + - its `@DStatus()` overwrites the inherited status, and its `@DProduces()` media types come before the inherited ones, so they are sent; its `@DConsumes()` media types are accepted next to the inherited ones - an override without decorators keeps the inherited route, which calls the override - `@DController(path)` without middlewares starts from the class middlewares of the parent (dropping what a decorator below it added); an array, also `[]`, replaces them - **Order.** Inherited routes are registered first, in the declaration order of the parent: with a parent `@DGet('/:id')`, a subclass's `@DGet('/me')` is answered by the parent's route. Declare such a static route in the parent, or let the parent's method hand it over with `event.next()`. @@ -158,7 +175,6 @@ These decorators do nothing at runtime; the [OpenAPI preset](/decorators/#openap |-----------|----|------------------------| | `@DTags(...names)` | class, method | Adds tags | | `@DReturns(status, description?, example?)` | class, method | Adds a response with that status; the type argument is its schema | -| `@DConsumes(...mediaTypes)` | class, method | Sets the request body media types | | `@DDescription(status, description?, example?)` | class, method | Deprecated alias of `@DReturns()` | | `@DExample(example)` | method | Adds an example to the default response | | `@DSecurity(scopes?, name?)` | class, method | Adds a security requirement, named `'default'` without `name` | diff --git a/packages/docs/src/decorators/index.md b/packages/docs/src/decorators/index.md index 764b41a8..3aa6080e 100644 --- a/packages/docs/src/decorators/index.md +++ b/packages/docs/src/decorators/index.md @@ -16,7 +16,7 @@ Write routup routes as TypeScript classes: `@DController()` sets the path of a c ## Requirements - Node.js 24 or later, ESM only -- `routup` `^6.3.0` (peer dependency) +- `routup` `^6.5.0` (peer dependency) - `@routup/body`, `@routup/cookie` and `@routup/query` are installed as dependencies. `@DBody()`, `@DQuery()`, `@DCookie()` and `@DCookies()` need their plugins registered before `decorators()`: see [Decorators and the Parsers](/guide/combining#decorators-and-the-parsers) - For [OpenAPI generation](#openapi-generation) only: `@trapi/core`, `@trapi/metadata` and `@trapi/swagger` `^2.3.0` (optional peer dependencies). Only `@routup/decorators/preset` imports them, so install them where you generate the document. An older `@trapi/metadata` ignores `@DProduces` without an error. `@trapi/metadata` has `typescript` `>=5.0.0` as a peer dependency diff --git a/packages/docs/src/guide/getting-started.md b/packages/docs/src/guide/getting-started.md index 248a2f91..ef05963e 100644 --- a/packages/docs/src/guide/getting-started.md +++ b/packages/docs/src/guide/getting-started.md @@ -18,7 +18,7 @@ The plugins are ESM only and are developed and tested on Node.js 24 or later. Ea | [`@routup/body`](/body/) | `^6.3.0` | none | | [`@routup/cookie`](/cookie/) | `^6.3.0` | none | | [`@routup/cors`](/cors/) | `^6.3.0` | none | -| [`@routup/decorators`](/decorators/) | `^6.3.0` | `@trapi/core` `^2.3.0`, `@trapi/metadata` `^2.3.0`, `@trapi/swagger` `^2.3.0` (optional, for OpenAPI generation) | +| [`@routup/decorators`](/decorators/) | `^6.5.0` | `@trapi/core` `^2.3.0`, `@trapi/metadata` `^2.3.0`, `@trapi/swagger` `^2.3.0` (optional, for OpenAPI generation) | | [`@routup/i18n`](/i18n/) | `^6.3.0` | `ilingo` `^6.2.1` | | [`@routup/logger`](/logger/) | `^6.3.0` | none | | [`@routup/prometheus`](/prometheus/) | `^6.4.0` | `prom-client` `>=15.0.0` | diff --git a/packages/docs/test/unit/examples/decorators.spec.ts b/packages/docs/test/unit/examples/decorators.spec.ts index f20e60fb..587637f0 100644 --- a/packages/docs/test/unit/examples/decorators.spec.ts +++ b/packages/docs/test/unit/examples/decorators.spec.ts @@ -24,6 +24,7 @@ import { } from '@routup/decorators'; import { describe, expect, it } from 'vitest'; import { app as cart } from '../../../examples/decorators/cart'; +import { app as consumes } from '../../../examples/decorators/consumes'; import { app as crud } from '../../../examples/decorators/crud'; import { app as customDecorator } from '../../../examples/decorators/custom-decorator'; import { preset } from '../../../examples/decorators/custom-preset'; @@ -353,6 +354,42 @@ describe('return-values', () => { }); }); +describe('consumes', () => { + function send(method: string, urlPath: string, contentType: string): Promise { + return call(consumes, urlPath, { + method, + headers: { 'content-type': contentType }, + body: '{"text":"hi"}', + }); + } + + it('accepts a declared media type, ignoring parameters', async () => { + const response = await send('POST', '/notes', 'application/json; charset=utf-8'); + + expect(response.status).toBe(200); + expect(await response.json()).toEqual({ text: 'hi' }); + }); + + it('answers 415 for another media type or none, and 400 for a malformed one', async () => { + expect((await send('POST', '/notes', 'text/plain')).status).toBe(415); + expect((await send('POST', '/notes', 'json')).status).toBe(400); + + const response = await call(consumes, '/notes', { method: 'POST', body: new Uint8Array([1]) }); + + expect(response.status).toBe(415); + }); + + it('does not check a request without a body', async () => { + expect((await call(consumes, '/notes')).status).toBe(200); + }); + + it('accepts the class and the method media types of a route', async () => { + expect((await send('PUT', '/notes/1', 'application/json')).status).toBe(200); + expect((await send('PUT', '/notes/1', 'text/markdown')).status).toBe(200); + expect((await send('PUT', '/notes/1', 'application/xml')).status).toBe(415); + }); +}); + describe('status', () => { it('answers with the @DStatus code', async () => { const response = await call(status, '/jobs', { method: 'POST' }); From c8c189b588fa289d92e401f4bd48446f73ef6ce0 Mon Sep 17 00:00:00 2001 From: tada5hi Date: Sun, 22 Nov 2026 20:20:10 +0100 Subject: [PATCH 4/5] feat(rate-limit): add a trustProxy option for the default key The default keyGenerator passes trustProxy to getRequestIP() instead of a fixed true. It defaults to true, so the key is unchanged; trustProxy: false keys a directly reachable app by the connection address, so a client cannot pick its key with X-Forwarded-For. Closes #767 --- .../docs/examples/rate-limit/trust-proxy.ts | 11 +++++ packages/docs/src/rate-limit/configuration.md | 11 ++++- packages/docs/src/rate-limit/index.md | 2 +- .../test/unit/examples/rate-limit.spec.ts | 24 ++++++++++ packages/rate-limit/README.md | 2 +- packages/rate-limit/src/types.ts | 12 ++++- packages/rate-limit/src/utils/options.ts | 3 +- .../rate-limit/test/unit/trust-proxy.spec.ts | 47 +++++++++++++++++++ 8 files changed, 106 insertions(+), 6 deletions(-) create mode 100644 packages/docs/examples/rate-limit/trust-proxy.ts create mode 100644 packages/rate-limit/test/unit/trust-proxy.spec.ts diff --git a/packages/docs/examples/rate-limit/trust-proxy.ts b/packages/docs/examples/rate-limit/trust-proxy.ts new file mode 100644 index 00000000..e523a55b --- /dev/null +++ b/packages/docs/examples/rate-limit/trust-proxy.ts @@ -0,0 +1,11 @@ +import { App, defineCoreHandler } from 'routup'; +import { rateLimit } from '@routup/rate-limit'; + +export const app = new App(); + +app.use(rateLimit({ + // reachable directly: key by the address of the connection + trustProxy: false, +})); + +app.get('/', defineCoreHandler(() => 'Hello, World!')); diff --git a/packages/docs/src/rate-limit/configuration.md b/packages/docs/src/rate-limit/configuration.md index 625513d1..720a92df 100644 --- a/packages/docs/src/rate-limit/configuration.md +++ b/packages/docs/src/rate-limit/configuration.md @@ -20,7 +20,8 @@ relatedPlugins: [rate-limit-redis] | `skipSuccessfulRequest` | `boolean` | `false` | Do not count requests that `requestWasSuccessful` accepts | | `countAfterResponse` | `boolean` | `false` | Count a request after its response instead of before forwarding it; needs a store with `get()` | | `requestWasSuccessful` | `(event, response) => boolean` | `response.status < 400` | Decides what counts as successful | -| `keyGenerator` | `(event) => string \| Promise` | `getRequestIP(event, { trustProxy: true }) \|\| '127.0.0.1'` | Identifies the client | +| `trustProxy` | `boolean \| number \| string \| string[] \| (address, hop) => boolean` | `true` | Proxies the default `keyGenerator` trusts for `X-Forwarded-For` | +| `keyGenerator` | `(event) => string \| Promise` | `getRequestIP(event, { trustProxy }) \|\| '127.0.0.1'` | Identifies the client | | `handler` | `(event, options) => unknown` | Sets `statusCode`, returns `message` | Builds the response of a rejected request | | `skip` | `(event) => boolean \| Promise` | `() => false` | Return `true` to bypass the limiter for a request | | `store` | `Store` | `new MemoryStore()` | Where hit counts are kept | @@ -65,13 +66,19 @@ Called with the event and the downstream `Response` when `skipFailedRequest` or A request whose handler throws is judged by the response of the [error handler](https://routup.dev/guide/error-handling) that answers it. When no error handler answers, the middleware sees no response at all and treats the request as not successful, whatever status the fallback response gets. +### `trustProxy` + +Which proxies the default `keyGenerator` trusts when it takes the client address from `X-Forwarded-For`, passed to routup's [`getRequestIP()`](https://routup.dev/api/request-helpers): `true` trusts every proxy, `false` none, so the address of the connection is the key. A hop count, an address or CIDR (also a list of them) or a function `(address, hop) => boolean` trusts only those proxies. `undefined` uses the `trustProxy` option of the app. A custom `keyGenerator` does not read it. + +<<< @/../examples/rate-limit/trust-proxy.ts + ### `keyGenerator` Returns the key a request is counted under (sync or async). Keying by something other than the address, such as an API token, limits per account: <<< @/../examples/rate-limit/key-by-token.ts -The default key trusts `X-Forwarded-For`: `getRequestIP(event, { trustProxy: true })` takes the client address from that header, which a client can set itself when the app is not behind a proxy that overwrites it. When the app is reachable directly, pass a `keyGenerator` that does not trust the header, such as `(event) => getRequestIP(event) ?? '127.0.0.1'`. Requests without a client address, such as calls to `app.fetch()` in tests, all share the key `'127.0.0.1'`. +The default key trusts `X-Forwarded-For` ([`trustProxy`](#trustproxy) is `true`): the client address comes from that header, which a client can set itself when the app is not behind a proxy that overwrites it. When the app is reachable directly, set `trustProxy: false`; behind your own proxies, list them. Requests without a client address, such as calls to `app.fetch()` in tests, all share the key `'127.0.0.1'`. ### `handler` diff --git a/packages/docs/src/rate-limit/index.md b/packages/docs/src/rate-limit/index.md index 0521f8a9..b2ff7128 100644 --- a/packages/docs/src/rate-limit/index.md +++ b/packages/docs/src/rate-limit/index.md @@ -43,7 +43,7 @@ curl -i http://localhost:3000/ | Step | What happens | |------|--------------| | Skip | `skip(event)` returning `true` passes the request on uncounted and without headers | -| Key | The client key is `getRequestIP(event, { trustProxy: true })`, or `'127.0.0.1'` when there is no address | +| Key | The client key is `getRequestIP(event, { trustProxy })` with [`trustProxy`](/rate-limit/configuration#trustproxy) `true` by default, or `'127.0.0.1'` when there is no address | | Count | The store increments the key's counter; every request that is not skipped counts, rejected ones too | | Quota | `max` is read after the increment (default `5` per `windowMs` of one minute) | | Reject | Once the counter exceeds a `max` other than `0`, `handler` answers (default: status `429`, the `message` text) and the routes behind it do not run | diff --git a/packages/docs/test/unit/examples/rate-limit.spec.ts b/packages/docs/test/unit/examples/rate-limit.spec.ts index 6555eb38..3aa4e824 100644 --- a/packages/docs/test/unit/examples/rate-limit.spec.ts +++ b/packages/docs/test/unit/examples/rate-limit.spec.ts @@ -23,6 +23,7 @@ import { app as limitPath } from '../../../examples/rate-limit/limit-path'; import { app as quickStart } from '../../../examples/rate-limit/quick-start'; import { app as readInfo } from '../../../examples/rate-limit/read-info'; import { app as tieredQuota } from '../../../examples/rate-limit/tiered-quota'; +import { app as trustProxy } from '../../../examples/rate-limit/trust-proxy'; // Runs before the samples are imported: their MemoryStore reads the clock // and starts its reset interval when the module loads. @@ -234,6 +235,29 @@ describe('key-by-token', () => { }); }); +describe('trust-proxy', () => { + it('keys by the connection address, not by X-Forwarded-For', async () => { + const spoofed = await fetchTimes(trustProxy, 6, '/', { ip: '10.0.0.1', headers: { 'x-forwarded-for': '203.0.113.7' } }); + const other = await trustProxy.fetch(request('/', { ip: '10.0.0.1', headers: { 'x-forwarded-for': '203.0.113.8' } })); + + expect(spoofed[5]!.status).toBe(429); + expect(other.status).toBe(429); + }); + + it('trusts X-Forwarded-For by default', async () => { + const app = new App(); + + app.use(rateLimit({ max: 1 })); + app.get('/', defineCoreHandler(() => 'ok')); + + await app.fetch(request('/', { ip: '10.0.0.1', headers: { 'x-forwarded-for': '203.0.113.7' } })); + + const other = await app.fetch(request('/', { ip: '10.0.0.1', headers: { 'x-forwarded-for': '203.0.113.8' } })); + + expect(other.status).toBe(200); + }); +}); + describe('json-handler', () => { it('answers a rejected request with JSON', async () => { const [, rejected] = await fetchTimes(jsonHandler, 2); diff --git a/packages/rate-limit/README.md b/packages/rate-limit/README.md index 9365f1ba..07238bdf 100644 --- a/packages/rate-limit/README.md +++ b/packages/rate-limit/README.md @@ -26,7 +26,7 @@ A login form, a password reset or a public API needs a cap on how often one clie | Concern | Without it | With @routup/rate-limit | |---------|------------|-------------------------| | Counting | A `Map` of hits per IP plus a reset timer | `MemoryStore` counts per key and resets every `windowMs` | -| Client identity | Read `X-Forwarded-For` or the socket address yourself | `getRequestIP(event, { trustProxy: true })` by default, replaceable with `keyGenerator` | +| Client identity | Read `X-Forwarded-For` or the socket address yourself | `getRequestIP(event, { trustProxy })` by default, with the trusted proxies set by `trustProxy`, replaceable with `keyGenerator` | | Headers | Compute and set limit, remaining and reset values | `RateLimit-Limit`, `RateLimit-Remaining`, `RateLimit-Reset`, and `Retry-After` when blocked | | Rejection | Return a status and body in every guarded route | `statusCode` (`429`) and `message`, or your own `handler` | | Which requests count | Undo the counter after the response | `skipFailedRequest`, `skipSuccessfulRequest`, `requestWasSuccessful` | diff --git a/packages/rate-limit/src/types.ts b/packages/rate-limit/src/types.ts index e4649096..7fc8422d 100644 --- a/packages/rate-limit/src/types.ts +++ b/packages/rate-limit/src/types.ts @@ -1,4 +1,4 @@ -import type { IAppEvent } from 'routup'; +import type { IAppEvent, RequestIpOptions } from 'routup'; import type { Store } from './store'; /** @@ -117,6 +117,16 @@ export type Options = { */ countAfterResponse: boolean + /** + * Which proxies the default `keyGenerator` trusts when it reads the + * client address from `X-Forwarded-For`, as `getRequestIP()` takes it: + * `true` trusts every proxy, `false` none (the connection address is + * used), or a hop count, address list or predicate. + * + * Defaults to `true`. + */ + trustProxy: RequestIpOptions['trustProxy'] + /** * Method to generate custom identifiers for clients. * diff --git a/packages/rate-limit/src/utils/options.ts b/packages/rate-limit/src/utils/options.ts index 2cf92081..9e0b7f12 100644 --- a/packages/rate-limit/src/utils/options.ts +++ b/packages/rate-limit/src/utils/options.ts @@ -15,7 +15,8 @@ export function normalizeHandlerOptions(input: OptionsInput = {}) : Options { countAfterResponse: false, requestWasSuccessful: (_event: IAppEvent, response: Response): boolean => response.status < 400, skip: (_event: IAppEvent): boolean => false, - keyGenerator: (event: IAppEvent): string => getRequestIP(event, { trustProxy: true }) || '127.0.0.1', + trustProxy: true, + keyGenerator: (event: IAppEvent): string => getRequestIP(event, { trustProxy: options.trustProxy }) || '127.0.0.1', async handler( event: IAppEvent, _optionsUsed: Options, diff --git a/packages/rate-limit/test/unit/trust-proxy.spec.ts b/packages/rate-limit/test/unit/trust-proxy.spec.ts new file mode 100644 index 00000000..eda8aced --- /dev/null +++ b/packages/rate-limit/test/unit/trust-proxy.spec.ts @@ -0,0 +1,47 @@ +import { describe, expect, it } from 'vitest'; +import { App, defineCoreHandler } from 'routup'; +import type { OptionsInput } from '../../src'; +import { rateLimit } from '../../src'; + +function createApp(options: OptionsInput = {}) { + const app = new App(); + + app.use(rateLimit({ max: 1, ...options })); + app.use(defineCoreHandler(() => 'ok')); + + return app; +} + +function send(app: App, forwardedFor: string) { + // the address of the proxy that connected + const request = Object.assign( + new Request('http://localhost/', { headers: { 'x-forwarded-for': forwardedFor } }), + { ip: '192.168.0.1' }, + ); + + return app.fetch(request); +} + +describe('trustProxy', () => { + it('should key by x-forwarded-for by default', async () => { + const app = createApp(); + + expect((await send(app, '10.0.0.1')).status).toEqual(200); + expect((await send(app, '10.0.0.2')).status).toEqual(200); + expect((await send(app, '10.0.0.1')).status).toEqual(429); + }); + + it('should ignore x-forwarded-for when proxies are not trusted', async () => { + const app = createApp({ trustProxy: false }); + + expect((await send(app, '10.0.0.1')).status).toEqual(200); + expect((await send(app, '10.0.0.2')).status).toEqual(429); + }); + + it('should pass the option to the default key generator only', async () => { + const app = createApp({ trustProxy: false, keyGenerator: (event) => event.headers.get('x-forwarded-for')! }); + + expect((await send(app, '10.0.0.1')).status).toEqual(200); + expect((await send(app, '10.0.0.2')).status).toEqual(200); + }); +}); From 1868a8acc46d9837b70b0071d815b75488c64634 Mon Sep 17 00:00:00 2001 From: tada5hi Date: Sun, 22 Nov 2026 20:29:34 +0100 Subject: [PATCH 5/5] docs(decorators): note that a subclass accepts its parent's class-level @DConsumes --- packages/docs/src/decorators/controllers.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/docs/src/decorators/controllers.md b/packages/docs/src/decorators/controllers.md index faaaa073..11e8d355 100644 --- a/packages/docs/src/decorators/controllers.md +++ b/packages/docs/src/decorators/controllers.md @@ -131,7 +131,7 @@ declare function DConsumes(...mediaTypes: string[]): (target: any, propertyKey?: <<< @/../examples/decorators/consumes.ts - **Runtime:** a request with a body is checked with routup's [`assertRequestContentType()`](https://routup.dev/api/request-helpers) before its parameters are read and the method runs: a missing or unlisted `content-type` answers `415`, a malformed one `400`. The check ignores parameters (`charset`) and case, and a media type may be a pattern such as `text/*` or `application/*+json`. A request without a body, such as a `GET`, is not checked. -- A route accepts the class-level and the method-level media types together, as the OpenAPI document lists them. +- A route accepts the class-level and the method-level media types together, as the OpenAPI document lists them. A subclass also accepts the class-level media types of its parent, which its document leaves out, since the preset does not inherit class-level decorators ([Inheritance](#inheritance)). - Middlewares run before the check, so an authentication middleware still answers first. - **OpenAPI:** the media types are the request body media types of the operation.