From a97dffcbfcfa085e383d3a40e92739ec394358d8 Mon Sep 17 00:00:00 2001 From: hayawata3626 Date: Fri, 24 Oct 2025 16:45:39 +0900 Subject: [PATCH 1/2] feat: add text/csv media type support --- .../src/core/findCompatibleMediaType.test.ts | 110 ++++++++++++++++++ .../src/core/findCompatibleMediaType.ts | 5 +- 2 files changed, 113 insertions(+), 2 deletions(-) create mode 100644 plugins/typescript/src/core/findCompatibleMediaType.test.ts diff --git a/plugins/typescript/src/core/findCompatibleMediaType.test.ts b/plugins/typescript/src/core/findCompatibleMediaType.test.ts new file mode 100644 index 0000000..bf0de3e --- /dev/null +++ b/plugins/typescript/src/core/findCompatibleMediaType.test.ts @@ -0,0 +1,110 @@ +import { describe, expect, it } from "vitest"; +import { findCompatibleMediaType } from "./findCompatibleMediaType"; + +describe("findCompatibleMediaType", () => { + it("should return undefined if content is empty", () => { + expect(findCompatibleMediaType({ content: {} })).toBeUndefined(); + }); + + it("should return MediaTypeObject for application/json", () => { + const mediaTypeObject = { schema: { type: "object" as const } }; + const result = findCompatibleMediaType({ + content: { + "application/json": mediaTypeObject, + }, + }); + expect(result).toBe(mediaTypeObject); + }); + + it("should return MediaTypeObject for application/json with charset", () => { + const mediaTypeObject = { schema: { type: "object" as const } }; + const result = findCompatibleMediaType({ + content: { + "application/json; charset=utf-8": mediaTypeObject, + }, + }); + expect(result).toBe(mediaTypeObject); + }); + + it("should return MediaTypeObject for */*", () => { + const mediaTypeObject = { schema: { type: "string" as const } }; + const result = findCompatibleMediaType({ + content: { + "*/*": mediaTypeObject, + }, + }); + expect(result).toBe(mediaTypeObject); + }); + + it("should return MediaTypeObject for application/octet-stream", () => { + const mediaTypeObject = { + schema: { type: "string" as const, format: "binary" }, + }; + const result = findCompatibleMediaType({ + content: { + "application/octet-stream": mediaTypeObject, + }, + }); + expect(result).toBe(mediaTypeObject); + }); + + it("should return MediaTypeObject for multipart/form-data", () => { + const mediaTypeObject = { + schema: { + type: "object" as const, + properties: { + file: { type: "string" as const, format: "binary" }, + }, + }, + }; + const result = findCompatibleMediaType({ + content: { + "multipart/form-data": mediaTypeObject, + }, + }); + expect(result).toBe(mediaTypeObject); + }); + + it("should return MediaTypeObject for text/csv", () => { + const mediaTypeObject = { schema: { type: "string" as const } }; + const result = findCompatibleMediaType({ + content: { + "text/csv": mediaTypeObject, + }, + }); + expect(result).toBe(mediaTypeObject); + }); + + it("should return MediaTypeObject for text/csv with charset", () => { + const mediaTypeObject = { schema: { type: "string" as const } }; + const result = findCompatibleMediaType({ + content: { + "text/csv; charset=utf-8": mediaTypeObject, + }, + }); + expect(result).toBe(mediaTypeObject); + }); + + it("should return undefined for unsupported media type", () => { + const result = findCompatibleMediaType({ + content: { + "text/html": { schema: { type: "string" as const } }, + }, + }); + expect(result).toBeUndefined(); + }); + + it("should return the first compatible media type when multiple are present", () => { + const jsonMediaType = { schema: { type: "object" as const } }; + const csvMediaType = { schema: { type: "string" as const } }; + const result = findCompatibleMediaType({ + content: { + "text/html": { schema: { type: "string" as const } }, + "application/json": jsonMediaType, + "text/csv": csvMediaType, + }, + }); + expect(result).toBeDefined(); + expect([jsonMediaType, csvMediaType]).toContain(result); + }); +}); diff --git a/plugins/typescript/src/core/findCompatibleMediaType.ts b/plugins/typescript/src/core/findCompatibleMediaType.ts index aaa80eb..5e31d5d 100644 --- a/plugins/typescript/src/core/findCompatibleMediaType.ts +++ b/plugins/typescript/src/core/findCompatibleMediaType.ts @@ -1,4 +1,4 @@ -import { +import type { MediaTypeObject, RequestBodyObject, ResponseObject, @@ -19,7 +19,8 @@ export const findCompatibleMediaType = ( contentType.startsWith("*/*") || contentType.startsWith("application/json") || contentType.startsWith("application/octet-stream") || - contentType.startsWith("multipart/form-data") + contentType.startsWith("multipart/form-data") || + contentType.startsWith("text/csv") ) { return requestBodyOrResponseObject.content[contentType]; } From 6eb353b78257793ebe21526db64cebe1493f273f Mon Sep 17 00:00:00 2001 From: hayawata3626 Date: Mon, 8 Dec 2025 16:49:56 +0900 Subject: [PATCH 2/2] chore: remove as const --- .../src/core/findCompatibleMediaType.test.ts | 41 ++++++++++++------- 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/plugins/typescript/src/core/findCompatibleMediaType.test.ts b/plugins/typescript/src/core/findCompatibleMediaType.test.ts index bf0de3e..be76c85 100644 --- a/plugins/typescript/src/core/findCompatibleMediaType.test.ts +++ b/plugins/typescript/src/core/findCompatibleMediaType.test.ts @@ -1,3 +1,4 @@ +import type { MediaTypeObject } from "openapi3-ts/oas30"; import { describe, expect, it } from "vitest"; import { findCompatibleMediaType } from "./findCompatibleMediaType"; @@ -7,7 +8,7 @@ describe("findCompatibleMediaType", () => { }); it("should return MediaTypeObject for application/json", () => { - const mediaTypeObject = { schema: { type: "object" as const } }; + const mediaTypeObject: MediaTypeObject = { schema: { type: "object" } }; const result = findCompatibleMediaType({ content: { "application/json": mediaTypeObject, @@ -17,7 +18,9 @@ describe("findCompatibleMediaType", () => { }); it("should return MediaTypeObject for application/json with charset", () => { - const mediaTypeObject = { schema: { type: "object" as const } }; + const mediaTypeObject: MediaTypeObject = { + schema: { type: "object" }, + }; const result = findCompatibleMediaType({ content: { "application/json; charset=utf-8": mediaTypeObject, @@ -27,7 +30,9 @@ describe("findCompatibleMediaType", () => { }); it("should return MediaTypeObject for */*", () => { - const mediaTypeObject = { schema: { type: "string" as const } }; + const mediaTypeObject: MediaTypeObject = { + schema: { type: "string" }, + }; const result = findCompatibleMediaType({ content: { "*/*": mediaTypeObject, @@ -37,8 +42,8 @@ describe("findCompatibleMediaType", () => { }); it("should return MediaTypeObject for application/octet-stream", () => { - const mediaTypeObject = { - schema: { type: "string" as const, format: "binary" }, + const mediaTypeObject: MediaTypeObject = { + schema: { type: "string", format: "binary" }, }; const result = findCompatibleMediaType({ content: { @@ -49,11 +54,11 @@ describe("findCompatibleMediaType", () => { }); it("should return MediaTypeObject for multipart/form-data", () => { - const mediaTypeObject = { + const mediaTypeObject: MediaTypeObject = { schema: { - type: "object" as const, + type: "object", properties: { - file: { type: "string" as const, format: "binary" }, + file: { type: "string", format: "binary" }, }, }, }; @@ -66,7 +71,9 @@ describe("findCompatibleMediaType", () => { }); it("should return MediaTypeObject for text/csv", () => { - const mediaTypeObject = { schema: { type: "string" as const } }; + const mediaTypeObject: MediaTypeObject = { + schema: { type: "string" }, + }; const result = findCompatibleMediaType({ content: { "text/csv": mediaTypeObject, @@ -76,7 +83,9 @@ describe("findCompatibleMediaType", () => { }); it("should return MediaTypeObject for text/csv with charset", () => { - const mediaTypeObject = { schema: { type: "string" as const } }; + const mediaTypeObject: MediaTypeObject = { + schema: { type: "string" }, + }; const result = findCompatibleMediaType({ content: { "text/csv; charset=utf-8": mediaTypeObject, @@ -88,18 +97,22 @@ describe("findCompatibleMediaType", () => { it("should return undefined for unsupported media type", () => { const result = findCompatibleMediaType({ content: { - "text/html": { schema: { type: "string" as const } }, + "text/html": { schema: { type: "string" } }, }, }); expect(result).toBeUndefined(); }); it("should return the first compatible media type when multiple are present", () => { - const jsonMediaType = { schema: { type: "object" as const } }; - const csvMediaType = { schema: { type: "string" as const } }; + const jsonMediaType: MediaTypeObject = { + schema: { type: "object" }, + }; + const csvMediaType: MediaTypeObject = { + schema: { type: "string" }, + }; const result = findCompatibleMediaType({ content: { - "text/html": { schema: { type: "string" as const } }, + "text/html": { schema: { type: "string" } }, "application/json": jsonMediaType, "text/csv": csvMediaType, },