From 11b4b90fae52508ed608dd2677534d6716798ef4 Mon Sep 17 00:00:00 2001 From: Shane Jonas Date: Fri, 18 Mar 2022 17:59:48 -0700 Subject: [PATCH 1/2] fix: show failing deref nested file --- src/dereference-document.test.ts | 63 ++++++++++++++++++++++++---- src/test-fixtures/method.json | 8 ++++ src/test-fixtures/nested-schema.json | 8 ++++ 3 files changed, 72 insertions(+), 7 deletions(-) create mode 100644 src/test-fixtures/method.json create mode 100644 src/test-fixtures/nested-schema.json diff --git a/src/dereference-document.test.ts b/src/dereference-document.test.ts index 836d9b6c..2078bbab 100644 --- a/src/dereference-document.test.ts +++ b/src/dereference-document.test.ts @@ -43,13 +43,13 @@ describe("dereferenceDocument", () => { ...workingDocument, "x-methods": { foobar: { - name: "foobar", - params: [], - result: { - name: "abcfoo", - schema: { type: "number" } - } - } + name: "foobar", + params: [], + result: { + name: "abcfoo", + schema: { type: "number" } + } + } }, components: { schemas: { @@ -330,6 +330,55 @@ describe("dereferenceDocument", () => { expect(result.methods[0].result.schema.type).toBe("string") }); + it("works with ref to a nested file", async () => { + expect.assertions(2); + + const testDoc = { + openrpc: "1.2.4", + info: { + title: "foo", + version: "1", + }, + methods: [ + { + name: "foo", + params: [], + result: { + name: "fooResult", + schema: { $ref: `${__dirname}/test-fixtures/nested-schema.json` } + } + } + ] + }; + + const result = await dereferenceDocument(testDoc as OpenrpcDocument, defaultResolver) as any; + + expect(result.methods[0].result.schema.type).toBe("object") + expect(result.methods[0].result.schema.properties.foo.type).toBe("string") + }); + + it("works with ref to a double nested file", async () => { + expect.assertions(2); + + const testDoc = { + openrpc: "1.2.4", + info: { + title: "foo", + version: "1", + }, + methods: [ + { + "$ref": "./test-fixtures/method.json", + } + ] + }; + + const result = await dereferenceDocument(testDoc as OpenrpcDocument, defaultResolver) as any; + + expect(result.methods[0].result.schema.type).toBe("object") + expect(result.methods[0].result.schema.properties.foo.type).toBe("string") + }); + it("works with schema that makes ref to a schema from components", async () => { expect.assertions(1); diff --git a/src/test-fixtures/method.json b/src/test-fixtures/method.json new file mode 100644 index 00000000..8b3f97ff --- /dev/null +++ b/src/test-fixtures/method.json @@ -0,0 +1,8 @@ +{ + "name": "foo", + "params": [], + "result": { + "name": "fooResult", + "schema": { "$ref": "./nested-schema.json" } + } +} diff --git a/src/test-fixtures/nested-schema.json b/src/test-fixtures/nested-schema.json new file mode 100644 index 00000000..36ead0a3 --- /dev/null +++ b/src/test-fixtures/nested-schema.json @@ -0,0 +1,8 @@ +{ + "type": "object", + "properties": { + "foo": { + "$ref": "./good-schema.json" + } + } +} From fa5250a83f6a847fcd49385de74207881e755472 Mon Sep 17 00:00:00 2001 From: Shane Jonas Date: Fri, 18 Mar 2022 18:25:46 -0700 Subject: [PATCH 2/2] fix: added src to start of $ref for methods --- src/dereference-document.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/dereference-document.test.ts b/src/dereference-document.test.ts index 2078bbab..277f5d46 100644 --- a/src/dereference-document.test.ts +++ b/src/dereference-document.test.ts @@ -368,7 +368,7 @@ describe("dereferenceDocument", () => { }, methods: [ { - "$ref": "./test-fixtures/method.json", + "$ref": "./src/test-fixtures/method.json", } ] };