Skip to content

Commit 92a7f9a

Browse files
authored
Inline certain references rather than hoisting them (#13)
1 parent 8092880 commit 92a7f9a

File tree

5 files changed

+131
-17
lines changed

5 files changed

+131
-17
lines changed

lib/bundle/defaults.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ module.exports.getDefaultsForOAS2 = function (defaults = getGenericDefaults(new
5454
generateKey (schema, file, hash, pathFromRoot) {
5555
pathFromRoot = normalizeOasSchemasHash(pathFromRoot, defaults.defaultRoot);
5656

57+
if (!pathFromRoot.startsWith(defaults.defaultRoot) && !isSchemaPlacement(pathFromRoot.split("/"))) {
58+
return null;
59+
}
60+
5761
if (hash !== "#" && hash !== null) {
5862
return defaults.generateKey(schema, file, normalizeOasSchemasHash(hash, defaults.defaultRoot), pathFromRoot);
5963
}
@@ -70,3 +74,16 @@ module.exports.getDefaultsForOAS3 = function (defaults = getGenericDefaults(new
7074
function normalizeOasSchemasHash (hash, root) {
7175
return hash.replace(/\/(?:components\/schemas|definitions)\//g, root.slice(1) + "/");
7276
}
77+
78+
// this should return true for every place in a OAS document that can reference a JSON Schema model
79+
function isSchemaPlacement (path) {
80+
if (isInOasOperation(path) && path.includes("schema")) {
81+
return true;
82+
}
83+
84+
return false;
85+
}
86+
87+
function isInOasOperation (path) {
88+
return path.length > 3 && path[1] === "paths";
89+
}

lib/bundle/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ function inventory$Ref ($refParent, $refKey, path, pathFromRoot, indirections, i
145145
}
146146
}
147147

148-
if (options.bundle.generateKey && file !== $refs._root$Ref.path) {
148+
if (options.bundle.generateKey && file !== $refs._root$Ref.path && !(path.indexOf($refs._root$Ref.path) === 0 && $ref.$ref.indexOf("#/") === 0)) {
149149
if (!customRoots[file]) {
150150
customRoots[file] = {};
151151
}

test/specs/custom-bundling-roots-legacy-stoplight/reference/openapi-2-bundled.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ module.exports = {
1010
"/flight/{id}": {
1111
parameters: [
1212
{
13-
$ref: "#/definitions/Id"
13+
in: "path",
14+
name: "id",
15+
required: true,
16+
type: "number"
1417
}
1518
],
1619
get: {
@@ -152,12 +155,6 @@ module.exports = {
152155
maxLength: 100,
153156
example: "747"
154157
},
155-
Id: {
156-
in: "path",
157-
name: "id",
158-
required: true,
159-
type: "number"
160-
},
161158
Manufacturer: {
162159
definitions: {},
163160
title: "Manufacturer",

test/specs/custom-bundling-roots-legacy-stoplight/reference/openapi-3.bundled.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@ module.exports = {
1414
"/flight/{id}": {
1515
parameters: [
1616
{
17-
$ref: "#/components/schemas/Id"
18-
}
17+
in: "path",
18+
name: "id",
19+
required: true,
20+
type: "number"
21+
},
1922
],
2023
get: {
2124
operationId: "get-flights",
@@ -166,12 +169,6 @@ module.exports = {
166169
maxLength: 100,
167170
example: "747"
168171
},
169-
Id: {
170-
in: "path",
171-
name: "id",
172-
required: true,
173-
type: "number"
174-
},
175172
Manufacturer: {
176173
definitions: {},
177174
title: "Manufacturer",

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

Lines changed: 104 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const { expect } = require("chai");
55
const $RefParser = require("../../..");
66
const path = require("../../utils/path");
77
const setupHttpMocks = require("../../utils/setup-http-mocks");
8-
const { getDefaultsForOldJsonSchema, getDefaultsForOAS2 } = require("../../../lib/bundle/defaults");
8+
const { getDefaultsForOldJsonSchema, getDefaultsForOAS2, getDefaultsForOAS3 } = require("../../../lib/bundle/defaults");
99

1010
describe("Custom bundling roots", () => {
1111
it("mixed inline", async () => {
@@ -420,4 +420,107 @@ describe("Custom bundling roots", () => {
420420
}
421421
});
422422
});
423+
424+
describe("OAS3 defaults", () => {
425+
it("should not touch any other shared component different than schema", async () => {
426+
setupHttpMocks({
427+
"http://localhost:8080/test/Book.json": { name: "Book" },
428+
"http://localhost:8080/test/Address.json": { title: "Address" },
429+
"http://localhost:8080/test/Pets.json": { title: "Pets" },
430+
});
431+
432+
let parser = new $RefParser();
433+
const model = {
434+
openapi: "3.0.0",
435+
paths: {
436+
"/pets": {
437+
get: {
438+
parameters: [
439+
{
440+
$ref: "http://localhost:8080/test/Book.json"
441+
},
442+
{
443+
$ref: "http://localhost:8080/test/Book.json"
444+
},
445+
],
446+
responses: {
447+
200: {
448+
content: {
449+
"application/json": {
450+
schema: {
451+
$ref: "http://localhost:8080/test/Address.json",
452+
}
453+
},
454+
"application/yaml": {
455+
schema: {
456+
$ref: "http://localhost:8080/test/Address.json",
457+
}
458+
}
459+
}
460+
},
461+
400: {
462+
$ref: "http://localhost:8080/test/Pets.json",
463+
},
464+
500: {
465+
$ref: "http://localhost:8080/test/Pets.json",
466+
},
467+
}
468+
}
469+
},
470+
},
471+
};
472+
473+
const schema = await parser.bundle(model, {
474+
bundle: getDefaultsForOAS3(),
475+
});
476+
477+
expect(schema).to.equal(parser.schema);
478+
expect(schema).to.deep.equal({
479+
openapi: "3.0.0",
480+
paths: {
481+
"/pets": {
482+
get: {
483+
parameters: [
484+
{
485+
name: "Book"
486+
},
487+
{
488+
$ref: "#/paths/~1pets/get/parameters/0"
489+
}
490+
],
491+
responses: {
492+
200: {
493+
content: {
494+
"application/json": {
495+
schema: {
496+
$ref: "#/components/schemas/Address"
497+
}
498+
},
499+
"application/yaml": {
500+
schema: {
501+
$ref: "#/components/schemas/Address"
502+
}
503+
}
504+
}
505+
},
506+
400: {
507+
title: "Pets"
508+
},
509+
500: {
510+
$ref: "#/paths/~1pets/get/responses/400"
511+
}
512+
}
513+
}
514+
},
515+
},
516+
components: {
517+
schemas: {
518+
Address: {
519+
title: "Address"
520+
}
521+
},
522+
}
523+
});
524+
});
525+
});
423526
});

0 commit comments

Comments
 (0)