Skip to content

Commit

Permalink
fix: replace refs to components when splitting file (#1381)
Browse files Browse the repository at this point in the history
  • Loading branch information
IgorKarpiuk authored Jan 16, 2024
1 parent 8e29966 commit 4f410b5
Show file tree
Hide file tree
Showing 5 changed files with 224 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/clever-dolphins-approve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@redocly/cli": patch
---

Fixed an issue with resolving references after splitting API descriptions written in the json format.
27 changes: 27 additions & 0 deletions __tests__/commands.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { join, relative } from 'path';
import { toMatchSpecificSnapshot } from './specific-snapshot';
import { getCommandOutput, getEntrypoints, callSerializer, getParams } from './helpers';
import * as fs from 'fs';
import { spawnSync } from 'child_process';

expect.extend({
toMatchExtendedSpecificSnapshot(received, snapshotFile) {
Expand Down Expand Up @@ -176,6 +177,32 @@ describe('E2E', () => {
const result = getCommandOutput(args, folderPath);
(<any>expect(result)).toMatchSpecificSnapshot(join(folderPath, 'snapshot.js'));
});

test('openapi json file refs validation', () => {
const folderPath = join(__dirname, `split/refs-in-json`);
const file = '../../../__tests__/split/refs-in-json/openapi.json';

const args = getParams('../../../packages/cli/src/index.ts', 'split', [
file,
'--outDir=output',
]);

// run the split command and write the result to files
spawnSync('ts-node', args, {
cwd: folderPath,
env: {
...process.env,
NODE_ENV: 'production',
NO_COLOR: 'TRUE',
},
});

const lintArgs = getParams('../../../packages/cli/src/index.ts', 'lint', [
join(folderPath, 'output/openapi.json'),
]);
const lintResult = getCommandOutput(lintArgs, folderPath);
(<any>expect(lintResult)).toMatchSpecificSnapshot(join(folderPath, 'snapshot.js'));
});
});

describe('join', () => {
Expand Down
176 changes: 176 additions & 0 deletions __tests__/split/refs-in-json/openapi.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
{
"openapi": "3.0.0",
"info": {
"version": "1.0.0",
"title": "Swagger Petstore",
"license": {
"name": "MIT",
"url": "https://tests.com"
}
},
"security": [],
"servers": [
{
"url": "http://petstore.swagger.io/v1"
}
],
"paths": {
"/pets": {
"get": {
"summary": "List all pets",
"operationId": "listPets",
"tags": ["pets"],
"parameters": [
{
"name": "limit",
"in": "query",
"description": "How many items to return at one time (max 100)",
"required": false,
"schema": {
"type": "integer",
"format": "int32"
}
}
],
"responses": {
"200": {
"description": "A paged array of pets",
"headers": {
"x-next": {
"description": "A link to the next page of responses",
"schema": {
"type": "string"
}
}
},
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Pets"
}
}
}
},
"400": {
"description": "Invalid request"
},
"default": {
"description": "unexpected error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
}
}
},
"post": {
"summary": "Create a pet",
"operationId": "createPets",
"tags": ["pets"],
"responses": {
"201": {
"description": "Null response"
},
"400": {
"description": "Invalid request"
},
"default": {
"description": "unexpected error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
}
}
}
},
"/pets/{petId}": {
"get": {
"summary": "Info for a specific pet",
"operationId": "showPetById",
"tags": ["pets"],
"parameters": [
{
"name": "petId",
"in": "path",
"required": true,
"description": "The id of the pet to retrieve",
"schema": {
"type": "string"
}
}
],
"responses": {
"200": {
"description": "Expected response to a valid request",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Pet"
}
}
}
},
"400": {
"description": "Invalid request"
},
"default": {
"description": "unexpected error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
}
}
}
}
},
"components": {
"schemas": {
"Pet": {
"type": "object",
"required": ["id", "name"],
"properties": {
"id": {
"type": "integer",
"format": "int64"
},
"name": {
"type": "string"
},
"tag": {
"type": "string"
}
}
},
"Pets": {
"type": "array",
"items": {
"$ref": "#/components/schemas/Pet"
}
},
"Error": {
"type": "object",
"required": ["code", "message"],
"properties": {
"code": {
"type": "integer",
"format": "int32"
},
"message": {
"type": "string"
}
}
}
}
}
}
13 changes: 13 additions & 0 deletions __tests__/split/refs-in-json/snapshot.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`E2E split openapi json file refs validation 1`] = `
No configurations were provided -- using built in recommended configuration by default.
validating /output/openapi.json...
/output/openapi.json: validated in <test>ms
Woohoo! Your API description is valid. 🎉
`;
6 changes: 3 additions & 3 deletions packages/cli/src/commands/split/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ function isStartsWithComponents(node: string) {
return node.startsWith(componentsPath);
}

function isNotYaml(filename: string) {
return !(filename.endsWith('.yaml') || filename.endsWith('.yml'));
function isSupportedExtension(filename: string) {
return filename.endsWith('.yaml') || filename.endsWith('.yml') || filename.endsWith('.json');
}

function loadFile(fileName: string) {
Expand Down Expand Up @@ -138,7 +138,7 @@ function traverseDirectoryDeepCallback(
directory: string,
componentsFiles: object
) {
if (isNotYaml(filename)) return;
if (!isSupportedExtension(filename)) return;
const pathData = readYaml(filename);
replace$Refs(pathData, directory, componentsFiles);
writeToFileByExtension(pathData, filename);
Expand Down

1 comment on commit 4f410b5

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage report

St.
Category Percentage Covered / Total
🟡 Statements 75.8% 4100/5409
🟡 Branches 66.07% 2183/3304
🟡 Functions 68.14% 663/973
🟡 Lines 76% 3847/5062

Test suite run success

671 tests passing in 94 suites.

Report generated by 🧪jest coverage report action from 4f410b5

Please sign in to comment.