fix: escape symbols in the samples lang name (#703)

Co-authored-by: Anton Kozachuk <antonkozachuk@Antons-MacBook-Pro.local>
This commit is contained in:
Anton Kozachuk
2022-05-25 17:31:25 +03:00
committed by GitHub
parent fb11aaa0e7
commit 0694e714f3
4 changed files with 92 additions and 3 deletions

View File

@@ -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();"
}
]
}
}
}
}

View File

@@ -6,6 +6,8 @@ import {
} from '../types'; } from '../types';
import { blue, green } from 'colorette'; import { blue, green } from 'colorette';
const utils = require('../../../utils');
jest.mock('../../../utils', () => ({ jest.mock('../../../utils', () => ({
...jest.requireActual('../../../utils'), ...jest.requireActual('../../../utils'),
writeYaml: jest.fn(), writeYaml: jest.fn(),
@@ -46,7 +48,6 @@ describe('#split', () => {
it('should use the correct separator', async () => { it('should use the correct separator', async () => {
const filePath = "packages/cli/src/commands/split/__tests__/fixtures/spec.json"; const filePath = "packages/cli/src/commands/split/__tests__/fixtures/spec.json";
const utils = require('../../../utils');
jest.spyOn(utils, 'pathToFilename').mockImplementation(() => 'newFilePath'); jest.spyOn(utils, 'pathToFilename').mockImplementation(() => 'newFilePath');
await handleSplit ( await handleSplit (
@@ -93,4 +94,24 @@ describe('#split', () => {
expect(openapiCore.slash).toHaveBeenCalledWith('webhooks/test.yaml'); expect(openapiCore.slash).toHaveBeenCalledWith('webhooks/test.yaml');
expect(path.relative).toHaveBeenCalledWith('test', 'test/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();
});
}); });

View File

@@ -5,7 +5,7 @@ import * as path from 'path';
import { performance } from 'perf_hooks'; import { performance } from 'perf_hooks';
const isEqual = require('lodash.isequal'); 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 { isString, isObject, isEmptyObject } from '../../js-utils';
import { import {
Definition, Definition,
@@ -271,7 +271,7 @@ function iteratePathItems(
const sampleFileName = path.join( const sampleFileName = path.join(
openapiDir, openapiDir,
'code_samples', 'code_samples',
sample.lang, escapeLanguageName(sample.lang),
codeSamplesPathPrefix + pathToFilename(pathName, pathSeparator), codeSamplesPathPrefix + pathToFilename(pathName, pathSeparator),
method + langToExt(sample.lang), method + langToExt(sample.lang),
); );

View File

@@ -74,6 +74,13 @@ export function pathToFilename(path: string, pathSeparator: string) {
.replace(/\//g, pathSeparator); .replace(/\//g, pathSeparator);
} }
export function escapeLanguageName(lang: string) {
return lang
.replace(/#/g, "_sharp")
.replace(/\//, '_')
.replace(/\s/g, '');
}
export class CircularJSONNotSupportedError extends Error { export class CircularJSONNotSupportedError extends Error {
constructor(public originalError: Error) { constructor(public originalError: Error) {
super(originalError.message); super(originalError.message);