Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: replace refs to components when splitting file #1381

Merged
merged 4 commits into from
Jan 16, 2024
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
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
Loading