Skip to content
Merged
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
61 changes: 61 additions & 0 deletions packages/cli/src/commands/split/__tests__/fixtures/samples.json
Original file line number Diff line number Diff line change
@@ -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();"
}
]
}
}
}
}
23 changes: 22 additions & 1 deletion packages/cli/src/commands/split/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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 (
Expand Down Expand Up @@ -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();
});
});
4 changes: 2 additions & 2 deletions packages/cli/src/commands/split/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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),
);
Expand Down
7 changes: 7 additions & 0 deletions packages/cli/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down