Skip to content

Commit

Permalink
feat: extend split and join commands to produce JSON output (#1305)
Browse files Browse the repository at this point in the history
  • Loading branch information
IgorKarpiuk authored Oct 26, 2023
1 parent 72b225a commit 1510e47
Show file tree
Hide file tree
Showing 22 changed files with 1,103 additions and 39 deletions.
5 changes: 5 additions & 0 deletions .changeset/plenty-mugs-suffer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@redocly/cli': minor
---

Add JSON output support to the `split` and `join` commands.
53 changes: 53 additions & 0 deletions __tests__/commands.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,19 @@ describe('E2E', () => {
const result = getCommandOutput(args, folderPath);
(<any>expect(result)).toMatchSpecificSnapshot(join(folderPath, 'snapshot.js'));
});

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

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

const result = getCommandOutput(args, folderPath);
(<any>expect(result)).toMatchSpecificSnapshot(join(folderPath, 'snapshot.js'));
});
});

describe('join', () => {
Expand Down Expand Up @@ -214,6 +227,46 @@ describe('E2E', () => {
const result = getCommandOutput(args, testPath);
(<any>expect(result)).toMatchSpecificSnapshot(join(testPath, 'snapshot.js'));
});

describe('files with different extensions', () => {
const joinParameters: {
name: string;
folder: string;
entrypoints: string[];
snapshot: string;
output?: string;
}[] = [
{
name: 'first entrypoint is a json file',
folder: 'json-and-yaml-input',
entrypoints: ['foo.json', 'bar.yaml'],
snapshot: 'json-output.snapshot.js',
},
{
name: 'first entrypoint is a yaml file',
folder: 'json-and-yaml-input',
entrypoints: ['bar.yaml', 'foo.json'],
snapshot: 'yaml-output.snapshot.js',
},
{
name: 'json output file',
folder: 'yaml-input-and-json-output',
entrypoints: ['foo.yaml', 'bar.yaml'],
output: 'openapi.json',
snapshot: 'snapshot.js',
},
];

test.each(joinParameters)('test with option: %s', (parameters) => {
const testPath = join(__dirname, `join/${parameters.folder}`);
const argsWithOption = parameters.output
? [...parameters.entrypoints, ...[`-o=${parameters.output}`]]
: parameters.entrypoints;
const args = getParams('../../../packages/cli/src/index.ts', 'join', argsWithOption);
const result = getCommandOutput(args, testPath);
(<any>expect(result)).toMatchSpecificSnapshot(join(testPath, parameters.snapshot));
});
});
});

describe('bundle', () => {
Expand Down
23 changes: 23 additions & 0 deletions __tests__/join/json-and-yaml-input/bar.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
openapi: 3.0.0
info:
title: Example API
description: This is an example API.
version: 1.0.0
servers:
- url: https://redocly-example.com/api
paths:
/users/{userId}:
parameters:
- name: userId
in: path
description: ID of the user
required: true
schema:
type: integer
get:
summary: Get user by ID
responses:
'200':
description: OK
'404':
description: Not found
49 changes: 49 additions & 0 deletions __tests__/join/json-and-yaml-input/foo.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"openapi": "3.0.0",
"info": {
"title": "Example API",
"description": "This is an example API.",
"version": "1.0.0"
},
"servers": [
{
"url": "https://redocly-example.com/api"
}
],
"paths": {
"/users/{userId}/orders/{orderId}": {
"parameters": [
{
"name": "userId",
"in": "path",
"description": "ID of the user",
"required": true,
"schema": {
"type": "integer"
}
},
{
"name": "orderId",
"in": "path",
"description": "ID of the order",
"required": true,
"schema": {
"type": "integer"
}
}
],
"get": {
"x-private": true,
"summary": "Get an order by ID for a specific user",
"responses": {
"200": {
"description": "OK"
},
"404": {
"description": "Not found"
}
}
}
}
}
}
117 changes: 117 additions & 0 deletions __tests__/join/json-and-yaml-input/json-output.snapshot.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`E2E join files with different extensions test with option: {
name: 'first entrypoint is a json file',
folder: 'json-and-yaml-input',
entrypoints: [Array],
snapshot: 'json-output.snapshot.js'
} 1`] = `
{
"openapi": "3.0.0",
"info": {
"title": "Example API",
"description": "This is an example API.",
"version": "<version>"
},
"servers": [
{
"url": "https://redocly-example.com/api"
}
],
"tags": [
{
"name": "foo_other",
"x-displayName": "other"
},
{
"name": "bar_other",
"x-displayName": "other"
}
],
"paths": {
"/users/{userId}/orders/{orderId}": {
"parameters": [
{
"name": "userId",
"in": "path",
"description": "ID of the user",
"required": true,
"schema": {
"type": "integer"
}
},
{
"name": "orderId",
"in": "path",
"description": "ID of the order",
"required": true,
"schema": {
"type": "integer"
}
}
],
"get": {
"x-private": true,
"summary": "Get an order by ID for a specific user",
"responses": {
"200": {
"description": "OK"
},
"404": {
"description": "Not found"
}
},
"tags": [
"foo_other"
]
}
},
"/users/{userId}": {
"parameters": [
{
"name": "userId",
"in": "path",
"description": "ID of the user",
"required": true,
"schema": {
"type": "integer"
}
}
],
"get": {
"summary": "Get user by ID",
"responses": {
"200": {
"description": "OK"
},
"404": {
"description": "Not found"
}
},
"tags": [
"bar_other"
]
}
}
},
"components": {},
"x-tagGroups": [
{
"name": "foo",
"tags": [
"foo_other"
]
},
{
"name": "bar",
"tags": [
"bar_other"
]
}
]
}
openapi.json: join processed in <test>ms
`;
76 changes: 76 additions & 0 deletions __tests__/join/json-and-yaml-input/yaml-output.snapshot.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`E2E join files with different extensions test with option: {
name: 'first entrypoint is a yaml file',
folder: 'json-and-yaml-input',
entrypoints: [Array],
snapshot: 'yaml-output.snapshot.js'
} 1`] = `
openapi: 3.0.0
info:
title: Example API
description: This is an example API.
version: 1.0.0
servers:
- url: https://redocly-example.com/api
tags:
- name: bar_other
x-displayName: other
- name: foo_other
x-displayName: other
paths:
/users/{userId}:
parameters:
- name: userId
in: path
description: ID of the user
required: true
schema:
type: integer
get:
summary: Get user by ID
responses:
'200':
description: OK
'404':
description: Not found
tags:
- bar_other
/users/{userId}/orders/{orderId}:
parameters:
- name: userId
in: path
description: ID of the user
required: true
schema:
type: integer
- name: orderId
in: path
description: ID of the order
required: true
schema:
type: integer
get:
x-private: true
summary: Get an order by ID for a specific user
responses:
'200':
description: OK
'404':
description: Not found
tags:
- foo_other
components: {}
x-tagGroups:
- name: bar
tags:
- bar_other
- name: foo
tags:
- foo_other
openapi.yaml: join processed in <test>ms
`;
Loading

1 comment on commit 1510e47

@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 76.08% 4045/5317
🟡 Branches 65.9% 2145/3255
🟡 Functions 68.26% 656/961
🟡 Lines 76.27% 3793/4973

Test suite run success

645 tests passing in 93 suites.

Report generated by 🧪jest coverage report action from 1510e47

Please sign in to comment.