Skip to content

Commit

Permalink
fix: use default reference resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
BelfordZ committed May 9, 2024
1 parent 1ca064e commit 038f969
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 25 deletions.
40 changes: 20 additions & 20 deletions src/dereference-document.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe("dereferenceDocument", () => {

it("doesnt explode", async () => {
expect.assertions(1);
const document = await dereferenceDocument(workingDocument, defaultResolver);
const document = await dereferenceDocument(workingDocument);
expect(document.methods).toBeDefined();
});

Expand All @@ -33,7 +33,7 @@ describe("dereferenceDocument", () => {
{ name: "abc", params: [], result: { name: "cba", schema: { type: "number" } } }
],
openrpc: "1.0.0-rc1",
}, defaultResolver);
});
expect(document.methods).toBeDefined();
});

Expand All @@ -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: {
Expand Down Expand Up @@ -102,9 +102,9 @@ describe("dereferenceDocument", () => {
schema: { $ref: "#/components/schemas/bigOlFoo" }
}
});
testDoc.methods.push({"$ref":"#/x-methods/foobar"})
testDoc.methods.push({ "$ref": "#/x-methods/foobar" })

const document = await dereferenceDocument(testDoc, defaultResolver);
const document = await dereferenceDocument(testDoc);
const docMethods = document.methods as MethodObject[];
expect(docMethods).toBeDefined();
expect(docMethods[0]).toBeDefined();
Expand Down Expand Up @@ -158,7 +158,7 @@ describe("dereferenceDocument", () => {
}
} as OpenrpcDocument;

const document = await dereferenceDocument(testDoc, defaultResolver);
const document = await dereferenceDocument(testDoc);
expect(document.methods).toBeDefined();
expect(document.methods[0]).toBeDefined();

Expand Down Expand Up @@ -204,7 +204,7 @@ describe("dereferenceDocument", () => {
};

try {
await dereferenceDocument(testDoc as OpenrpcDocument, defaultResolver)
await dereferenceDocument(testDoc as OpenrpcDocument)
} catch (e) {
expect(e).toBeInstanceOf(OpenRPCDocumentDereferencingError);
}
Expand All @@ -231,7 +231,7 @@ describe("dereferenceDocument", () => {
};

try {
await dereferenceDocument(testDoc as OpenrpcDocument, defaultResolver)
await dereferenceDocument(testDoc as OpenrpcDocument)
} catch (e) {
expect(e).toBeInstanceOf(OpenRPCDocumentDereferencingError);
}
Expand Down Expand Up @@ -261,7 +261,7 @@ describe("dereferenceDocument", () => {
};

try {
await dereferenceDocument(testDoc as OpenrpcDocument, defaultResolver)
await dereferenceDocument(testDoc as OpenrpcDocument)
} catch (e) {
expect(e).toBeInstanceOf(OpenRPCDocumentDereferencingError);
}
Expand Down Expand Up @@ -299,7 +299,7 @@ describe("dereferenceDocument", () => {
}
};

const result = await dereferenceDocument(testDoc as OpenrpcDocument, defaultResolver) as any;
const result = await dereferenceDocument(testDoc as OpenrpcDocument) as any;

Check warning on line 302 in src/dereference-document.test.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type

expect(result.methods[0].links[0]).toBe(testDoc.components.links.fooLink)
});
Expand All @@ -325,7 +325,7 @@ describe("dereferenceDocument", () => {
]
};

const result = await dereferenceDocument(testDoc as OpenrpcDocument, defaultResolver) as any;
const result = await dereferenceDocument(testDoc as OpenrpcDocument) as any;

Check warning on line 328 in src/dereference-document.test.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type

expect(result.methods[0].result.schema.type).toBe("string")
});
Expand Down Expand Up @@ -361,12 +361,12 @@ describe("dereferenceDocument", () => {
}
};

const result = await dereferenceDocument(testDoc as OpenrpcDocument, defaultResolver) as any;
const result = await dereferenceDocument(testDoc as OpenrpcDocument) as any;

Check warning on line 364 in src/dereference-document.test.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type

expect(result.methods[0].result.schema.properties.foo).toBe(result.components.schemas.foo);
});

it("throws when a schema cannot be resolved from componnets", async () => {
it("throws when a schema cannot be resolved from components", async () => {
expect.assertions(1);

const testDoc = {
Expand Down Expand Up @@ -399,7 +399,7 @@ describe("dereferenceDocument", () => {
};

try {
await dereferenceDocument(testDoc as OpenrpcDocument, defaultResolver) as any;
await dereferenceDocument(testDoc as OpenrpcDocument) as any;

Check warning on line 402 in src/dereference-document.test.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
} catch (e) {
expect(e).toBeInstanceOf(OpenRPCDocumentDereferencingError);
}
Expand Down
10 changes: 5 additions & 5 deletions src/dereference-document.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Dereferencer from "@json-schema-tools/dereferencer";
import { OpenrpcDocument as OpenRPC, ReferenceObject, ExamplePairingObject, JSONSchema, SchemaComponents, ContentDescriptorComponents, ContentDescriptorObject, OpenrpcDocument, MethodObject, MethodOrReference } from "@open-rpc/meta-schema";
import referenceResolver from "@json-schema-tools/reference-resolver";
import safeStringify from "fast-safe-stringify";
import safeStringify from "fast-safe-stringify";

export type ReferenceResolver = typeof referenceResolver
/**
Expand Down Expand Up @@ -118,8 +118,8 @@ const handleSchemasInsideContentDescriptorComponents = async (doc: OpenrpcDocume
const handleMethod = async (methodOrRef: MethodOrReference, doc: OpenrpcDocument, resolver: ReferenceResolver): Promise<MethodObject> => {
let method = methodOrRef as MethodObject;

if(methodOrRef.$ref !== undefined){
method = await derefItem({$ref: methodOrRef.$ref}, doc, resolver)
if (methodOrRef.$ref !== undefined) {
method = await derefItem({ $ref: methodOrRef.$ref }, doc, resolver)
}

if (method.tags !== undefined) {
Expand Down Expand Up @@ -187,9 +187,9 @@ const handleMethod = async (methodOrRef: MethodOrReference, doc: OpenrpcDocument
* ```
*
*/
export default async function dereferenceDocument(openrpcDocument: OpenRPC, resolver: ReferenceResolver): Promise<OpenRPC> {
export default async function dereferenceDocument(openrpcDocument: OpenRPC, resolver: ReferenceResolver = referenceResolver): Promise<OpenRPC> {
let derefDoc = { ...openrpcDocument };

derefDoc = await handleSchemaComponents(derefDoc);
derefDoc = await handleSchemasInsideContentDescriptorComponents(derefDoc);
const methods = [] as any;

Check warning on line 195 in src/dereference-document.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
Expand Down

0 comments on commit 038f969

Please sign in to comment.