Skip to content

Commit

Permalink
fix: resolve API alias paths correctly for config files located outsi…
Browse files Browse the repository at this point in the history
…de the root folder
  • Loading branch information
tatomyr committed Dec 11, 2024
1 parent 05cb768 commit 679e73e
Show file tree
Hide file tree
Showing 10 changed files with 132 additions and 45 deletions.
5 changes: 5 additions & 0 deletions .changeset/silly-chairs-applaud.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@redocly/cli": patch
---

Fixed an issue where an API alias's root path might be resolved incorrectly for configuration files located outside the root folder.
14 changes: 14 additions & 0 deletions __tests__/commands.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,20 @@ describe('E2E', () => {
const result = getCommandOutput(args, testPath);
(<any>expect(cleanupOutput(result))).toMatchSpecificSnapshot(join(testPath, 'snapshot.js'));
});

test('apply a decorator to a specific api (without specifying the api)', () => {
const testPath = join(folderPath, 'apply-per-api-decorators');
const args = getParams('../../../packages/cli/src/index.ts', 'bundle');
const result = getCommandOutput(args, testPath);
(<any>expect(cleanupOutput(result))).toMatchSpecificSnapshot(join(testPath, 'snapshot.js'));
});

test('apply a decorator to a specific api (when the api is specified as an alias)', () => {
const testPath = join(folderPath, 'apply-per-api-decorators');
const args = getParams('../../../packages/cli/src/index.ts', 'bundle', ['test@v1']);
const result = getCommandOutput(args, testPath);
(<any>expect(cleanupOutput(result))).toMatchSpecificSnapshot(join(testPath, 'snapshot.js'));
});
});

