Skip to content

Commit

Permalink
fix: default response should be used as a fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
TimPetricola authored and astahmer committed Nov 21, 2023
1 parent 5f10205 commit b9b4772
Show file tree
Hide file tree
Showing 11 changed files with 24 additions and 20 deletions.
5 changes: 5 additions & 0 deletions .changeset/silly-spiders-switch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"typed-openapi": minor
---

Fix default response behavior (only use "default" as a fallback)
19 changes: 9 additions & 10 deletions packages/typed-openapi/src/map-openapi-endpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,17 +103,16 @@ export const mapOpenApiEndpoints = (doc: OpenAPIObject) => {
endpoint.parameters = Object.keys(params).length ? (params as any as EndpointParameters) : undefined;
}

let responseObject;
// Match the default response or first 2xx-3xx response found
if (operation.responses.default) {
// Match the first 2xx-3xx response found, or fallback to default one otherwise
let responseObject: ResponseObject | undefined;
Object.entries(operation.responses).map(([status, responseOrRef]) => {
const statusCode = Number(status);
if (statusCode >= 200 && statusCode < 300) {
responseObject = refs.unwrap<ResponseObject>(responseOrRef);
}
});
if (!responseObject && operation.responses.default) {
responseObject = refs.unwrap(operation.responses.default);
} else {
Object.entries(operation.responses).map(([status, responseOrRef]) => {
const statusCode = Number(status);
if (statusCode >= 200 && statusCode < 300) {
responseObject = refs.unwrap<ResponseObject>(responseOrRef);
}
});
}

const content = responseObject?.content;
Expand Down
2 changes: 1 addition & 1 deletion packages/typed-openapi/tests/generator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ describe("generator", () => {
parameters: {
body: Array<User>;
};
response: unknown;
response: Schemas.User;
};
export type get_LoginUser = {
method: "GET";
Expand Down
4 changes: 2 additions & 2 deletions packages/typed-openapi/tests/map-openapi-endpoints.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2196,8 +2196,8 @@ describe("map-openapi-endpoints", () => {
},
"path": "/user/createWithList",
"response": {
"type": "keyword",
"value": "unknown",
"type": "ref",
"value": "User",
},
},
{
Expand Down
2 changes: 1 addition & 1 deletion packages/typed-openapi/tests/snapshots/petstore.arktype.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ export const types = scope({
parameters: {
body: "User[]",
},
response: "unknown",
response: "User",
},
get_LoginUser: {
method: '"GET"',
Expand Down
2 changes: 1 addition & 1 deletion packages/typed-openapi/tests/snapshots/petstore.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ export namespace Endpoints {
parameters: {
body: Array<User>;
};
response: unknown;
response: Schemas.User;
};
export type get_LoginUser = {
method: "GET";
Expand Down
2 changes: 1 addition & 1 deletion packages/typed-openapi/tests/snapshots/petstore.io-ts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ export const post_CreateUsersWithListInput = t.type({
parameters: t.type({
body: t.array(User),
}),
response: t.unknown,
response: User,
});

export type get_LoginUser = t.TypeOf<typeof get_LoginUser>;
Expand Down
2 changes: 1 addition & 1 deletion packages/typed-openapi/tests/snapshots/petstore.typebox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ export const post_CreateUsersWithListInput = Type.Object({
parameters: Type.Object({
body: Type.Array(User),
}),
response: Type.Unknown(),
response: User,
});

export type get_LoginUser = Static<typeof get_LoginUser>;
Expand Down
2 changes: 1 addition & 1 deletion packages/typed-openapi/tests/snapshots/petstore.valibot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ export const post_CreateUsersWithListInput = v.object({
parameters: v.object({
body: v.array(User),
}),
response: v.unknown(),
response: User,
});

export type get_LoginUser = v.Output<typeof get_LoginUser>;
Expand Down
2 changes: 1 addition & 1 deletion packages/typed-openapi/tests/snapshots/petstore.yup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ export const post_CreateUsersWithListInput = {
parameters: y.object({
body: y.array(User),
}),
response: y.mixed((value): value is any => true).required() as y.MixedSchema<unknown>,
response: User,
};

export type get_LoginUser = typeof get_LoginUser;
Expand Down
2 changes: 1 addition & 1 deletion packages/typed-openapi/tests/snapshots/petstore.zod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ export const post_CreateUsersWithListInput = {
parameters: z.object({
body: z.array(User),
}),
response: z.unknown(),
response: User,
};

export type get_LoginUser = typeof get_LoginUser;
Expand Down

0 comments on commit b9b4772

Please sign in to comment.