mirror of
https://github.com/LukeHagar/redocly-cli.git
synced 2025-12-07 20:57:49 +00:00
fix: escape symbols in the samples lang name (#703)
Co-authored-by: Anton Kozachuk <antonkozachuk@Antons-MacBook-Pro.local>
This commit is contained in:
@@ -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();"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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),
|
||||
);
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user