Skip to content

Commit 536dc4d

Browse files
authored
feat: add text/csv media type support (#321)
* feat: add text/csv media type support * chore: remove as const
1 parent 9ff319c commit 536dc4d

File tree

2 files changed

+126
-2
lines changed

2 files changed

+126
-2
lines changed
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
import type { MediaTypeObject } from "openapi3-ts/oas30";
2+
import { describe, expect, it } from "vitest";
3+
import { findCompatibleMediaType } from "./findCompatibleMediaType";
4+
5+
describe("findCompatibleMediaType", () => {
6+
it("should return undefined if content is empty", () => {
7+
expect(findCompatibleMediaType({ content: {} })).toBeUndefined();
8+
});
9+
10+
it("should return MediaTypeObject for application/json", () => {
11+
const mediaTypeObject: MediaTypeObject = { schema: { type: "object" } };
12+
const result = findCompatibleMediaType({
13+
content: {
14+
"application/json": mediaTypeObject,
15+
},
16+
});
17+
expect(result).toBe(mediaTypeObject);
18+
});
19+
20+
it("should return MediaTypeObject for application/json with charset", () => {
21+
const mediaTypeObject: MediaTypeObject = {
22+
schema: { type: "object" },
23+
};
24+
const result = findCompatibleMediaType({
25+
content: {
26+
"application/json; charset=utf-8": mediaTypeObject,
27+
},
28+
});
29+
expect(result).toBe(mediaTypeObject);
30+
});
31+
32+
it("should return MediaTypeObject for */*", () => {
33+
const mediaTypeObject: MediaTypeObject = {
34+
schema: { type: "string" },
35+
};
36+
const result = findCompatibleMediaType({
37+
content: {
38+
"*/*": mediaTypeObject,
39+
},
40+
});
41+
expect(result).toBe(mediaTypeObject);
42+
});
43+
44+
it("should return MediaTypeObject for application/octet-stream", () => {
45+
const mediaTypeObject: MediaTypeObject = {
46+
schema: { type: "string", format: "binary" },
47+
};
48+
const result = findCompatibleMediaType({
49+
content: {
50+
"application/octet-stream": mediaTypeObject,
51+
},
52+
});
53+
expect(result).toBe(mediaTypeObject);
54+
});
55+
56+
it("should return MediaTypeObject for multipart/form-data", () => {
57+
const mediaTypeObject: MediaTypeObject = {
58+
schema: {
59+
type: "object",
60+
properties: {
61+
file: { type: "string", format: "binary" },
62+
},
63+
},
64+
};
65+
const result = findCompatibleMediaType({
66+
content: {
67+
"multipart/form-data": mediaTypeObject,
68+
},
69+
});
70+
expect(result).toBe(mediaTypeObject);
71+
});
72+
73+
it("should return MediaTypeObject for text/csv", () => {
74+
const mediaTypeObject: MediaTypeObject = {
75+
schema: { type: "string" },
76+
};
77+
const result = findCompatibleMediaType({
78+
content: {
79+
"text/csv": mediaTypeObject,
80+
},
81+
});
82+
expect(result).toBe(mediaTypeObject);
83+
});
84+
85+
it("should return MediaTypeObject for text/csv with charset", () => {
86+
const mediaTypeObject: MediaTypeObject = {
87+
schema: { type: "string" },
88+
};
89+
const result = findCompatibleMediaType({
90+
content: {
91+
"text/csv; charset=utf-8": mediaTypeObject,
92+
},
93+
});
94+
expect(result).toBe(mediaTypeObject);
95+
});
96+
97+
it("should return undefined for unsupported media type", () => {
98+
const result = findCompatibleMediaType({
99+
content: {
100+
"text/html": { schema: { type: "string" } },
101+
},
102+
});
103+
expect(result).toBeUndefined();
104+
});
105+
106+
it("should return the first compatible media type when multiple are present", () => {
107+
const jsonMediaType: MediaTypeObject = {
108+
schema: { type: "object" },
109+
};
110+
const csvMediaType: MediaTypeObject = {
111+
schema: { type: "string" },
112+
};
113+
const result = findCompatibleMediaType({
114+
content: {
115+
"text/html": { schema: { type: "string" } },
116+
"application/json": jsonMediaType,
117+
"text/csv": csvMediaType,
118+
},
119+
});
120+
expect(result).toBeDefined();
121+
expect([jsonMediaType, csvMediaType]).toContain(result);
122+
});
123+
});

plugins/typescript/src/core/findCompatibleMediaType.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {
1+
import type {
22
MediaTypeObject,
33
RequestBodyObject,
44
ResponseObject,
@@ -19,7 +19,8 @@ export const findCompatibleMediaType = (
1919
contentType.startsWith("*/*") ||
2020
contentType.startsWith("application/json") ||
2121
contentType.startsWith("application/octet-stream") ||
22-
contentType.startsWith("multipart/form-data")
22+
contentType.startsWith("multipart/form-data") ||
23+
contentType.startsWith("text/csv")
2324
) {
2425
return requestBodyOrResponseObject.content[contentType];
2526
}

0 commit comments

Comments
 (0)