Skip to content

Commit

Permalink
Fix inconsistency for results associated to skipped blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
satazor authored and andreffvalente committed Aug 5, 2024
1 parent 53ce346 commit 467e559
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/parser/security.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export const parseSecurity = (operation, spec, securityHandlers) => {
const blockHasMissingValues = Object.keys(block).some(name => readSchemeValue(name) == null);

if (blockHasMissingValues) {
report.push({ schemes: {}, status: 'skipped' });
report.push({ ok: false, schemes: {} });
continue;
}

Expand Down
19 changes: 18 additions & 1 deletion src/parser/security.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -364,12 +364,29 @@ describe('parseSecurity()', () => {

const onRequest = parseSecurity(operation, spec, securityHandlers);

expect.assertions(2);
expect.assertions(3);

await onRequest(request);

expect(securityHandlers.ApiKey).not.toHaveBeenCalled();
expect(securityHandlers.OAuth2).toHaveBeenCalledTimes(1);
expect(request[DECORATOR_NAME].securityReport).toMatchInlineSnapshot(`
[
{
"ok": false,
"schemes": {},
},
{
"ok": true,
"schemes": {
"OAuth2": {
"data": "OAuth2 data",
"ok": true,
},
},
},
]
`);
});

it('should validate scopes', async () => {
Expand Down
17 changes: 6 additions & 11 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ declare module 'fastify' {
interface FastifyRequest {
[DECORATOR_NAME]: {
operation: OpenAPI.OpenAPIV3_1.OperationObject,
security: RequestSecurity
securityReport: RequestSecurityReportEntry[]
security: SecurityData
securityReport: SecurityReport
}
}

Expand All @@ -20,11 +20,13 @@ declare module 'fastify' {
}
}

export interface RequestSecurity {
export interface SecurityData {
[key:string]: any
}

export interface RequestSecurityReportEntry {
export type SecurityReport = SecurityReportBlock[]

export interface SecurityReportBlock {
ok: boolean
schemes: {
[key:string]: {
Expand All @@ -35,13 +37,6 @@ export interface RequestSecurityReportEntry {
}
}

export interface RequestSecurityReportResult {
ok: boolean
schemes: {
[key:string]: RequestSecurityReportResult
}
}

export type SecurityHandler = (value: string, request: FastifyRequest) => SecurityHandlerReturn | undefined

export interface SecurityHandlerReturn { data?: any, scopes?: string[] }
Expand Down

0 comments on commit 467e559

Please sign in to comment.