diff --git a/packages/cli/src/commands/split/__tests__/fixtures/samples.json b/packages/cli/src/commands/split/__tests__/fixtures/samples.json new file mode 100644 index 00000000..50712ab3 --- /dev/null +++ b/packages/cli/src/commands/split/__tests__/fixtures/samples.json @@ -0,0 +1,61 @@ +{ + "openapi": "3.0.1", + "info": { + "title": "TEST", + "description": "TEST", + "version": "v1", + "license": { + "name": "Apache 2.0", + "url": "http://www.apache.org/licenses/LICENSE-2.0.html" + } + }, + "servers": [ + { + "url": "http://petstore.swagger.io/v1" + } + ], + "components": { + "schemas": { + "Test": { + "nullable": true + } + } + }, + "paths": { + "/test": { + "get": { + "summary": "test", + "operationId": "test", + "responses": { + "202": { + "description": "Test", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Test" + } + } + } + }, + "400": { + "description": "An error response" + } + }, + "x-codeSamples": [ + { + "lang": "C#", + "source": "PetStore.v1.Pet pet = new PetStore.v1.Pet();" + }, + { + "lang": "C/AL", + "source": "PetStore.v1.Pet pet = new PetStore.v1.Pet();" + }, + { + "lang": "Visual Basic", + "source": "PetStore.v1.Pet pet = new PetStore.v1.Pet();" + } + ] + } + } + } +} diff --git a/packages/cli/src/commands/split/__tests__/index.test.ts b/packages/cli/src/commands/split/__tests__/index.test.ts index 1426500d..77cdc4b2 100644 --- a/packages/cli/src/commands/split/__tests__/index.test.ts +++ b/packages/cli/src/commands/split/__tests__/index.test.ts @@ -6,6 +6,8 @@ import { } from '../types'; import { blue, green } from 'colorette'; +const utils = require('../../../utils'); + jest.mock('../../../utils', () => ({ ...jest.requireActual('../../../utils'), writeYaml: jest.fn(), @@ -46,7 +48,6 @@ describe('#split', () => { it('should use the correct separator', async () => { const filePath = "packages/cli/src/commands/split/__tests__/fixtures/spec.json"; - const utils = require('../../../utils'); jest.spyOn(utils, 'pathToFilename').mockImplementation(() => 'newFilePath'); await handleSplit ( @@ -93,4 +94,24 @@ describe('#split', () => { expect(openapiCore.slash).toHaveBeenCalledWith('webhooks/test.yaml'); expect(path.relative).toHaveBeenCalledWith('test', 'test/webhooks/test.yaml'); }); + + it('should create correct folder name for code samples', async () => { + const openapi = require("./fixtures/samples.json"); + + jest.spyOn(utils, 'escapeLanguageName'); + iteratePathItems(openapi.paths, openapiDir, path.join(openapiDir, 'paths'), componentsFiles, '_'); + + expect(utils.escapeLanguageName).nthCalledWith(1, 'C#'); + expect(utils.escapeLanguageName).nthReturnedWith(1, 'C_sharp'); + + expect(utils.escapeLanguageName).nthCalledWith(2, 'C/AL'); + expect(utils.escapeLanguageName).nthReturnedWith(2, 'C_AL'); + + expect(utils.escapeLanguageName).nthCalledWith(3, 'Visual Basic'); + expect(utils.escapeLanguageName).nthReturnedWith(3, 'VisualBasic'); + + expect(utils.escapeLanguageName).toBeCalledTimes(3); + + utils.escapeLanguageName.mockRestore(); + }); }); diff --git a/packages/cli/src/commands/split/index.ts b/packages/cli/src/commands/split/index.ts index d1c392da..9a204748 100644 --- a/packages/cli/src/commands/split/index.ts +++ b/packages/cli/src/commands/split/index.ts @@ -5,7 +5,7 @@ import * as path from 'path'; import { performance } from 'perf_hooks'; const isEqual = require('lodash.isequal'); -import { printExecutionTime, pathToFilename, readYaml, writeYaml, exitWithError } from '../../utils'; +import { printExecutionTime, pathToFilename, readYaml, writeYaml, exitWithError, escapeLanguageName } from '../../utils'; import { isString, isObject, isEmptyObject } from '../../js-utils'; import { Definition, @@ -271,7 +271,7 @@ function iteratePathItems( const sampleFileName = path.join( openapiDir, 'code_samples', - sample.lang, + escapeLanguageName(sample.lang), codeSamplesPathPrefix + pathToFilename(pathName, pathSeparator), method + langToExt(sample.lang), ); diff --git a/packages/cli/src/utils.ts b/packages/cli/src/utils.ts index 9c8761e2..2d0a34fd 100644 --- a/packages/cli/src/utils.ts +++ b/packages/cli/src/utils.ts @@ -74,6 +74,13 @@ export function pathToFilename(path: string, pathSeparator: string) { .replace(/\//g, pathSeparator); } +export function escapeLanguageName(lang: string) { + return lang + .replace(/#/g, "_sharp") + .replace(/\//, '_') + .replace(/\s/g, ''); +} + export class CircularJSONNotSupportedError extends Error { constructor(public originalError: Error) { super(originalError.message);