Skip to content

Commit

Permalink
feat(oas3): return security scopes for types other than oauth2 (#268)
Browse files Browse the repository at this point in the history
  • Loading branch information
darekplawecki authored Jun 18, 2024
1 parent 778247e commit be416bf
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
30 changes: 30 additions & 0 deletions src/oas3/__tests__/accessors.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { DeepPartial } from '@stoplight/types';
import { SecuritySchemeType } from 'openapi3-ts';

import { setSkipHashing } from '../../hash';
import { getSecurities as _getSecurities, OperationSecurities } from '../accessors';
Expand Down Expand Up @@ -111,6 +112,35 @@ describe('getOas3Securities', () => {
]);
});

it.each<SecuritySchemeType>(['http', 'apiKey', 'openIdConnect'])(
'given global securities and matching operation scheme with scopes should return scopes as extensions for security scheme type: %s',
type => {
expect(
getSecurities({
security: [{ operationScheme: ['image:read'] }],
components: {
securitySchemes: {
operationScheme: {
type,
},
},
},
}),
).toStrictEqual([
[
[
'operationScheme',
{
type,
['x-scopes']: ['image:read'],
extensions: { ['x-scopes']: ['image:read'] },
},
],
],
]);
},
);

it('given global securities and matching spec and invalid operation scheme should return empty array', () => {
expect(
getSecurities(
Expand Down
5 changes: 4 additions & 1 deletion src/oas3/accessors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,14 @@ export function getSecurities(
];
}

const extensions = scopes?.length ? { ['x-scopes']: scopes } : {};

return [
opScheme,
{
...definition,
extensions: getExtensions(definition),
...extensions,
extensions: getExtensions({ ...definition, ...extensions }),
},
];
})
Expand Down

0 comments on commit be416bf

Please sign in to comment.