Skip to content

Commit 0007c72

Browse files
authored
Handle scoped only imports (#12)
1 parent dcdb5ea commit 0007c72

File tree

2 files changed

+101
-4
lines changed

2 files changed

+101
-4
lines changed

lib/bundle/index.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,7 @@ function inventory$Ref ($refParent, $refKey, path, pathFromRoot, indirections, i
143143

144144
if (options.bundle.generateKey && file !== $refs._root$Ref.path) {
145145
if (!customRoots[file]) {
146-
customRoots[file] = {
147-
"#": options.bundle.generateKey($refs._root$Ref.value, file, null, pathFromRoot)
148-
};
146+
customRoots[file] = {};
149147
}
150148

151149
if (!(hash in customRoots[file])) {
@@ -252,7 +250,7 @@ function remap (schema, inventory, options, customRoots) {
252250
// console.log('Re-mapping $ref pointer "%s" at %s', entry.$ref.$ref, entry.pathFromRoot);
253251

254252
// if entry is not inlineable and has a custom root linked, we need to remap the properties of the object
255-
if (customRoots[entry.file] && entry.hash in customRoots[entry.file] && customRoots[entry.file]["#"] !== null) {
253+
if (customRoots[entry.file] && entry.hash in customRoots[entry.file] && typeof customRoots[entry.file]["#"] === "string") {
256254
if (entry.hash === "#") {
257255
// the whole file is referenced for the first time
258256
// we need to inject its entire content here

test/specs/custom-bundling-roots/custom-bundling-roots.spec.js

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,105 @@ describe("Custom bundling roots", () => {
5454
});
5555
});
5656

57+
it("scoped references only", async () => {
58+
let parser = new $RefParser();
59+
60+
const schema = await parser.bundle(path.rel("specs/custom-bundling-roots/n"), {
61+
properties: {
62+
test: {
63+
type: {
64+
$ref: "#/definitions/Number"
65+
}
66+
}
67+
},
68+
definitions: {
69+
Number: {
70+
type: {
71+
$ref: "./id.json#/type"
72+
}
73+
},
74+
Name: {
75+
type: "string",
76+
example: {
77+
$ref: "./id.json#/name"
78+
}
79+
}
80+
}
81+
}, {
82+
bundle: getDefaultsForOAS2(),
83+
});
84+
85+
expect(schema).to.equal(parser.schema);
86+
expect(schema).to.deep.equal({
87+
properties: {
88+
test: {
89+
type: {
90+
$ref: "#/definitions/Number"
91+
}
92+
}
93+
},
94+
definitions: {
95+
Number: {
96+
type: "number",
97+
},
98+
Name: {
99+
example: "id",
100+
type: "string"
101+
}
102+
}
103+
});
104+
});
105+
106+
it("scoped and full references", async () => {
107+
let parser = new $RefParser();
108+
109+
const schema = await parser.bundle(path.rel("specs/custom-bundling-roots/n"), {
110+
properties: {
111+
test: {
112+
type: {
113+
$ref: "#/definitions/Number"
114+
}
115+
}
116+
},
117+
definitions: {
118+
Number: {
119+
type: {
120+
$ref: "./id.json#/type"
121+
}
122+
},
123+
Id: {
124+
$ref: "./id.json"
125+
}
126+
}
127+
}, {
128+
bundle: getDefaultsForOAS2(),
129+
});
130+
131+
expect(schema).to.equal(parser.schema);
132+
expect(schema).to.deep.equal({
133+
properties: {
134+
test: {
135+
type: {
136+
$ref: "#/definitions/Number"
137+
}
138+
}
139+
},
140+
definitions: {
141+
Number: {
142+
type: {
143+
$ref: "#/definitions/Id/type"
144+
}
145+
},
146+
Id: {
147+
type: "number",
148+
name: "id",
149+
in: "path",
150+
required: true
151+
}
152+
}
153+
});
154+
});
155+
57156
it("should handle $refs whose parents were remapped", async () => {
58157
setupHttpMocks({
59158
"http://localhost:8080/api/nodes.raw?srn=gh/stoplightio/test/Book.v1.yaml": {

0 commit comments

Comments
 (0)