diff --git a/src/dereference-document.test.ts b/src/dereference-document.test.ts index 836d9b6c..277f5d46 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": "./src/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" + } + } +}