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
5 changes: 5 additions & 0 deletions .changeset/curly-tools-tap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@redocly/openapi-core": patch
---

Fixed an issue where the `bundle` command did not substitute self-references with local references.
24 changes: 24 additions & 0 deletions __tests__/bundle/self-file-ref-normalization/openapi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
openapi: 3.0.0
info:
title: Self-file ref normalization
version: 1.0.0
servers:
- url: http://example.com
security: []
paths:
/test:
get:
parameters:
- $ref: 'openapi.yaml#/components/parameters/myParam'
responses:
'200':
description: OK
'400':
description: Bad Request
components:
parameters:
myParam:
name: myParam
in: query
schema:
type: string
3 changes: 3 additions & 0 deletions __tests__/bundle/self-file-ref-normalization/redocly.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
apis:
main:
root: ./openapi.yaml
27 changes: 27 additions & 0 deletions __tests__/bundle/self-file-ref-normalization/snapshot.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
openapi: 3.0.0
info:
title: Self-file ref normalization
version: 1.0.0
servers:
- url: http://example.com
security: []
paths:
/test:
get:
parameters:
- $ref: '#/components/parameters/myParam'
responses:
'200':
description: OK
'400':
description: Bad Request
components:
parameters:
myParam:
name: myParam
in: query
schema:
type: string

bundling openapi.yaml using configuration for api 'main'...
📦 Created a bundle for openapi.yaml at stdout <test>ms.
97 changes: 97 additions & 0 deletions packages/core/src/__tests__/__snapshots__/bundle.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,57 @@ components:

`;

exports[`bundle > should normalize self-file explicit $ref in nested referenced file 1`] = `
openapi: 3.0.0
info:
title: OAS3 root with external ref to file with self-refs
version: 1.0.0
paths:
/test:
get:
parameters:
- $ref: '#/components/parameters/testParam'
components:
schemas:
NestedSchema:
type: object
properties:
value:
type: string
TestSchema:
type: object
properties:
id:
type: string
nested:
$ref: '#/components/schemas/NestedSchema'
parameters:
testParam:
name: testParam
in: query
schema:
$ref: '#/components/schemas/TestSchema'

`;

exports[`bundle > should normalize self-file explicit $ref in oas2 1`] = `
swagger: '2.0'
info:
title: OAS2 self-file ref
version: '1.0'
paths:
/test:
get:
parameters:
- $ref: '#/parameters/testParam'
parameters:
testParam:
name: testParam
in: query
type: string

`;

exports[`bundle > should not bundle url refs if used with keepUrlRefs 1`] = `
openapi: 3.0.0
paths:
Expand Down Expand Up @@ -277,3 +328,49 @@ components:
someexternal: External schema content

`;

exports[`bundle async > should normalize self-file explicit $ref in asyncapi 2 1`] = `
asyncapi: 2.6.0
info:
title: AsyncAPI 2 self-file ref
version: 1.0.0
channels:
user/test:
subscribe:
message:
payload:
$ref: '#/components/schemas/TestPayload'
components:
schemas:
TestPayload:
type: string

`;

exports[`bundle async > should normalize self-file explicit $ref in nested referenced file for async3 1`] = `
asyncapi: 3.0.0
info:
title: AsyncAPI 3 root with external ref to file with self-refs
version: 1.0.0
channels:
userTest:
messages:
TestMessage:
payload:
$ref: '#/components/schemas/TestPayload'
components:
schemas:
NestedSchema:
type: object
properties:
value:
type: string
TestPayload:
type: object
properties:
id:
type: string
nested:
$ref: '#/components/schemas/NestedSchema'

`;
41 changes: 41 additions & 0 deletions packages/core/src/__tests__/bundle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,27 @@ describe('bundle', () => {

`);
});

it('should normalize self-file explicit $ref in oas2', async () => {
const { bundle: res, problems } = await bundle({
config: await createConfig({}),
ref: path.join(__dirname, 'fixtures/self-file-refs/oas2.yaml'),
});
expect(problems).toHaveLength(0);
expect(res.parsed).toMatchSnapshot();
});

