mirror of
https://github.com/Kong/insomnia.git
synced 2026-10-27 00:59:18 -04:00
Transform OAS -> Kong DC JSON rather than YAML (#4129)
* output JSON * add test for declarative config * feedback Co-authored-by: gatzjames <jamesgatzos@gmail.com>
This commit is contained in:
1 parent
dddacb9eaa
commit
e0ff79a2a6
3 files changed
+53
-13
No files matched your search
@@ -1,4 +1,4 @@
|
||||
import { generate as _generate, generateFromString as _generateFromString, KongForKubernetesResult } from 'openapi-2-kong';
|
||||
import { DeclarativeConfigResult, generate as _generate, generateFromString as _generateFromString, KongForKubernetesResult } from 'openapi-2-kong';
|
||||
import os from 'os';
|
||||
import path from 'path';
|
||||
|
||||
@@ -23,6 +23,19 @@ const mockConversionResult: KongForKubernetesResult = {
|
||||
warnings: [],
|
||||
};
|
||||
|
||||
const mockDeclarativeConversionResult: DeclarativeConfigResult = {
|
||||
documents: [
|
||||
{
|
||||
'_format_version': '1.1',
|
||||
services: [],
|
||||
upstreams: [],
|
||||
},
|
||||
],
|
||||
type: 'kong-declarative-config',
|
||||
label: '',
|
||||
warnings: [],
|
||||
};
|
||||
|
||||
describe('generateConfig()', () => {
|
||||
beforeAll(() => {
|
||||
globalBeforeAll();
|
||||
@@ -120,9 +133,11 @@ describe('generateConfig()', () => {
|
||||
expect(generate).toHaveBeenCalledWith(
|
||||
path.normalize('test/dir/file.yaml'),
|
||||
conversionTypeMap.kubernetes,
|
||||
undefined,
|
||||
undefined
|
||||
);
|
||||
expect(logger.__getLogs().log).toEqual([`Configuration generated to "${outputPath}".`]);
|
||||
expect(logger.__getLogs().log).toEqual([
|
||||
`Configuration generated to "${outputPath}".`,
|
||||
]);
|
||||
});
|
||||
|
||||
it('should generate documents using absolute path', async () => {
|
||||
@@ -138,8 +153,14 @@ describe('generateConfig()', () => {
|
||||
expect(result).toBe(true);
|
||||
|
||||
// Read from workingDir
|
||||
expect(generate).toHaveBeenCalledWith(absolutePath, conversionTypeMap.kubernetes, undefined);
|
||||
expect(logger.__getLogs().log).toEqual([`Configuration generated to "${outputPath}".`]);
|
||||
expect(generate).toHaveBeenCalledWith(
|
||||
absolutePath,
|
||||
conversionTypeMap.kubernetes,
|
||||
undefined
|
||||
);
|
||||
expect(logger.__getLogs().log).toEqual([
|
||||
`Configuration generated to "${outputPath}".`,
|
||||
]);
|
||||
});
|
||||
|
||||
it('should throw InsoError if there is an error thrown by openapi-2-kong', async () => {
|
||||
@@ -160,4 +181,17 @@ describe('generateConfig()', () => {
|
||||
'Could not find a valid specification to generate configuration.',
|
||||
]);
|
||||
});
|
||||
|
||||
it('should generate declarative config', async () => {
|
||||
generate.mockResolvedValue(mockDeclarativeConversionResult);
|
||||
|
||||
const result = await generateConfig(
|
||||
filePath,
|
||||
{ type: 'declarative' }
|
||||
);
|
||||
expect(result).toBe(true);
|
||||
expect(logger.__getLogs().log).toEqual([
|
||||
'{"_format_version":"1.1","services":[],"upstreams":[]}',
|
||||
]);
|
||||
});
|
||||
});
|
||||
@@ -89,9 +89,17 @@ export const generateConfig = async (
|
||||
return false;
|
||||
}
|
||||
|
||||
const yamlDocs = result.documents.map((document: DeclarativeConfig | K8sManifest) => YAML.stringify(document));
|
||||
// Join the YAML docs with "---" and strip any extra newlines surrounding them
|
||||
const document = yamlDocs.join('\n---\n').replace(/\n+---\n+/g, '\n---\n');
|
||||
const isListOfDeclarativeConfigs = (docs: DeclarativeConfig[] | K8sManifest[]) :docs is DeclarativeConfig[] => typeof docs?.[0] !== 'string' && '_format_version' in docs?.[0];
|
||||
|
||||
let document = '';
|
||||
if (isListOfDeclarativeConfigs(result.documents)){
|
||||
// We know for certain the result.documents has only one entry for declarative config: packages/openapi-2-kong/src/declarative-config/generate.ts#L20
|
||||
document = JSON.stringify(result.documents?.[0]);
|
||||
} else {
|
||||
const yamlDocs = result.documents.map((document: K8sManifest) => YAML.stringify(document));
|
||||
// Join the YAML docs with "---" and strip any extra newlines surrounding them
|
||||
document = yamlDocs.join('\n---\n').replace(/\n+---\n+/g, '\n---\n');
|
||||
}
|
||||
|
||||
if (output) {
|
||||
const outputPath = await writeFileWithCliOptions(output, document, workingDir);
|
||||
|
||||
@@ -24,11 +24,9 @@ module.exports = {
|
||||
error: err.message,
|
||||
};
|
||||
}
|
||||
|
||||
const yamlDocs = result.documents.map(d => YAML.stringify(d));
|
||||
|
||||
// Join the YAML docs with "---" and strip any extra newlines surrounding them
|
||||
const document = yamlDocs.join('\n---\n').replace(/\n+---\n+/g, '\n---\n');
|
||||
|
||||
// We know for certain the result.documents has only one entry for declarative config: packages/openapi-2-kong/src/declarative-config/generate.ts#L20
|
||||
const document = JSON.stringify(result.documents?.[0], null, '\t');
|
||||
|
||||
return {
|
||||
document,
|
||||
|
||||
Reference in new issue
Block a user