Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(openapi-typescript): include location in error messages #1999

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/three-bobcats-mate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"openapi-typescript": patch
---

Improved error messages to contain locations.
51 changes: 22 additions & 29 deletions packages/openapi-typescript/src/lib/redoc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
Source,
type Document,
lintDocument,
type NormalizedProblem,
} from "@redocly/openapi-core";
import { performance } from "node:perf_hooks";
import { Readable } from "node:stream";
Expand Down Expand Up @@ -81,6 +82,25 @@ export async function parseSchema(schema: unknown, { absoluteRef, resolver }: Pa
throw new Error(`Expected string, object, or Buffer. Got ${Array.isArray(schema) ? "Array" : typeof schema}`);
}

function _processProblems(problems: NormalizedProblem[], options: { silent: boolean }) {
if (problems.length) {
let errorMessage: string | undefined = undefined;
for (const problem of problems) {
const problemLocation = problem.location?.[0].pointer;
const problemMessage = problemLocation ? `${problem.message} at ${problemLocation}` : problem.message;
if (problem.severity === "error") {
errorMessage = problemMessage;
error(problemMessage);
} else {
warn(problemMessage, options.silent);
}
}
if (errorMessage) {
throw new Error(errorMessage);
}
}
}

/**
* Validate an OpenAPI schema and flatten into a single schema using Redocly CLI
*/
Expand Down Expand Up @@ -127,20 +147,7 @@ export async function validateAndBundle(
config: options.redoc.styleguide,
externalRefResolver: resolver,
});
if (problems.length) {
let errorMessage: string | undefined = undefined;
for (const problem of problems) {
if (problem.severity === "error") {
errorMessage = problem.message;
error(problem.message);
} else {
warn(problem.message, options.silent);
}
}
if (errorMessage) {
throw new Error(errorMessage);
}
}
_processProblems(problems, options);
debug("Linted schema", "lint", performance.now() - redocLintT);

// 3. bundle
Expand All @@ -150,21 +157,7 @@ export async function validateAndBundle(
dereference: false,
doc: document,
});
if (bundled.problems.length) {
let errorMessage: string | undefined = undefined;
for (const problem of bundled.problems) {
if (problem.severity === "error") {
errorMessage = problem.message;
error(problem.message);
throw new Error(problem.message);
} else {
warn(problem.message, options.silent);
}
}
if (errorMessage) {
throw new Error(errorMessage);
}
}
_processProblems(bundled.problems, options);
debug("Bundled schema", "bundle", performance.now() - redocBundleT);

return bundled.bundle.parsed;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ export function transformSchemaObjectWithComposition(
}
// for any other unexpected type, throw error
if (Array.isArray(schemaObject) || typeof schemaObject !== "object") {
throw new Error(`Expected SchemaObject, received ${Array.isArray(schemaObject) ? "Array" : typeof schemaObject}`);
throw new Error(
`Expected SchemaObject, received ${Array.isArray(schemaObject) ? "Array" : typeof schemaObject} at ${options.path}`,
);
}

/**
Expand Down
30 changes: 30 additions & 0 deletions packages/openapi-typescript/test/invalid.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,34 @@ describe("Invalid schemas", () => {
test("Other missing required fields", async () => {
await expect(() => openapiTS({} as any)).rejects.toThrowError("Unsupported schema format, expected `openapi: 3.x`");
});

test("Unresolved $ref error messages", async () => {
const consoleErrorSpy = vi.spyOn(console, "error").mockImplementation(() => {});

await expect(() =>
openapiTS({
openapi: "3.1",
info: { title: "test", version: "1.0" },
components: {
schemas: {
Pet: {
type: "object",
properties: {
category: { $ref: "#/components/schemas/NonExistingSchema" },
type: { $ref: "#/components/schemas/AnotherSchema" },
},
},
},
},
}),
).rejects.toThrowError("Can't resolve $ref at #/components/schemas/Pet/properties/type");

expect(consoleErrorSpy).toHaveBeenCalledTimes(2);
expect(consoleErrorSpy).toHaveBeenCalledWith(
expect.stringContaining("Can't resolve $ref at #/components/schemas/Pet/properties/category"),
);
expect(consoleErrorSpy).toHaveBeenCalledWith(
expect.stringContaining("Can't resolve $ref at #/components/schemas/Pet/properties/type"),
);
});
});