Skip to content

Commit

Permalink
fix(#178): handle $ref in response object
Browse files Browse the repository at this point in the history
  • Loading branch information
jbach authored and astahmer committed Jul 25, 2023
1 parent c9118af commit d897485
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/quick-jeans-hope.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"openapi-zod-client": minor
---

handle $ref in responses object
4 changes: 3 additions & 1 deletion lib/src/getZodiosEndpointDefinitionList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,9 @@ export const getZodiosEndpointDefinitionList = (doc: OpenAPIObject, options?: Te
}

for (const statusCode in operation.responses) {
const responseItem = operation.responses[statusCode] as ResponseObject;
const responseItem = (
isReferenceObject(operation.responses[statusCode]) ? ctx.resolver.getSchemaByRef(operation.responses[statusCode].$ref) : operation.responses[statusCode]
) as ResponseObject;

const mediaTypes = Object.keys(responseItem.content ?? {});
const matchingMediaType = mediaTypes.find(isMediaTypeAllowed);
Expand Down
59 changes: 59 additions & 0 deletions lib/tests/resolve-ref-responses.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { getZodiosEndpointDefinitionList } from "../src";
import { expect, test } from "vitest";

test("resolve-ref-responses", () => {
// Without the refiner function passed.
expect(
getZodiosEndpointDefinitionList({
openapi: "3.0.3",
info: { version: "1", title: "Example API" },
paths: {
"/": {
get: {
operationId: "getExample",
responses: {
"200": {
$ref: "#/components/responses/ExampleResponse"
},
},
},
},
},
components: {
responses: {
ExampleResponse: {
description: "example response",
content: { "application/json": { schema: { type: "string" } } },
}
}
},
})
).toMatchInlineSnapshot(`
{
"deepDependencyGraph": {},
"endpoints": [
{
"description": undefined,
"errors": [],
"method": "get",
"parameters": [],
"path": "/",
"requestFormat": "json",
"response": "z.string()",
},
],
"issues": {
"ignoredFallbackResponse": [],
"ignoredGenericError": [],
},
"refsDependencyGraph": {},
"resolver": {
"getSchemaByRef": [Function],
"resolveRef": [Function],
"resolveSchemaName": [Function],
},
"schemaByName": {},
"zodSchemaByName": {},
}
`);
});

1 comment on commit d897485

@vercel
Copy link

@vercel vercel bot commented on d897485 Jul 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.