Skip to content

Commit

Permalink
improve version OAS conflict error message
Browse files Browse the repository at this point in the history
  • Loading branch information
maximpn committed May 1, 2024
1 parent 473f05c commit d2584d2
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,30 +66,36 @@ function extractOasVersion(bundledDocuments: BundledDocument[]): string {
throw new Error('Empty bundled document list');
}

let version = bundledDocuments[0].document.openapi as string;

// Automatically promote to the recent OAS 3.0 version which is 3.0.3
// 3.0.3 is the version used in the specification https://swagger.io/specification/v3/
if (version < '3.0.3') {
version = '3.0.3';
}
const firstBundledDocument = bundledDocuments[0];

for (let i = 1; i < bundledDocuments.length; ++i) {
if (!areOasVersionsEqual(bundledDocuments[i].document.openapi as string, version)) {
if (
!areOasVersionsEqual(
bundledDocuments[i].document.openapi as string,
firstBundledDocument.document.openapi as string
)
) {
throw new Error(
`OpenAPI specs must have the same OpenAPI versions, conflicting versions are ${chalk.blue(
`OpenAPI specs must have the same OpenAPI versions, encountered ${chalk.blue(
bundledDocuments[i].document.openapi
)} and ${chalk.blue(version)}`
)} at ${chalk.bold(bundledDocuments[i].absolutePath)} does not match ${chalk.blue(
firstBundledDocument.document.openapi
)} at ${chalk.bold(firstBundledDocument.absolutePath)}`
);
}
}

return version;
const version = firstBundledDocument.document.openapi as string;

// Automatically promote to the recent OAS 3.0 version which is 3.0.3
// 3.0.3 is the version used in the specification https://swagger.io/specification/v3/
return version < '3.0.3' ? '3.0.3' : version;
}

/**
* Tells if versions are equal by comparing only major and minor OAS version parts
*/
function areOasVersionsEqual(versionA: string, versionB: string): boolean {
// versionA.substring(0, 3) results in `3.0` or `3.1`
return versionA.substring(0, 3) === versionB.substring(0, 3);
}
26 changes: 22 additions & 4 deletions packages/kbn-openapi-bundler/src/openapi_bundler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,30 @@
* Side Public License, v 1.
*/

import chalk from 'chalk';
import { existsSync } from 'fs';
import { join } from 'path';
import { bundle } from './openapi_bundler';
import { readYamlDocument } from './utils/read_yaml_document';

jest.mock('chalk', () => {
const identity = jest.fn((x) => x);

return {
__esModule: true,
default: {
magentaBright: identity,
yellow: identity,
red: identity,
green: identity,
dim: identity,
blue: identity,
bold: identity,
underline: identity,
magenta: identity,
},
};
});

const ROOT_PATH = join(__dirname, '__test__');
const BUNDLED_FILE_PATH_TEMPLATE = join(__dirname, 'target', 'bundled-{version}.yaml');
const DEFAULT_BUNDLED_FILE_PATH = BUNDLED_FILE_PATH_TEMPLATE.replace('{version}', '2023-10-31');
Expand Down Expand Up @@ -40,9 +58,9 @@ describe('OpenAPI Bundler', () => {
it('DOES NOT bundle spec with different OpenAPI versions', async () => {
await expectBundlingError(
'different_openapi_versions',
`OpenAPI specs must have the same OpenAPI versions, conflicting versions are ${chalk.blue(
'3.1.0'
)} and ${chalk.blue('3.0.3')}`
new RegExp(
`^OpenAPI specs must have the same OpenAPI versions, encountered 3\.1\.0 at .* does not match 3\.0\.3 at .*`
)
);
});

Expand Down

0 comments on commit d2584d2

Please sign in to comment.