|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * #23083 — the OpenAPI reader re-parses a SERVED `api` item, so it strips the |
| 5 | + * read-time decorations before its closed parse. |
| 6 | + * |
| 7 | + * `GET /openapi.json` enumerates `api` items through `protocol.getMetaItems`, |
| 8 | + * whose served audience exits through `decorateMetadataItems` and stamps |
| 9 | + * `_diagnostics` on every item (`api` has a registered schema). Where no |
| 10 | + * endpoint matcher is reachable — a composition whose `metadata` slot is the |
| 11 | + * kernel's in-memory fallback, `@objectstack/verify`'s `bootStack` among them — |
| 12 | + * the handler hands those served items to `selectDocumentableEndpoints` as they |
| 13 | + * are, and the closed `ApiEndpointSchema` refused the platform's own badge by |
| 14 | + * name, `unrecognized_keys: ["_diagnostics"]`: every VALID declaration was |
| 15 | + * omitted from the document and logged at `error` as a declaration to fix. |
| 16 | + * |
| 17 | + * The fixtures are the platform's own output: `decorateMetadataItems` from |
| 18 | + * `@objectstack/metadata-protocol` is the producer the served read calls, so a |
| 19 | + * decoration it stamps is in the fixture without anyone editing this file. |
| 20 | + * |
| 21 | + * Controls, each the other half of a pin above it: |
| 22 | + * - a truly invalid item is still omitted and logged — the strip launders |
| 23 | + * nothing, and the refusal names the item's real defect; |
| 24 | + * - the ADR-0010 envelope (`_packageId`, `_provenance`) parses exactly as it |
| 25 | + * did — the closed schema allowlists it, so it is not a decoration; |
| 26 | + * - an underscore key that is NOT a declared read decoration is still refused: |
| 27 | + * the strip is `METADATA_READ_DECORATIONS`, never a blanket `_` prefix. |
| 28 | + */ |
| 29 | + |
| 30 | +import { describe, it, expect, vi, beforeAll, afterAll, afterEach } from 'vitest'; |
| 31 | +import { ApiEndpointSchema } from '@objectstack/spec/api'; |
| 32 | +import { decorateMetadataItems } from '@objectstack/metadata-protocol'; |
| 33 | +import { enrichOpenApiWithEndpoints, selectDocumentableEndpoints } from './openapi-endpoints'; |
| 34 | +import { RestServer } from './rest-server'; |
| 35 | + |
| 36 | +// [#17865] The handler-level pin below observes the REST fault log, so this |
| 37 | +// file declares the level it asserts against instead of inheriting the suite's |
| 38 | +// quiet one. 'info' is the SHIPPED default — what a real caller gets. |
| 39 | +beforeAll(() => { vi.stubEnv('OS_REST_LOG', 'info'); }); |
| 40 | +afterAll(() => { vi.unstubAllEnvs(); }); |
| 41 | +afterEach(() => { vi.restoreAllMocks(); }); |
| 42 | + |
| 43 | +// --------------------------------------------------------------------------- |
| 44 | +// The showcase's two declarations, restated (not imported — an example app is |
| 45 | +// not this package's dependency) |
| 46 | +// --------------------------------------------------------------------------- |
| 47 | + |
| 48 | +const TASK_FEED = { |
| 49 | + name: 'showcase_task_feed', |
| 50 | + path: '/api/v1/apps/showcase/tasks', |
| 51 | + method: 'GET', |
| 52 | + summary: 'Task feed', |
| 53 | + type: 'object_operation', |
| 54 | + objectParams: { object: 'showcase_task', operation: 'find' }, |
| 55 | + authRequired: true, |
| 56 | + cacheTtlSeconds: 30, |
| 57 | +}; |
| 58 | + |
| 59 | +const INQUIRY_PURGE = { |
| 60 | + name: 'showcase_inquiry_purge_api', |
| 61 | + path: '/api/v1/apps/showcase/inquiries/purge', |
| 62 | + method: 'POST', |
| 63 | + summary: 'Purge closed inquiries', |
| 64 | + type: 'flow', |
| 65 | + target: 'showcase_inquiry_purge', |
| 66 | + authRequired: true, |
| 67 | +}; |
| 68 | + |
| 69 | +/** What the served read hands a reader: the producer's own decoration. */ |
| 70 | +function servedRead(items: Array<Record<string, unknown>>): Array<Record<string, unknown>> { |
| 71 | + return decorateMetadataItems('api', items.map((item) => ({ ...item }))); |
| 72 | +} |
| 73 | + |
| 74 | +function collectingLogger() { |
| 75 | + return { error: vi.fn() }; |
| 76 | +} |
| 77 | + |
| 78 | +function baseDoc() { |
| 79 | + return { |
| 80 | + openapi: '3.1.0', |
| 81 | + info: { title: 'ObjectStack API', version: '17.0.0' }, |
| 82 | + paths: { '/api/meta': { get: { operationId: 'getMeta' } } }, |
| 83 | + components: { schemas: {}, securitySchemes: { bearerAuth: { type: 'http', scheme: 'bearer' } } }, |
| 84 | + security: [{ bearerAuth: [] }], |
| 85 | + }; |
| 86 | +} |
| 87 | + |
| 88 | +// --------------------------------------------------------------------------- |
| 89 | +// Pins |
| 90 | +// --------------------------------------------------------------------------- |
| 91 | + |
| 92 | +describe('a valid declaration is documented however the read decorated it (#23083)', () => { |
| 93 | + it('documents both items exactly as the served read decorates them, and logs nothing', () => { |
| 94 | + const served = servedRead([TASK_FEED, INQUIRY_PURGE]); |
| 95 | + // Precondition: the fixture really carries the decoration, and the producer |
| 96 | + // itself calls both items valid — so a refusal here can only be the reader's. |
| 97 | + for (const item of served) expect(item._diagnostics).toEqual({ valid: true }); |
| 98 | + |
| 99 | + const logger = collectingLogger(); |
| 100 | + const kept = selectDocumentableEndpoints(served, logger); |
| 101 | + |
| 102 | + expect(kept.map((e) => e.name).sort()).toEqual(['showcase_inquiry_purge_api', 'showcase_task_feed']); |
| 103 | + expect(logger.error).not.toHaveBeenCalled(); |
| 104 | + for (const endpoint of kept) expect(endpoint).not.toHaveProperty('_diagnostics'); |
| 105 | + }); |
| 106 | + |
| 107 | + it('documents an item carrying the draft badge, and logs nothing', () => { |
| 108 | + const logger = collectingLogger(); |
| 109 | + const kept = selectDocumentableEndpoints([{ ...TASK_FEED, _draft: true }], logger); |
| 110 | + |
| 111 | + expect(kept.map((e) => e.name)).toEqual(['showcase_task_feed']); |
| 112 | + expect(logger.error).not.toHaveBeenCalled(); |
| 113 | + expect(kept[0]).not.toHaveProperty('_draft'); |
| 114 | + }); |
| 115 | + |
| 116 | + it('folds the decorated declarations into the document as path entries', () => { |
| 117 | + const logger = collectingLogger(); |
| 118 | + const out = enrichOpenApiWithEndpoints(baseDoc(), servedRead([TASK_FEED, INQUIRY_PURGE]), logger) as any; |
| 119 | + |
| 120 | + expect(out.paths['/api/v1/apps/showcase/tasks'].get.operationId).toBe('showcase_task_feed'); |
| 121 | + expect(out.paths['/api/v1/apps/showcase/inquiries/purge'].post.operationId).toBe('showcase_inquiry_purge_api'); |
| 122 | + expect(logger.error).not.toHaveBeenCalled(); |
| 123 | + }); |
| 124 | +}); |
| 125 | + |
| 126 | +describe('controls — the strip removes the declared decorations and nothing else', () => { |
| 127 | + it('still omits and logs a truly invalid item, naming its real defect', () => { |
| 128 | + const served = servedRead([{ ...TASK_FEED, name: 'bad_type', type: 'nope' }]); |
| 129 | + // The producer agrees it is invalid — the decoration rides on a bad item. |
| 130 | + expect((served[0]._diagnostics as { valid: boolean }).valid).toBe(false); |
| 131 | + |
| 132 | + const logger = collectingLogger(); |
| 133 | + const kept = selectDocumentableEndpoints(served, logger); |
| 134 | + |
| 135 | + expect(kept).toEqual([]); |
| 136 | + expect(logger.error).toHaveBeenCalledTimes(1); |
| 137 | + expect(logger.error.mock.calls[0][0]).toContain("'bad_type'"); |
| 138 | + const { issues } = logger.error.mock.calls[0][1] as { issues: Array<{ code: string; path: unknown[]; keys?: string[] }> }; |
| 139 | + // The refusal is about the item, not about our own badge. |
| 140 | + expect(issues.some((i) => i.path[0] === 'type')).toBe(true); |
| 141 | + expect(issues.some((i) => i.code === 'unrecognized_keys')).toBe(false); |
| 142 | + }); |
| 143 | + |
| 144 | + it('parses the ADR-0010 envelope exactly as before, decorated or not', () => { |
| 145 | + const enveloped = { ...TASK_FEED, _packageId: 'com.example.showcase', _provenance: 'package' }; |
| 146 | + const expected = ApiEndpointSchema.parse(enveloped); |
| 147 | + |
| 148 | + const logger = collectingLogger(); |
| 149 | + const [plain] = selectDocumentableEndpoints([enveloped], logger); |
| 150 | + const [decorated] = selectDocumentableEndpoints(servedRead([enveloped]), logger); |
| 151 | + |
| 152 | + expect(plain).toEqual(expected); |
| 153 | + expect(decorated).toEqual(expected); |
| 154 | + expect(decorated._packageId).toBe('com.example.showcase'); |
| 155 | + expect(logger.error).not.toHaveBeenCalled(); |
| 156 | + }); |
| 157 | + |
| 158 | + it('still refuses an underscore key that is not a declared read decoration', () => { |
| 159 | + const logger = collectingLogger(); |
| 160 | + const kept = selectDocumentableEndpoints([{ ...TASK_FEED, _unexpected: true }], logger); |
| 161 | + |
| 162 | + expect(kept).toEqual([]); |
| 163 | + expect(logger.error).toHaveBeenCalledTimes(1); |
| 164 | + const { issues } = logger.error.mock.calls[0][1] as { issues: Array<{ code: string; keys?: string[] }> }; |
| 165 | + expect(issues.find((i) => i.code === 'unrecognized_keys')?.keys).toEqual(['_unexpected']); |
| 166 | + }); |
| 167 | +}); |
| 168 | + |
| 169 | +// --------------------------------------------------------------------------- |
| 170 | +// The production caller, on a composition with no endpoint matcher |
| 171 | +// --------------------------------------------------------------------------- |
| 172 | + |
| 173 | +function createMockServer() { |
| 174 | + return { |
| 175 | + get: vi.fn(), post: vi.fn(), put: vi.fn(), delete: vi.fn(), patch: vi.fn(), use: vi.fn(), |
| 176 | + listen: vi.fn().mockResolvedValue(undefined), close: vi.fn().mockResolvedValue(undefined), |
| 177 | + }; |
| 178 | +} |
| 179 | + |
| 180 | +function makeRes() { |
| 181 | + const res: any = { statusCode: 200, body: undefined }; |
| 182 | + res.status = vi.fn((c: number) => { res.statusCode = c; return res; }); |
| 183 | + res.json = vi.fn((b: any) => { res.body = b; return res; }); |
| 184 | + res.header = vi.fn(() => res); res.setHeader = vi.fn(); res.write = vi.fn(); res.end = vi.fn(); res.send = vi.fn(); |
| 185 | + return res; |
| 186 | +} |
| 187 | + |
| 188 | +/** |
| 189 | + * The REST server with NO metadata service wired — the shape of a composition |
| 190 | + * whose `metadata` slot cannot answer `matchEndpoint` — over a protocol whose |
| 191 | + * `getMetaItems` answers what the served read answers. |
| 192 | + */ |
| 193 | +function mountWithoutMatcher(served: Array<Record<string, unknown>>) { |
| 194 | + const protocol: any = { |
| 195 | + getDiscovery: vi.fn().mockResolvedValue({ |
| 196 | + version: 'v0', routes: { data: '', metadata: '', ui: '', auth: '/auth' }, |
| 197 | + }), |
| 198 | + getMetaTypes: vi.fn().mockResolvedValue([]), |
| 199 | + getMetaItems: vi.fn(async ({ type }: any) => { |
| 200 | + const t = String(type ?? ''); |
| 201 | + if (t === 'api' || t === 'apis') return { type: 'api', items: served }; |
| 202 | + return { type: t, items: [] }; |
| 203 | + }), |
| 204 | + getMetaItem: vi.fn().mockResolvedValue({}), |
| 205 | + findData: vi.fn().mockResolvedValue([]), |
| 206 | + }; |
| 207 | + const rest = new RestServer( |
| 208 | + createMockServer() as any, |
| 209 | + protocol, |
| 210 | + { api: { requireAuth: false, version: 'v1' } } as any, |
| 211 | + ); |
| 212 | + // The description document denies anonymous callers; an identity is a |
| 213 | + // precondition of reaching the handler, not the subject here. |
| 214 | + (rest as any).resolveExecCtx = async () => ({ userId: 'u1' }); |
| 215 | + rest.registerRoutes(); |
| 216 | + (rest as any).registerOpenApiEndpoints('/api/v1'); |
| 217 | + return rest; |
| 218 | +} |
| 219 | + |
| 220 | +async function getOpenApi(rest: any) { |
| 221 | + const entry = rest.routeManager.get('GET', '/api/v1/openapi.json'); |
| 222 | + if (!entry) throw new Error('openapi.json route not registered'); |
| 223 | + const res = makeRes(); |
| 224 | + await entry.handler({ method: 'GET', headers: { host: 'example.test' }, params: {}, query: {}, path: '/api/v1/openapi.json' }, res); |
| 225 | + return res; |
| 226 | +} |
| 227 | + |
| 228 | +describe('GET /openapi.json with no endpoint matcher reachable', () => { |
| 229 | + it('lists both declared endpoints, and no error line names either of them', async () => { |
| 230 | + const errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {}); |
| 231 | + const rest = mountWithoutMatcher(servedRead([TASK_FEED, INQUIRY_PURGE])); |
| 232 | + |
| 233 | + const res = await getOpenApi(rest); |
| 234 | + |
| 235 | + expect(res.statusCode).toBe(200); |
| 236 | + const declared = Object.keys(res.body?.paths ?? {}).filter((p) => p.startsWith('/api/v1/apps/')).sort(); |
| 237 | + expect(declared).toEqual(['/api/v1/apps/showcase/inquiries/purge', '/api/v1/apps/showcase/tasks']); |
| 238 | + |
| 239 | + const lines = errorSpy.mock.calls.map((args) => args.map(String).join(' ')); |
| 240 | + expect(lines.filter((l) => l.includes('showcase_task_feed') || l.includes('showcase_inquiry_purge_api'))).toEqual([]); |
| 241 | + }, 60_000); |
| 242 | +}); |
0 commit comments