From 49733d674dfb68f00cdd71ca803ab8a054719ae4 Mon Sep 17 00:00:00 2001 From: Patrick McElhaney Date: Wed, 3 Apr 2024 16:04:46 -0400 Subject: [PATCH] configure JSONSchemaFaker to not fail on invalid types --- .changeset/eight-dolls-collect.md | 5 +++++ src/server/response-builder.ts | 2 ++ test/server/response-builder.test.ts | 25 +++++++++++++++++++++++++ 3 files changed, 32 insertions(+) create mode 100644 .changeset/eight-dolls-collect.md diff --git a/.changeset/eight-dolls-collect.md b/.changeset/eight-dolls-collect.md new file mode 100644 index 00000000..779b72d5 --- /dev/null +++ b/.changeset/eight-dolls-collect.md @@ -0,0 +1,5 @@ +--- +"counterfact": patch +--- + +configure JSONSchemaFaker to not fail on invalid types diff --git a/src/server/response-builder.ts b/src/server/response-builder.ts index c730eb4c..70cdc524 100644 --- a/src/server/response-builder.ts +++ b/src/server/response-builder.ts @@ -6,6 +6,8 @@ import type { OpenApiOperation, ResponseBuilder } from "./types.d.ts"; JSONSchemaFaker.option("useExamplesValue", true); JSONSchemaFaker.option("minItems", 0); JSONSchemaFaker.option("maxItems", 20); +JSONSchemaFaker.option("failOnInvalidTypes", false); +JSONSchemaFaker.option("failOnInvalidFormat", false); function convertToXmlIfNecessary( type: string, diff --git a/test/server/response-builder.test.ts b/test/server/response-builder.test.ts index 4e07d424..6aedeae9 100644 --- a/test/server/response-builder.test.ts +++ b/test/server/response-builder.test.ts @@ -151,6 +151,31 @@ describe("a response builder", () => { }, ]); }); + + it("returns 500 if it doesn't know what to do with the status code", () => { + const operationWithInvalidSchema = structuredClone(operation); + + // @ts-expect-error TypeScript can't track the type with structuredClone() + operationWithInvalidSchema.responses[200].content[ + "application/json" + ].schema.type = "file"; + + const response = createResponseBuilder( + operationWithInvalidSchema, + )[200]?.random(); + + expect(response?.status).toBe(200); + expect(response?.content).toStrictEqual([ + { + body: undefined, + type: "application/json", + }, + { + body: "example text response", + type: "text/plain", + }, + ]); + }); }); describe("builds a random response based on an Open API operation object (OpenAPI 2)", () => {