Skip to content

Commit 26e0206

Browse files
vadyvasJLekawa
andauthored
fix: normalize explicit self-file $ref to internal pointer (#2356)
* fix: normalize explicit self-file $ref to internal pointer * tests: add tests for normalizing self-file explicit $ref in OAS2, AsyncAPI 2, and AsyncAPI 3 * chore: add changeset and remove unused fixture * Update .changeset/curly-tools-tap.md * tests: update self-file reference normalization tests and fixtures for OAS2 and AsyncAPI 3 * fix: simplify self-file reference check * fix: ensure self-file reference is updated only when necessary --------- Co-authored-by: Jacek Łękawa <164185257+JLekawa@users.noreply.github.com>
1 parent 4e51c84 commit 26e0206

File tree

14 files changed

+303
-1
lines changed

14 files changed

+303
-1
lines changed

.changeset/curly-tools-tap.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@redocly/openapi-core": patch
3+
---
4+
5+
Fixed an issue where the `bundle` command did not substitute self-references with local references.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
openapi: 3.0.0
2+
info:
3+
title: Self-file ref normalization
4+
version: 1.0.0
5+
servers:
6+
- url: http://example.com
7+
security: []
8+
paths:
9+
/test:
10+
get:
11+
parameters:
12+
- $ref: 'openapi.yaml#/components/parameters/myParam'
13+
responses:
14+
'200':
15+
description: OK
16+
'400':
17+
description: Bad Request
18+
components:
19+
parameters:
20+
myParam:
21+
name: myParam
22+
in: query
23+
schema:
24+
type: string
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
apis:
2+
main:
3+
root: ./openapi.yaml
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
openapi: 3.0.0
2+
info:
3+
title: Self-file ref normalization
4+
version: 1.0.0
5+
servers:
6+
- url: http://example.com
7+
security: []
8+
paths:
9+
/test:
10+
get:
11+
parameters:
12+
- $ref: '#/components/parameters/myParam'
13+
responses:
14+
'200':
15+
description: OK
16+
'400':
17+
description: Bad Request
18+
components:
19+
parameters:
20+
myParam:
21+
name: myParam
22+
in: query
23+
schema:
24+
type: string
25+
26+
bundling openapi.yaml using configuration for api 'main'...
27+
📦 Created a bundle for openapi.yaml at stdout <test>ms.

packages/core/src/__tests__/__snapshots__/bundle.test.ts.snap

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,57 @@ components:
124124
125125
`;
126126

127+
exports[`bundle > should normalize self-file explicit $ref in nested referenced file 1`] = `
128+
openapi: 3.0.0
129+
info:
130+
title: OAS3 root with external ref to file with self-refs
131+
version: 1.0.0
132+
paths:
133+
/test:
134+
get:
135+
parameters:
136+
- $ref: '#/components/parameters/testParam'
137+
components:
138+
schemas:
139+
NestedSchema:
140+
type: object
141+
properties:
142+
value:
143+
type: string
144+
TestSchema:
145+
type: object
146+
properties:
147+
id:
148+
type: string
149+
nested:
150+
$ref: '#/components/schemas/NestedSchema'
151+
parameters:
152+
testParam:
153+
name: testParam
154+
in: query
155+
schema:
156+
$ref: '#/components/schemas/TestSchema'
157+
158+
`;
159+
160+
exports[`bundle > should normalize self-file explicit $ref in oas2 1`] = `
161+
swagger: '2.0'
162+
info:
163+
title: OAS2 self-file ref
164+
version: '1.0'
165+
paths:
166+
/test:
167+
get:
168+
parameters:
169+
- $ref: '#/parameters/testParam'
170+
parameters:
171+
testParam:
172+
name: testParam
173+
in: query
174+
type: string
175+
176+
`;
177+
127178
exports[`bundle > should not bundle url refs if used with keepUrlRefs 1`] = `
128179
openapi: 3.0.0
129180
paths:
@@ -277,3 +328,49 @@ components:
277328
someexternal: External schema content
278329
279330
`;
331+
332+
exports[`bundle async > should normalize self-file explicit $ref in asyncapi 2 1`] = `
333+
asyncapi: 2.6.0
334+
info:
335+
title: AsyncAPI 2 self-file ref
336+
version: 1.0.0
337+
channels:
338+
user/test:
339+
subscribe:
340+
message:
341+
payload:
342+
$ref: '#/components/schemas/TestPayload'
343+
components:
344+
schemas:
345+
TestPayload:
346+
type: string
347+
348+
`;
349+
350+
exports[`bundle async > should normalize self-file explicit $ref in nested referenced file for async3 1`] = `
351+
asyncapi: 3.0.0
352+
info:
353+
title: AsyncAPI 3 root with external ref to file with self-refs
354+
version: 1.0.0
355+
channels:
356+
userTest:
357+
messages:
358+
TestMessage:
359+
payload:
360+
$ref: '#/components/schemas/TestPayload'
361+
components:
362+
schemas:
363+
NestedSchema:
364+
type: object
365+
properties:
366+
value:
367+
type: string
368+
TestPayload:
369+
type: object
370+
properties:
371+
id:
372+
type: string
373+
nested:
374+
$ref: '#/components/schemas/NestedSchema'
375+
376+
`;

packages/core/src/__tests__/bundle.test.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,27 @@ describe('bundle', () => {
273273
274274
`);
275275
});
276+
277+
it('should normalize self-file explicit $ref in oas2', async () => {
278+
const { bundle: res, problems } = await bundle({
279+
config: await createConfig({}),
280+
ref: path.join(__dirname, 'fixtures/self-file-refs/oas2.yaml'),
281+
});
282+
expect(problems).toHaveLength(0);
283+
expect(res.parsed).toMatchSnapshot();
284+
});
285+
286+
it('should normalize self-file explicit $ref in nested referenced file', async () => {
287+
const config = await createConfig({});
288+
289+
const { bundle: res, problems } = await bundle({
290+
config,
291+
ref: path.join(__dirname, 'fixtures/self-file-refs/oas3-root.yaml'),
292+
});
293+
294+
expect(problems).toHaveLength(0);
295+
expect(res.parsed).toMatchSnapshot();
296+
});
276297
});
277298

