Skip to content

Commit f1f0e89

Browse files
committed
feat: support any response type with media type containing application + json
1 parent 54b7692 commit f1f0e89

File tree

3 files changed

+56
-1
lines changed

3 files changed

+56
-1
lines changed

.changeset/seven-taxis-remain.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"typed-openapi": patch
3+
---
4+
5+
Allow any response type with media type containing application + json

packages/typed-openapi/src/map-openapi-endpoints.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ const isAllowedParamMediaTypes = (
233233
allowedParamMediaTypes.includes(mediaType as any) ||
234234
mediaType.includes("text/");
235235

236-
const isResponseMediaType = (mediaType: string) => mediaType === "application/json" || mediaType === "*/*";
236+
const isResponseMediaType = (mediaType: string) => mediaType === "*/*" || (mediaType.includes("application/") && mediaType.includes("json"));
237237
const getAlias = ({ path, method, operation }: Endpoint) =>
238238
sanitizeName(
239239
(method + "_" + capitalize(operation.operationId ?? pathToVariableName(path))).replace(/-/g, "__"),
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { describe, test } from "vitest";
2+
import { mapOpenApiEndpoints } from "../src/map-openapi-endpoints.ts";
3+
import type { OpenAPIObject } from "openapi3-ts/oas31";
4+
5+
const openApiDoc: OpenAPIObject = {
6+
openapi: "3.0.3",
7+
info: { title: "Test API Spec", version: "1.0.0" },
8+
paths: {
9+
"/test": {
10+
get: {
11+
operationId: "getTest",
12+
responses: {
13+
"200": {
14+
content: {
15+
"application/vnd.github.v3.star+json": {
16+
schema: {
17+
type: "array",
18+
items: { $ref: "#/components/schemas/Response" },
19+
},
20+
},
21+
},
22+
},
23+
},
24+
},
25+
},
26+
},
27+
components: {
28+
schemas: {
29+
Response: {
30+
type: "object",
31+
properties: {
32+
id: { type: "integer" },
33+
name: { type: "string" },
34+
},
35+
},
36+
},
37+
},
38+
};
39+
40+
describe("issue-52: response of content type '*/*'", () => {
41+
test("should not resolve response as unknown", ({ expect }) => {
42+
const result = mapOpenApiEndpoints(openApiDoc);
43+
// Find the endpoint by alias (see getAlias logic: 'get_GetTest')
44+
const endpoint = result.endpointList.find(e => e.meta.alias === "get_GetTest");
45+
expect(endpoint).toBeDefined();
46+
// The bug: endpoint.response is 'unknown' instead of the correct type
47+
expect(endpoint?.response).not.toMatchObject({ type: "keyword", value: "unknown" });
48+
expect(endpoint?.response).toMatchObject({ type: "array", value: "Array<Response>" });
49+
});
50+
});

0 commit comments

Comments
 (0)