From edf10ac2c4a6e15fc30aff6af84750b541366603 Mon Sep 17 00:00:00 2001 From: BatLeDev Date: Mon, 9 Nov 2026 09:45:43 +0100 Subject: [PATCH] fix: ignore non-apiKey security schemes in operation form Operations declaring an oauth2 (or http / openIdConnect) security scheme crashed the operation page: the form builder indexed schema.properties by the scheme's `in`, which only apiKey schemes have. Only apiKey schemes in header/query now produce a field, parameters with an unknown location are skipped, and every scheme of a combined security requirement is resolved. Co-Authored-By: Claude Opus 5.5 --- tests/transform.unit.spec.ts | 54 ++++++++++++++++++++++++++++++++++++ ui/src/utils/transform.ts | 32 ++++++++++----------- 2 files changed, 68 insertions(+), 18 deletions(-) create mode 100644 tests/transform.unit.spec.ts diff --git a/tests/transform.unit.spec.ts b/tests/transform.unit.spec.ts new file mode 100644 index 0000000..1e9eb0e --- /dev/null +++ b/tests/transform.unit.spec.ts @@ -0,0 +1,54 @@ +import { test, expect } from '@playwright/test' +import { getVJSFSchema, initTransformer } from '../ui/src/utils/transform.ts' + +const specs = (security: any[], operationSecurity?: any[]) => ({ + openapi: '3.1.0', + info: { title: 'test', version: '1' }, + components: { + securitySchemes: { + apiKey: { type: 'apiKey', in: 'header', name: 'x-apiKey' }, + queryKey: { type: 'apiKey', in: 'query', name: 'key' }, + sdCookie: { type: 'apiKey', in: 'cookie', name: 'id_token' }, + oauth2: { type: 'oauth2', flows: { clientCredentials: { tokenUrl: 'https://example.com/token', scopes: { read: 'Read' } } } }, + bearer: { type: 'http', scheme: 'bearer' }, + oidc: { type: 'openIdConnect', openIdConnectUrl: 'https://example.com/.well-known/openid-configuration' } + } + }, + security, + paths: {} +}) as any + +test.describe('getVJSFSchema — security schemes', () => { + test('ignores schemes without a header/query location instead of crashing', () => { + initTransformer(specs([{ apiKey: [] }, { sdCookie: [] }])) + const schema = getVJSFSchema({ + security: [{ oauth2: ['read'] }, { bearer: [] }, { oidc: [] }, { sdCookie: [] }] + } as any, []) + expect(Object.keys(schema.properties.header.properties)).toEqual(['x-apiKey']) + expect(schema.properties.query).toBeUndefined() + }) + + test('reads every scheme of a combined requirement', () => { + initTransformer(specs([])) + const schema = getVJSFSchema({ security: [{ apiKey: [], queryKey: [] }, {}] } as any, []) + expect(Object.keys(schema.properties.header.properties)).toEqual(['x-apiKey']) + expect(Object.keys(schema.properties.query.properties)).toEqual(['key']) + }) +}) + +test.describe('getVJSFSchema — parameters', () => { + test('skips parameters with a cookie, body or unknown location', () => { + initTransformer(specs([])) + const schema = getVJSFSchema({ + parameters: [ + { name: 'size', in: 'query', schema: { type: 'integer' } }, + { name: 'session', in: 'cookie', schema: { type: 'string' } }, + { name: 'payload', in: 'body', schema: { type: 'object' } }, + { name: 'weird', in: 'nowhere', schema: { type: 'string' } } + ] + } as any, []) + expect(Object.keys(schema.properties.query.properties)).toEqual(['size']) + expect(schema.properties.header).toBeUndefined() + expect(schema.properties.path).toBeUndefined() + }) +}) diff --git a/ui/src/utils/transform.ts b/ui/src/utils/transform.ts index 66d45bc..38a4168 100644 --- a/ui/src/utils/transform.ts +++ b/ui/src/utils/transform.ts @@ -1,5 +1,5 @@ import type { Components, OpenAPISpecs, Operation, Parameter as ParameterSpec } from '#api/types' -import clone from '@data-fair/lib-utils/clone' +import clone from '@data-fair/lib-utils/clone.js' let globalSchema: OpenAPISpecs | undefined @@ -28,7 +28,8 @@ export const getVJSFSchema = (operation: Operation, pathItemParameters: Paramete operation.security ) for (const [, sec] of Object.entries(securities)) { - if (sec.in === 'cookie') continue + // Only apiKey schemes map to a named header/query field (oauth2, http… have no `in`) + if (sec?.type !== 'apiKey' || !sec.name || (sec.in !== 'header' && sec.in !== 'query')) continue schema.properties[sec.in].properties[sec.name] = { type: 'string', description: @@ -39,9 +40,8 @@ export const getVJSFSchema = (operation: Operation, pathItemParameters: Paramete // Transform parameters const parameters = resolveParameters(pathItemParameters, operation.parameters as Parameter[]) for (const param of parameters) { - if (param.in === 'cookie') continue - // @ts-ignore Not allowed in the OpenAPI spec, but this behavior should not be blocking. - if (param.in === 'body') continue + // Skip cookie, swagger 2 'body' and any unknown location + if (param.in !== 'header' && param.in !== 'path' && param.in !== 'query') continue const paramSchema = param.schema as Record if (!paramSchema) continue // Parameters should have a content key or schema key. // TODO: Content isn't supported yet @@ -185,21 +185,17 @@ const resolveSecurities = ( ) => { if (!securitySchemes) return {} - const globalS = globalSecurities?.reduce((acc, sec) => { - const key = Object.keys(sec)[0] - if (securitySchemes[key]) { - acc[key] = securitySchemes[key] - } - return acc - }, {} as Record) || {} - - const operationS = operationSecurities?.reduce((acc, sec) => { - const key = Object.keys(sec)[0] - if (securitySchemes[key]) { - acc[key] = securitySchemes[key] + const pickSchemes = (requirements: OpenAPISpecs['security']) => { + const acc: Record = {} + for (const requirement of requirements ?? []) { + for (const key of Object.keys(requirement ?? {})) { + if (securitySchemes[key]) acc[key] = securitySchemes[key] + } } return acc - }, {} as Record) || {} + } + const globalS = pickSchemes(globalSecurities) + const operationS = pickSchemes(operationSecurities) return { ...globalS, ...operationS } as { [k: string]: {