278299
describe('bundleFromString', () => {
@@ -432,4 +453,24 @@ describe('bundle async', () => {
432453
433454
`);
434455
});
456+
457+
it('should normalize self-file explicit $ref in asyncapi 2', async () => {
458+
const { bundle: res, problems } = await bundle({
459+
config: await createConfig({}),
460+
ref: path.join(__dirname, 'fixtures/self-file-refs/async2.yaml'),
461+
});
462+
463+
expect(problems).toHaveLength(0);
464+
expect(res.parsed).toMatchSnapshot();
465+
});
466+
467+
it('should normalize self-file explicit $ref in nested referenced file for async3', async () => {
468+
const { bundle: res, problems } = await bundle({
469+
config: await createConfig({}),
470+
ref: path.join(__dirname, 'fixtures/self-file-refs/async3-root.yaml'),
471+
});
472+
473+
expect(problems).toHaveLength(0);
474+
expect(res.parsed).toMatchSnapshot();
475+
});
435476
});
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
asyncapi: '2.6.0'
2+
info:
3+
title: AsyncAPI 2 self-file ref
4+
version: '1.0.0'
5+
channels:
6+
user/test:
7+
subscribe:
8+
message:
9+
payload:
10+
$ref: 'async2.yaml#/components/schemas/TestPayload'
11+
components:
12+
schemas:
13+
TestPayload:
14+
type: string
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
asyncapi: 3.0.0
2+
info:
3+
title: AsyncAPI 3 nested file with self-refs
4+
version: '1.0.0'
5+
channels: {}
6+
components:
7+
messages:
8+
TestMessage:
9+
payload:
10+
$ref: 'async3-nested.yaml#/components/schemas/TestPayload'
11+
schemas:
12+
TestPayload:
13+
type: object
14+
properties:
15+
id:
16+
type: string
17+
nested:
18+
$ref: 'async3-nested.yaml#/components/schemas/NestedSchema'
19+
NestedSchema:
20+
type: object
21+
properties:
22+
value:
23+
type: string
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
asyncapi: 3.0.0
2+
info:
3+
title: AsyncAPI 3 root with external ref to file with self-refs
4+
version: '1.0.0'
5+
channels:
6+
userTest:
7+
messages:
8+
TestMessage:
9+
$ref: './async3-nested.yaml#/components/messages/TestMessage'
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
swagger: '2.0'
2+
info:
3+
title: OAS2 self-file ref
4+
version: '1.0'
5+
paths:
6+
/test:
7+
get:
8+
parameters:
9+
- $ref: 'oas2.yaml#/parameters/testParam'
10+
parameters:
11+
testParam:
12+
name: testParam
13+
in: query
14+
type: string

0 commit comments

Comments
 (0)