Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions tests/transform.unit.spec.ts
Original file line number Diff line number Diff line change
@@ -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()
})
})
32 changes: 14 additions & 18 deletions ui/src/utils/transform.ts
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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:
Expand All @@ -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<string, any>
if (!paramSchema) continue // Parameters should have a content key or schema key. // TODO: Content isn't supported yet

Expand Down Expand Up @@ -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<string, any>) || {}

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<string, any> = {}
for (const requirement of requirements ?? []) {
for (const key of Object.keys(requirement ?? {})) {
if (securitySchemes[key]) acc[key] = securitySchemes[key]
}
}
return acc
}, {} as Record<string, any>) || {}
}
const globalS = pickSchemes(globalSecurities)
const operationS = pickSchemes(operationSecurities)

return { ...globalS, ...operationS } as {
[k: string]: {
Expand Down
Loading