describe('build-docs', () => {
Expand Down
8 changes: 8 additions & 0 deletions __tests__/miscellaneous/apply-per-api-decorators/openapi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
openapi: 3.1.0
info:
title: Test
paths: {}
components:
schemas:
Unused:
type: string
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = sparkle;

function sparkle() {
return {
Info: {
leave(target) {
target.version = '1.0.0';
},
},
};
}
12 changes: 12 additions & 0 deletions __tests__/miscellaneous/apply-per-api-decorators/plugins/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const version = require('./decorators/version.js');

module.exports = function testPlugin() {
return {
id: 'test',
decorators: {
oas3: {
version,
},
},
};
};
8 changes: 8 additions & 0 deletions __tests__/miscellaneous/apply-per-api-decorators/redocly.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
apis:
test@v1:
root: openapi.yaml
decorators:
test/version: on
remove-unused-components: on
plugins:
- plugins/test.js
33 changes: 33 additions & 0 deletions __tests__/miscellaneous/apply-per-api-decorators/snapshot.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`E2E miscellaneous apply a decorator to a specific api (when the api is specified as an alias) 1`] = `
openapi: 3.1.0
info:
title: Test
version: 1.0.0
paths: {}
components:
schemas:
Unused:
type: string
bundling ./__tests__/miscellaneous/apply-per-api-decorators/openapi.yaml...
📦 Created a bundle for ./__tests__/miscellaneous/apply-per-api-decorators/openapi.yaml at stdout <test>ms.
`;

exports[`E2E miscellaneous apply a decorator to a specific api (without specifying the api) 1`] = `
openapi: 3.1.0
info:
title: Test
version: 1.0.0
paths: {}
components:
schemas:
Unused:
type: string
bundling ./__tests__/miscellaneous/apply-per-api-decorators/openapi.yaml...
📦 Created a bundle for ./__tests__/miscellaneous/apply-per-api-decorators/openapi.yaml at stdout <test>ms.
`;
18 changes: 11 additions & 7 deletions packages/cli/src/commands/login.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { blue, green, gray } from 'colorette';
import { RedoclyClient } from '@redocly/openapi-core';
import { promptUser } from '../utils/miscellaneous';
import { exitWithError, promptUser } from '../utils/miscellaneous';

import type { CommandArgs } from '../wrapper';
import type { Region } from '@redocly/openapi-core';
Expand All @@ -21,10 +21,14 @@ export type LoginOptions = {
};

export async function handleLogin({ argv, config }: CommandArgs<LoginOptions>) {
const region = argv.region || config.region;
const client = new RedoclyClient(region);
const clientToken = await promptClientToken(client.domain);
process.stdout.write(gray('\n Logging in...\n'));
await client.login(clientToken, argv.verbose);
process.stdout.write(green(' Authorization confirmed. ✅\n\n'));
try {
const region = argv.region || config.region;
const client = new RedoclyClient(region);
const clientToken = await promptClientToken(client.domain);
process.stdout.write(gray('\n Logging in...\n'));
await client.login(clientToken, argv.verbose);
process.stdout.write(green(' Authorization confirmed. ✅\n\n'));
} catch (err) {
exitWithError(' ' + err?.message);
}
}
4 changes: 2 additions & 2 deletions packages/cli/src/utils/miscellaneous.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ function getAliasOrPath(config: ConfigApis, aliasOrPath: string): Entrypoint {
const aliasApi = config.apis[aliasOrPath];
return aliasApi
? {
path: aliasApi.root,
path: resolve(getConfigDirectory(config), aliasApi.root),
alias: aliasOrPath,
output: aliasApi.output,
output: aliasApi.output && resolve(getConfigDirectory(config), aliasApi.output),
}
: {
path: aliasOrPath,
Expand Down
64 changes: 28 additions & 36 deletions packages/core/src/config/config-resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -370,19 +370,19 @@ export async function resolveApis({
return resolvedApis;
}

async function resolveAndMergeNestedStyleguideConfig(
{
styleguideConfig,
configPath = '',
resolver = new BaseResolver(),
}: {
styleguideConfig?: StyleguideRawConfig;
configPath?: string;
resolver?: BaseResolver;
},
parentConfigPaths: string[] = [],
extendPaths: string[] = []
): Promise<ResolvedStyleguideConfig> {
async function resolveAndMergeNestedStyleguideConfig({
styleguideConfig,
configPath = '',
resolver = new BaseResolver(),
parentConfigPaths = [],
extendPaths = [],
}: {
styleguideConfig?: StyleguideRawConfig;
configPath?: string;
resolver?: BaseResolver;
parentConfigPaths?: string[];
extendPaths?: string[];
}): Promise<ResolvedStyleguideConfig> {
if (parentConfigPaths.includes(configPath)) {
throw new Error(`Circular dependency in config file: "${configPath}"`);
}
Expand Down Expand Up @@ -414,15 +414,13 @@ async function resolveAndMergeNestedStyleguideConfig(
? new URL(presetItem, configPath).href
: path.resolve(path.dirname(configPath), presetItem);
const extendedStyleguideConfig = await loadExtendStyleguideConfig(pathItem, resolver);
return await resolveAndMergeNestedStyleguideConfig(
{
styleguideConfig: extendedStyleguideConfig,
configPath: pathItem,
resolver: resolver,
},
[...parentConfigPaths, resolvedConfigPath],
extendPaths
);
return await resolveAndMergeNestedStyleguideConfig({
styleguideConfig: extendedStyleguideConfig,
configPath: pathItem,
resolver,
parentConfigPaths: [...parentConfigPaths, resolvedConfigPath],
extendPaths,
});
}) || []
);

Expand All @@ -446,20 +444,14 @@ async function resolveAndMergeNestedStyleguideConfig(
};
}

export async function resolveStyleguideConfig(
opts: {
styleguideConfig?: StyleguideRawConfig;
configPath?: string;
resolver?: BaseResolver;
},
parentConfigPaths: string[] = [],
extendPaths: string[] = []
): Promise<ResolvedStyleguideConfig> {
const resolvedStyleguideConfig = await resolveAndMergeNestedStyleguideConfig(
opts,
parentConfigPaths,
extendPaths
);
export async function resolveStyleguideConfig(opts: {
styleguideConfig?: StyleguideRawConfig;
configPath?: string;
resolver?: BaseResolver;
parentConfigPaths?: string[];
extendPaths?: string[];
}): Promise<ResolvedStyleguideConfig> {
const resolvedStyleguideConfig = await resolveAndMergeNestedStyleguideConfig(opts);

return {
...resolvedStyleguideConfig,
Expand Down

0 comments on commit 679e73e

Please sign in to comment.