Skip to content
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
1 change: 1 addition & 0 deletions docs/commands/bundle.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Option | Type | Description
entrypoints | [string] | List of API root definition filenames or names assigned in the `apis` section of your Redocly configuration file. Default values are all names defined in the `apis` section within your configuration file.
--config | string | Specify path to the [config file](#custom-configuration-file).
--dereferenced, -d | boolean | Generate fully dereferenced bundle.
--keep-url-references, -k | boolean | Keep absolute url references.
--ext | string | Specify bundled file extension. Possible values are `json`, `yaml`, or `yml`. Default value is `yaml`.
--extends | [string] | Can be used in combination with `--lint` to [extend a specific configuration](./lint.md#extend-configuration). Default values are taken from the Redocly configuration file.
--force, -f | boolean | Generate bundle output even when errors occur.
Expand Down
2 changes: 2 additions & 0 deletions packages/cli/src/commands/bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export async function handleBundle(
metafile?: string;
extends?: string[];
'remove-unused-components'?: boolean;
'keep-url-references'?: boolean;
},
version: string,
) {
Expand Down Expand Up @@ -98,6 +99,7 @@ export async function handleBundle(
ref: path,
dereference: argv.dereferenced,
removeUnusedComponents,
keepUrlRefs: argv['keep-url-references'],

@adamaltman adamaltman May 18, 2022

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Two questions I have: should it be keep-url-references or keep-url-refs?

What if someone should want to keep other refs such as file refs? Will it require another option? Or should the option not be boolean? (Yes, I guess that's four questions)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

What the bundle will it be if we keep file $refs? :)
Regarding the name, I'm ok with the current one, it is clear enough for me.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I'm merging it as it is.
@adamaltman if you think it might be beneficial to keep other refs, I think it would be better to have a separate boolean keep-whatever-references option.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Okay, that makes sense. I wonder though about references vs refs in the option name.

});

const fileTotals = getTotals(problems);
Expand Down
26 changes: 26 additions & 0 deletions packages/core/__tests__/__snapshots__/bundle.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,32 @@ components:

`;

exports[`bundle should not bundle url refs if used with keepUrlRefs 1`] = `
openapi: 3.0.0
paths:
/pet:
parameters:
- $ref: '#/components/parameters/path-param'
put:
parameters:
- $ref: https://someexternal.schema
- $ref: '#/components/parameters/param-b'
- name: test
get:
parameters:
- $ref: http://someexternal.schema
- $ref: '#/components/parameters/param-c'
components:
parameters:
path-param:
name: path_param
param-c:
name: param_c
param-b:
name: param_b

`;

exports[`bundle should not place referened schema inline when component in question is not of type "schemas" 1`] = `
openapi: 3.0.0
paths:
Expand Down
27 changes: 26 additions & 1 deletion packages/core/__tests__/bundle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,5 +128,30 @@ describe('bundle', () => {
}
);
expect(res.parsed).toMatchSnapshot();
})
});

it('should not bundle url refs if used with keepUrlRefs', async () => {
const fetchMock = jest.fn(() =>
Promise.resolve({
ok: true,
text: () => 'External schema content',
headers: {
get: () => '',
},
}),
);
const { bundle: res, problems } = await bundle({
config: new Config({} as ResolvedConfig),
externalRefResolver: new BaseResolver({
http: {
customFetch: fetchMock,
headers: [],
},
}),
ref: path.join(__dirname, 'fixtures/refs/openapi-with-url-refs.yaml'),
keepUrlRefs: true,
});
expect(problems).toHaveLength(0);
expect(res.parsed).toMatchSnapshot();
});
});
18 changes: 18 additions & 0 deletions packages/core/__tests__/fixtures/refs/openapi-with-url-refs.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
openapi: 3.0.0
paths:
/pet:
parameters:
- $ref: '#/components/parameters/path-param'
put:
parameters:
- $ref: 'https://someexternal.schema'
- $ref: ./param-b.yaml
- name: test
get:
parameters:
- $ref: 'http://someexternal.schema'
- $ref: ./param-c.yaml
components:
parameters:
path-param:
name: path_param
21 changes: 18 additions & 3 deletions packages/core/src/bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Oas3_1Types } from './types/oas3_1';
import { NormalizedNodeType, normalizeTypes, NodeType } from './types';
import { WalkContext, walkDocument, UserContext, ResolveResult } from './walk';
import { detectOpenAPI, openAPIMajor, OasMajorVersion } from './oas-types';
import { isRef, Location, refBaseName } from './ref-utils';
import {isAbsoluteUrl, isRef, Location, refBaseName} from './ref-utils';
import { initRules } from './config/rules';
import { reportUnresolvedRef } from './rules/no-unresolved-refs';
import { isPlainObject } from './utils';
Expand Down Expand Up @@ -35,6 +35,7 @@ export async function bundle(opts: {
base?: string;
skipRedoclyRegistryRefs?: boolean;
removeUnusedComponents?: boolean;
keepUrlRefs?: boolean;
}) {
const {
ref,
Expand Down Expand Up @@ -71,6 +72,7 @@ export async function bundleDocument(opts: {
dereference?: boolean;
skipRedoclyRegistryRefs?: boolean;
removeUnusedComponents?: boolean;
keepUrlRefs?: boolean;
}) {
const {
document,
Expand All @@ -80,6 +82,7 @@ export async function bundleDocument(opts: {
dereference = false,
skipRedoclyRegistryRefs = false,
removeUnusedComponents = false,
keepUrlRefs = false,
} = opts;
const oasVersion = detectOpenAPI(document.parsed);
const oasMajorVersion = openAPIMajor(oasVersion);
Expand Down Expand Up @@ -128,7 +131,14 @@ export async function bundleDocument(opts: {
{
severity: 'error',
ruleId: 'bundler',
visitor: makeBundleVisitor(oasMajorVersion, dereference, skipRedoclyRegistryRefs, document, resolvedRefMap),
visitor: makeBundleVisitor(
oasMajorVersion,
dereference,
skipRedoclyRegistryRefs,
document,
resolvedRefMap,
keepUrlRefs,
),
},
...decorators,
] as any,
Expand Down Expand Up @@ -199,7 +209,8 @@ function makeBundleVisitor(
dereference: boolean,
skipRedoclyRegistryRefs: boolean,
rootDocument: Document,
resolvedRefMap: ResolvedRefMap
resolvedRefMap: ResolvedRefMap,
keepUrlRefs: boolean,
) {
let components: Record<string, Record<string, any>>;

Expand All @@ -224,6 +235,10 @@ function makeBundleVisitor(
return;
}

if (keepUrlRefs && isAbsoluteUrl(node.$ref)) {
return;
}

const componentType = mapTypeToComponent(ctx.type.name, version);
if (!componentType) {
replaceRef(node, resolved, ctx);
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/ref-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export function isMappingRef(mapping: string) {
return (
mapping.startsWith('#') ||
mapping.startsWith('https://') ||
mapping.startsWith('http://') ||
Comment thread
tatomyr marked this conversation as resolved.
mapping.startsWith('./') ||
mapping.startsWith('../') ||
mapping.indexOf('/') > -1
Expand Down