Skip to content

Commit

Permalink
feat(oas): implement security declaration type (#223)
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel A. White authored Apr 18, 2023
1 parent 4d14e4a commit 821f72b
Show file tree
Hide file tree
Showing 29 changed files with 121 additions and 16 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
"dependencies": {
"@stoplight/json": "^3.18.1",
"@stoplight/json-schema-generator": "1.0.2",
"@stoplight/types": "^13.6.0",
"@stoplight/types": "^13.12.0",
"@types/json-schema": "7.0.11",
"@types/swagger-schema-official": "~2.0.22",
"@types/type-is": "^1.6.3",
Expand Down
8 changes: 8 additions & 0 deletions src/oas/__tests__/__snapshots__/operation.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ Array [
},
],
],
"securityDeclarationType": "declared",
"servers": Array [
Object {
"id": "http_server-abc-https://petstore.swagger.io/v2",
Expand Down Expand Up @@ -257,6 +258,7 @@ Array [
},
],
"security": Array [],
"securityDeclarationType": "inheritedFromService",
"servers": Array [
Object {
"id": "http_server-abc-https://petstore.swagger.io/v2",
Expand Down Expand Up @@ -365,6 +367,7 @@ Array [
},
],
"security": Array [],
"securityDeclarationType": "inheritedFromService",
"servers": Array [
Object {
"id": "http_server-abc-https://petstore.swagger.io/v2",
Expand Down Expand Up @@ -409,6 +412,7 @@ Array [
},
],
"security": Array [],
"securityDeclarationType": "inheritedFromService",
"servers": Array [
Object {
"id": "http_server-abc-https://petstore.swagger.io/v2",
Expand Down Expand Up @@ -670,6 +674,7 @@ Array [
},
],
],
"securityDeclarationType": "declared",
"servers": Array [
Object {
"id": "http_server-abc-https://petstore.swagger.io/v2",
Expand Down Expand Up @@ -888,6 +893,7 @@ Array [
},
],
],
"securityDeclarationType": "declared",
"servers": Array [
Object {
"id": "http_server-abc-https://petstore.swagger.io/v2",
Expand Down Expand Up @@ -971,6 +977,7 @@ Array [
},
],
"security": Array [],
"securityDeclarationType": "inheritedFromService",
"servers": Array [
Object {
"id": "http_server-abc-https://petstore.swagger.io/v2",
Expand Down Expand Up @@ -1228,6 +1235,7 @@ Array [
},
],
],
"securityDeclarationType": "declared",
"servers": Array [
Object {
"id": "http_server-abc-https://petstore.swagger.io/v2",
Expand Down
1 change: 1 addition & 0 deletions src/oas/__tests__/operation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ describe('oas operation', () => {
extensions: {},
responses: [],
security: [],
securityDeclarationType: 'inheritedFromService',
servers: [],
summary: '/Update',
tags: [],
Expand Down
3 changes: 3 additions & 0 deletions src/oas/operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type { Fragment, HttpOperationTransformer } from '../types';
import { TransformerContext, TranslateFunction } from '../types';
import { getExtensions } from './accessors';
import { translateToTags } from './tags';
import { translateToSecurityDeclarationType } from './transformers';

const DEFAULT_METHODS = ['get', 'post', 'put', 'delete', 'options', 'head', 'patch', 'trace'];

Expand Down Expand Up @@ -95,5 +96,7 @@ export const transformOasOperation: TranslateFunction<
},
isString,
),

securityDeclarationType: translateToSecurityDeclarationType(operation),
};
};
1 change: 1 addition & 0 deletions src/oas/transformers/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { convertSchema, translateSchemaObject } from './schema/index';
export { translateToSecurityDeclarationType } from './securities';
export { translateLogo } from './translateLogo';
18 changes: 18 additions & 0 deletions src/oas/transformers/securities.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { HttpOperationSecurityDeclarationTypes, IHttpOperation } from '@stoplight/types';
import type { OperationObject } from 'openapi3-ts/src/model/OpenApi';
import type { Operation } from 'swagger-schema-official';

export function translateToSecurityDeclarationType({
security,
}: Partial<Operation | OperationObject>): IHttpOperation['securityDeclarationType'] {
let securityDeclarationType = HttpOperationSecurityDeclarationTypes.InheritedFromService;

if (Array.isArray(security)) {
securityDeclarationType =
security.length === 0
? HttpOperationSecurityDeclarationTypes.None
: HttpOperationSecurityDeclarationTypes.Declared;
}

return securityDeclarationType;
}
2 changes: 2 additions & 0 deletions src/oas2/__tests__/__fixtures__/id/bundled.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ export default {
},
],
security: [],
securityDeclarationType: 'inheritedFromService',
servers: [
{
id: 'http_server-service_abc-http://localhost:3000',
Expand Down Expand Up @@ -369,6 +370,7 @@ export default {
},
],
],
securityDeclarationType: 'declared',
servers: [
{
id: 'http_server-service_abc-http://localhost:3000',
Expand Down
1 change: 1 addition & 0 deletions src/oas2/__tests__/__snapshots__/operation.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ Object {
},
],
],
"securityDeclarationType": "declared",
"servers": Array [],
"summary": "osum",
"tags": Array [],
Expand Down
4 changes: 4 additions & 0 deletions src/oas2/__tests__/bundle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ describe('bundleOas2Service', () => {
},
responses: [],
security: [],
securityDeclarationType: 'inheritedFromService',
servers: [],
tags: [],
},
Expand All @@ -129,6 +130,7 @@ describe('bundleOas2Service', () => {
},
responses: [],
security: [],
securityDeclarationType: 'inheritedFromService',
servers: [],
tags: [],
},
Expand All @@ -151,6 +153,7 @@ describe('bundleOas2Service', () => {
},
responses: [],
security: [],
securityDeclarationType: 'inheritedFromService',
servers: [],
tags: [],
},
Expand Down Expand Up @@ -269,6 +272,7 @@ describe('bundleOas2Service', () => {
},
],
],
securityDeclarationType: 'declared',
servers: [],
tags: [],
},
Expand Down
2 changes: 2 additions & 0 deletions src/oas2/__tests__/operation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ describe('transformOas2Operation', () => {
},
responses: [],
security: [],
securityDeclarationType: 'inheritedFromService',
servers: [],
tags: [],
extensions: {},
Expand Down Expand Up @@ -259,6 +260,7 @@ describe('transformOas2Operation', () => {
},
responses: [],
security: [],
securityDeclarationType: 'inheritedFromService',
servers: [],
tags: [],
extensions: {},
Expand Down
1 change: 1 addition & 0 deletions src/oas3/__tests__/__fixtures__/examples/bundled.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export default {
},
responses: [],
security: [],
securityDeclarationType: 'inheritedFromService',
servers: [
{
id: 'http_server-service_abc-http://localhost:3000',
Expand Down
1 change: 1 addition & 0 deletions src/oas3/__tests__/__fixtures__/examples/legacy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export default [
url: 'http://localhost:3000',
},
],
securityDeclarationType: 'inheritedFromService',
tags: [],
},
];
2 changes: 2 additions & 0 deletions src/oas3/__tests__/__fixtures__/id/bundled.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ export default {
},
],
security: [],
securityDeclarationType: 'inheritedFromService',
servers: [
{
id: 'http_server-service_abc-http://localhost:3000',
Expand Down Expand Up @@ -365,6 +366,7 @@ export default {
},
],
],
securityDeclarationType: 'declared',
servers: [
{
id: 'http_server-service_abc-http://localhost:3000',
Expand Down
2 changes: 2 additions & 0 deletions src/oas3/__tests__/__fixtures__/id/legacy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ export default [
},
],
security: [],
securityDeclarationType: 'inheritedFromService',
extensions: {},
},

Expand Down Expand Up @@ -470,6 +471,7 @@ export default [
},
],
],
securityDeclarationType: 'declared',
extensions: {},
},
];
2 changes: 2 additions & 0 deletions src/oas3/__tests__/__fixtures__/shared-components/bundled.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export default {
},
],
security: [],
securityDeclarationType: 'inheritedFromService',
servers: [],
summary: 'Get a organization repository',
tags: [],
Expand Down Expand Up @@ -55,6 +56,7 @@ export default {
},
],
security: [],
securityDeclarationType: 'inheritedFromService',
servers: [],
summary: 'Create a organization repository',
tags: [],
Expand Down
2 changes: 2 additions & 0 deletions src/oas3/__tests__/__fixtures__/shared-components/legacy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ export default [
},
tags: [],
security: [],
securityDeclarationType: 'inheritedFromService',
extensions: {},
},
/**
Expand Down Expand Up @@ -276,6 +277,7 @@ export default [
},
tags: [],
security: [],
securityDeclarationType: 'inheritedFromService',
extensions: {},
},
];
7 changes: 7 additions & 0 deletions src/oas3/__tests__/__snapshots__/operation.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Object {
},
"responses": Array [],
"security": Array [],
"securityDeclarationType": "inheritedFromService",
"servers": Array [],
"tags": Array [],
},
Expand Down Expand Up @@ -58,6 +59,7 @@ Object {
},
"responses": Array [],
"security": Array [],
"securityDeclarationType": "inheritedFromService",
"servers": Array [],
"tags": Array [],
}
Expand All @@ -80,6 +82,7 @@ Object {
},
"responses": Array [],
"security": Array [],
"securityDeclarationType": "inheritedFromService",
"servers": Array [],
"summary": "summary",
"tags": Array [],
Expand All @@ -103,6 +106,7 @@ Object {
},
"responses": Array [],
"security": Array [],
"securityDeclarationType": "inheritedFromService",
"servers": Array [
Object {
"id": Any<String>,
Expand Down Expand Up @@ -132,6 +136,7 @@ Object {
},
"responses": Array [],
"security": Array [],
"securityDeclarationType": "inheritedFromService",
"servers": Array [
Object {
"id": Any<String>,
Expand Down Expand Up @@ -161,6 +166,7 @@ Object {
},
"responses": Array [],
"security": Array [],
"securityDeclarationType": "inheritedFromService",
"servers": Array [],
"summary": "summary",
"tags": Array [
Expand Down Expand Up @@ -189,6 +195,7 @@ Object {
},
"responses": Array [],
"security": Array [],
"securityDeclarationType": "inheritedFromService",
"servers": Array [
Object {
"id": Any<String>,
Expand Down
3 changes: 3 additions & 0 deletions src/oas3/__tests__/accessors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ describe('getOas3Securities', () => {
'operationScheme',
{
type: 'apiKey',
extensions: {},
},
],
],
Expand All @@ -72,6 +73,7 @@ describe('getOas3Securities', () => {
'specScheme',
{
type: 'apiKey',
extensions: {},
},
],
],
Expand Down Expand Up @@ -102,6 +104,7 @@ describe('getOas3Securities', () => {
'specScheme',
{
type: 'apiKey',
extensions: {},
},
],
],
Expand Down
4 changes: 4 additions & 0 deletions src/oas3/__tests__/bundle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ describe('bundleOas3Service', () => {
],
},
security: [],
securityDeclarationType: 'inheritedFromService',
servers: [],
},
],
Expand Down Expand Up @@ -684,6 +685,7 @@ describe('bundleOas3Service', () => {
},
],
security: [],
securityDeclarationType: 'inheritedFromService',
servers: [],
tags: [],
},
Expand Down Expand Up @@ -743,6 +745,7 @@ describe('bundleOas3Service', () => {
},
responses: [],
security: [],
securityDeclarationType: 'inheritedFromService',
servers: [],
tags: [],
},
Expand Down Expand Up @@ -849,6 +852,7 @@ describe('bundleOas3Service', () => {
},
],
],
securityDeclarationType: 'declared',
servers: [],
tags: [],
},
Expand Down
Loading

0 comments on commit 821f72b

Please sign in to comment.