Skip to content

Commit 5a801bf

Browse files
authored
Merge pull request #11 from hey-api/fix/internal-json-pointer-bundling
fix/internal json pointer bundling
2 parents f05ba07 + d6f8438 commit 5a801bf

File tree

4 files changed

+74
-1
lines changed

4 files changed

+74
-1
lines changed

lib/__tests__/pointer.test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { describe, expect, it } from "vitest";
2+
import { $RefParser } from "..";
3+
import path from "path";
4+
5+
describe("pointer", () => {
6+
it("inlines internal JSON Pointer refs under #/paths/ for OpenAPI bundling", async () => {
7+
const refParser = new $RefParser();
8+
const pathOrUrlOrSchema = path.resolve("lib", "__tests__", "spec", "openapi-paths-ref.json");
9+
const schema = (await refParser.bundle({ pathOrUrlOrSchema })) as any;
10+
11+
// The GET endpoint should have its schema defined inline
12+
const getSchema = schema.paths["/foo"].get.responses["200"].content["application/json"].schema;
13+
expect(getSchema.$ref).toBeUndefined();
14+
expect(getSchema.type).toBe("object");
15+
expect(getSchema.properties.bar.type).toBe("string");
16+
17+
// The POST endpoint should have its schema inlined (copied) instead of a $ref
18+
const postSchema = schema.paths["/foo"].post.responses["200"].content["application/json"].schema;
19+
expect(postSchema.$ref).toBeUndefined();
20+
expect(postSchema.type).toBe("object");
21+
expect(postSchema.properties.bar.type).toBe("string");
22+
23+
// Both schemas should be identical objects
24+
expect(postSchema).toEqual(getSchema);
25+
});
26+
});
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
{
2+
"openapi": "3.1.0",
3+
"info": {
4+
"title": "Sample API",
5+
"version": "1.0.0"
6+
},
7+
"paths": {
8+
"/foo": {
9+
"get": {
10+
"summary": "Get foo",
11+
"responses": {
12+
"200": {
13+
"description": "OK",
14+
"content": {
15+
"application/json": {
16+
"schema": {
17+
"type": "object",
18+
"properties": {
19+
"bar": {
20+
"type": "string"
21+
}
22+
}
23+
}
24+
}
25+
}
26+
}
27+
}
28+
},
29+
"post": {
30+
"summary": "Create foo",
31+
"responses": {
32+
"200": {
33+
"description": "OK",
34+
"content": {
35+
"application/json": {
36+
"schema": {
37+
"$ref": "#/paths/~1foo/get/responses/200/content/application~1json/schema"
38+
}
39+
}
40+
}
41+
}
42+
}
43+
}
44+
}
45+
}
46+
}

lib/bundle.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ function remap(inventory: InventoryEntry[]) {
300300
for (const entry of inventory) {
301301
// console.log('Re-mapping $ref pointer "%s" at %s', entry.$ref.$ref, entry.pathFromRoot);
302302

303-
if (!entry.external) {
303+
if (!entry.external && !entry.hash?.startsWith("#/paths/")) {
304304
// This $ref already resolves to the main JSON Schema file
305305
entry.$ref.$ref = entry.hash;
306306
} else if (entry.file === file && entry.hash === hash) {

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
],
4343
"scripts": {
4444
"build": "rimraf dist && tsc",
45+
"dev": "rimraf dist && tsc --watch",
4546
"lint": "eslint lib",
4647
"prepublishOnly": "yarn build",
4748
"prettier": "prettier --write \"**/*.+(js|jsx|ts|tsx|har||json|css|md)\"",

0 commit comments

Comments
 (0)