Skip to content

Commit

Permalink
fix: duplicated components created when referencing from components (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
RomanHotsiy committed May 16, 2023
1 parent 5e7d408 commit c02649d
Show file tree
Hide file tree
Showing 19 changed files with 127 additions and 2 deletions.
3 changes: 3 additions & 0 deletions __tests__/bundle/no-duplicated-components/.redocly.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
apis:
main:
root: ./openapi.yaml
1 change: 1 addition & 0 deletions __tests__/bundle/no-duplicated-components/Test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
type: object
8 changes: 8 additions & 0 deletions __tests__/bundle/no-duplicated-components/openapi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
openapi: 3.1.0
paths:
/path:
$ref: './paths/path.yaml'
components:
schemas:
Test:
$ref: './Test.yaml'
6 changes: 6 additions & 0 deletions __tests__/bundle/no-duplicated-components/paths/path.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
get:
requestBody:
content:
'application/json':
schema:
$ref: ../Test.yaml
21 changes: 21 additions & 0 deletions __tests__/bundle/no-duplicated-components/snapshot.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`E2E bundle no-duplicated-components 1`] = `
openapi: 3.1.0
paths:
/path:
get:
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/Test'
components:
schemas:
Test:
type: object
bundling ./openapi.yaml...
📦 Created a bundle for ./openapi.yaml at stdout <test>ms.
`;
3 changes: 3 additions & 0 deletions __tests__/bundle/no-self-referenced-components/.redocly.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
apis:
main:
root: ./openapi.yaml
9 changes: 9 additions & 0 deletions __tests__/bundle/no-self-referenced-components/openapi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
openapi: 3.0.3
paths:
/pets/{petId}:
$ref: './paths/Pet.yaml'
components:
schemas:
$ref: './schemas/_index.yaml'
parameters:
$ref: './parameters/_index.yaml'
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
name: petId
in: path
schema:
type: string
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
PetId:
$ref: './PetId.yaml'
9 changes: 9 additions & 0 deletions __tests__/bundle/no-self-referenced-components/paths/Pet.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
get:
parameters:
- $ref: '../parameters/PetId.yaml'
responses:
'200':
content:
application/json:
schema:
$ref: '../schemas/Pet.yaml'
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
type: object
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Pet:
$ref: './Pet.yaml'
30 changes: 30 additions & 0 deletions __tests__/bundle/no-self-referenced-components/snapshot.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`E2E bundle no-self-referenced-components 1`] = `
openapi: 3.0.3
paths:
/pets/{petId}:
get:
parameters:
- $ref: '#/components/parameters/PetId'
responses:
'200':
content:
application/json:
schema:
$ref: '#/components/schemas/Pet'
components:
schemas:
Pet:
type: object
parameters:
PetId:
name: petId
in: path
schema:
type: string
bundling ./openapi.yaml...
📦 Created a bundle for ./openapi.yaml at stdout <test>ms.
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
apis:
main:
root: ./openapi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
schemas:
myObject:
$ref: ../folder/myObject.v2.schema.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
type: object
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
openapi: 3.0.3
components:
$ref: ./components/schemas.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`E2E bundle no-self-referenced-or-duplicated-components 1`] = `
openapi: 3.0.3
components:
schemas:
myObject.v2.schema:
type: object
bundling ./openapi.yaml...
📦 Created a bundle for ./openapi.yaml at stdout <test>ms.
`;
7 changes: 5 additions & 2 deletions packages/core/src/bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ function makeBundleVisitor(
keepUrlRefs: boolean
) {
let components: Record<string, Record<string, any>>;
let rootLocation: Location;

const visitor: Oas3Visitor | Oas2Visitor = {
ref: {
Expand Down Expand Up @@ -264,7 +265,8 @@ function makeBundleVisitor(
},
},
Root: {
enter(root: any) {
enter(root: any, ctx: any) {
rootLocation = ctx.location;
if (version === OasMajorVersion.Version3) {
components = root.components = root.components || {};
} else if (version === OasMajorVersion.Version2) {
Expand Down Expand Up @@ -342,7 +344,8 @@ function makeBundleVisitor(
) {
if (
isRef(node) &&
ctx.resolve(node).location?.absolutePointer === target.location.absolutePointer
ctx.resolve(node, rootLocation.absolutePointer).location?.absolutePointer ===
target.location.absolutePointer
) {
return true;
}
Expand Down

1 comment on commit c02649d

@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 74.19% 3495/4711
🟡 Branches 65.04% 1981/3046
🟡 Functions 66.48% 587/883
🟡 Lines 74.24% 3253/4382

Test suite run success

585 tests passing in 85 suites.

Report generated by 🧪jest coverage report action from c02649d

Please sign in to comment.