Skip to content

Commit

Permalink
Merge pull request #829 from pmcelhaney/invalid-json-schema
Browse files Browse the repository at this point in the history
configure JSONSchemaFaker to not fail on invalid types
  • Loading branch information
pmcelhaney committed Apr 4, 2024
2 parents c811936 + 49733d6 commit 1ec9616
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/eight-dolls-collect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"counterfact": patch
---

configure JSONSchemaFaker to not fail on invalid types
2 changes: 2 additions & 0 deletions src/server/response-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
25 changes: 25 additions & 0 deletions test/server/response-builder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)", () => {
Expand Down

0 comments on commit 1ec9616

Please sign in to comment.