Skip to content
Open
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
11 changes: 11 additions & 0 deletions packages/openapi-merge-cli/src/data.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
import { Swagger } from "atlassian-openapi";

export type PathConfig = {
path: string;
method: Swagger.Method;
}

export type OperationSelection = {
/**
* Only Operatinos that have these tags will be taken from this OpenAPI file. If a single Operation contains
Expand All @@ -10,6 +17,10 @@ export type OperationSelection = {
* an includeTag and an excludeTag then it will be excluded; exclusion takes precedence.
*/
excludeTags?: string[];

includePaths?: PathConfig[];

excludePaths?: PathConfig[];
}

export type PathModification = {
Expand Down
9 changes: 9 additions & 0 deletions packages/openapi-merge/src/data.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { Swagger } from 'atlassian-openapi';

export type PathConfig = {
path: string;
method: Swagger.Method;
}

export type OperationSelection = {
/**
* Only Operatinos that have these tags will be taken from this OpenAPI file. If a single Operation contains
Expand All @@ -12,6 +17,10 @@ export type OperationSelection = {
* an includeTag and an excludeTag then it will be excluded; exclusion takes precedence.
*/
excludeTags?: string[];

includePaths?: PathConfig[];

excludePaths?: PathConfig[];
};

export interface DisputeBase {
Expand Down
81 changes: 78 additions & 3 deletions packages/openapi-merge/src/operation-selection.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
import _ from 'lodash';
import { Swagger } from "atlassian-openapi";
import { OperationSelection } from './data';
import { OperationSelection, PathConfig } from './data';

const allMethods: Swagger.Method[] = [
'get' , 'put' , 'post' , 'delete' , 'options' , 'head' , 'patch' , 'trace'
'get', 'put', 'post', 'delete', 'options', 'head', 'patch', 'trace'
]

function operationContainsAnyTag(operation: Swagger.Operation, tags: string[]): boolean {
return operation.tags !== undefined && operation.tags.some(tag => tags.includes(tag));
}

function operationContainsAnyPath(currentPath: string, method: Swagger.Method, pathConfigs: PathConfig[]): boolean {
return currentPath !== undefined && pathConfigs.some(pathConfig => {
const regex = new RegExp(`^${pathConfig.path}`);
if (regex.test(currentPath) && method.toLowerCase() === pathConfig.method.toLowerCase()) {
return true;
}
return false;
});
}


function dropOperationsThatHaveTags(originalOas: Swagger.SwaggerV3, excludedTags: string[]): Swagger.SwaggerV3 {
if (excludedTags.length === 0) {
return originalOas;
Expand All @@ -36,6 +47,32 @@ function dropOperationsThatHaveTags(originalOas: Swagger.SwaggerV3, excludedTags
return oas;
}

function dropOperationsThatHavePaths(originalOas: Swagger.SwaggerV3, excludedPaths: PathConfig[]): Swagger.SwaggerV3 {
if (excludedPaths.length === 0) {
return originalOas;
}

const oas = _.cloneDeep(originalOas);

for (const path in oas.paths) {
/* eslint-disable-next-line no-prototype-builtins */
if (oas.paths.hasOwnProperty(path)) {
const pathItem = oas.paths[path];

for (let i = 0; i < allMethods.length; i++) {
const method = allMethods[i];
const operation = pathItem[method];

if (operation !== undefined && operationContainsAnyPath(path, method, excludedPaths)) {
delete pathItem[method];
}
}
}
}

return oas;
}

function includeOperationsThatHaveTags(originalOas: Swagger.SwaggerV3, includeTags: string[]): Swagger.SwaggerV3 {
if (includeTags.length === 0) {
return originalOas;
Expand All @@ -62,11 +99,49 @@ function includeOperationsThatHaveTags(originalOas: Swagger.SwaggerV3, includeTa
return oas;
}

function includeOperationsThatHavePaths(originalOas: Swagger.SwaggerV3, includedPaths: PathConfig[]): Swagger.SwaggerV3 {
if (includedPaths.length === 0) {
return originalOas;
}

const oas = _.cloneDeep(originalOas);

for (const path in oas.paths) {
/* eslint-disable-next-line no-prototype-builtins */
if (oas.paths.hasOwnProperty(path)) {
const pathItem = oas.paths[path];

for (let i = 0; i < allMethods.length; i++) {
const method = allMethods[i];
const operation = pathItem[method];

if (operation !== undefined && !operationContainsAnyPath(path,method, includedPaths)) {
delete pathItem[method];
}
}
}
}

return oas;
}


export function runOperationSelection(originalOas: Swagger.SwaggerV3, operationSelection: OperationSelection | undefined): Swagger.SwaggerV3 {
if (operationSelection === undefined) {
return originalOas;
}

return dropOperationsThatHaveTags(includeOperationsThatHaveTags(originalOas, operationSelection.includeTags || []), operationSelection.excludeTags || []);
// dropOperationsThatHaveTags(includeOperationsThatHaveTags(originalOas, operationSelection.includeTags || []), operationSelection.excludeTags || [])
return dropOperationsThatHavePaths(
includeOperationsThatHavePaths(
dropOperationsThatHaveTags(
includeOperationsThatHaveTags(
originalOas, operationSelection.includeTags || []
),
operationSelection.excludeTags || []
),
operationSelection.includePaths || []
),
operationSelection.excludePaths || []
)
}