it('should normalize self-file explicit $ref in nested referenced file', async () => {
const config = await createConfig({});

const { bundle: res, problems } = await bundle({
config,
ref: path.join(__dirname, 'fixtures/self-file-refs/oas3-root.yaml'),
});

expect(problems).toHaveLength(0);
expect(res.parsed).toMatchSnapshot();
});
});

describe('bundleFromString', () => {
Expand Down Expand Up @@ -432,4 +453,24 @@ describe('bundle async', () => {

`);
});

it('should normalize self-file explicit $ref in asyncapi 2', async () => {
const { bundle: res, problems } = await bundle({
config: await createConfig({}),
ref: path.join(__dirname, 'fixtures/self-file-refs/async2.yaml'),
});

expect(problems).toHaveLength(0);
expect(res.parsed).toMatchSnapshot();
});

it('should normalize self-file explicit $ref in nested referenced file for async3', async () => {
const { bundle: res, problems } = await bundle({
config: await createConfig({}),
ref: path.join(__dirname, 'fixtures/self-file-refs/async3-root.yaml'),
});

expect(problems).toHaveLength(0);
expect(res.parsed).toMatchSnapshot();
});
});
14 changes: 14 additions & 0 deletions packages/core/src/__tests__/fixtures/self-file-refs/async2.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
asyncapi: '2.6.0'
info:
title: AsyncAPI 2 self-file ref
version: '1.0.0'
channels:
user/test:
subscribe:
message:
payload:
$ref: 'async2.yaml#/components/schemas/TestPayload'
components:
schemas:
TestPayload:
type: string
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
asyncapi: 3.0.0
info:
title: AsyncAPI 3 nested file with self-refs
version: '1.0.0'
channels: {}
components:
messages:
TestMessage:
payload:
$ref: 'async3-nested.yaml#/components/schemas/TestPayload'
schemas:
TestPayload:
type: object
properties:
id:
type: string
nested:
$ref: 'async3-nested.yaml#/components/schemas/NestedSchema'
NestedSchema:
type: object
properties:
value:
type: string
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
asyncapi: 3.0.0
info:
title: AsyncAPI 3 root with external ref to file with self-refs
version: '1.0.0'
channels:
userTest:
messages:
TestMessage:
$ref: './async3-nested.yaml#/components/messages/TestMessage'
14 changes: 14 additions & 0 deletions packages/core/src/__tests__/fixtures/self-file-refs/oas2.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
swagger: '2.0'
info:
title: OAS2 self-file ref
version: '1.0'
paths:
/test:
get:
parameters:
- $ref: 'oas2.yaml#/parameters/testParam'
parameters:
testParam:
name: testParam
in: query
type: string
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
openapi: 3.0.0
info:
title: OAS3 nested file with self-refs
version: 1.0.0
paths: {}
components:
parameters:
testParam:
name: testParam
in: query
schema:
$ref: 'oas3-nested.yaml#/components/schemas/TestSchema'
schemas:
TestSchema:
type: object
properties:
id:
type: string
nested:
$ref: 'oas3-nested.yaml#/components/schemas/NestedSchema'
NestedSchema:
type: object
properties:
value:
type: string
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
openapi: 3.0.0
info:
title: OAS3 root with external ref to file with self-refs
version: 1.0.0
paths:
/test:
get:
parameters:
- $ref: './oas3-nested.yaml#/components/parameters/testParam'
6 changes: 6 additions & 0 deletions packages/core/src/bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,12 +338,18 @@ function makeBundleVisitor(
reportUnresolvedRef(resolved, ctx.report, ctx.location);
return;
}

if (
resolved.location.source === rootDocument.source &&
resolved.location.source === ctx.location.source &&
ctx.type.name !== 'scalar' &&
!dereference
) {
// Normalize explicit self-file refs to internal pointer
if (node.$ref !== resolved.location.pointer) {
node.$ref = resolved.location.pointer;
}

return;
}

Expand Down
7 changes: 6 additions & 1 deletion packages/core/src/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,12 @@ export async function resolveDocument(opts: {
}

const { uri, pointer } = parseRef(ref.$ref);
const isRemote = uri !== null;

const isRemote =
uri !== null &&
externalRefResolver.resolveExternalRef(document.source.absoluteRef, uri) !==
document.source.absoluteRef;

let targetDoc: Document;
try {
targetDoc = isRemote
Expand Down
Loading