From 98a76372bef1e757c07ee3a440439363cc6028df Mon Sep 17 00:00:00 2001 From: Fabien BERNARD Date: Mon, 3 Feb 2020 17:52:05 +0100 Subject: [PATCH] Deal with charset Close #231 --- src/scripts/import-open-api.ts | 14 ++++++++------ src/scripts/tests/import-open-api.test.ts | 16 ++++++++++++++++ 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/src/scripts/import-open-api.ts b/src/scripts/import-open-api.ts index ba01bd34..805aac8c 100644 --- a/src/scripts/import-open-api.ts +++ b/src/scripts/import-open-api.ts @@ -195,12 +195,14 @@ export const getResReqTypes = ( if (isReference(res)) { return getRef(res.$ref); } else { - if (res.content && res.content["application/json"]) { - const schema = res.content["application/json"].schema!; - return resolveValue(schema); - } else if (res.content && res.content["application/octet-stream"]) { - const schema = res.content["application/octet-stream"].schema!; - return resolveValue(schema); + if (res.content) { + for (let contentType of Object.keys(res.content)) { + if (contentType.startsWith("application/json") || contentType.startsWith("application/octet-stream")) { + const schema = res.content[contentType].schema!; + return resolveValue(schema); + } + } + return "void"; } else { return "void"; } diff --git a/src/scripts/tests/import-open-api.test.ts b/src/scripts/tests/import-open-api.test.ts index 9a99afb6..de96cac7 100644 --- a/src/scripts/tests/import-open-api.test.ts +++ b/src/scripts/tests/import-open-api.test.ts @@ -737,6 +737,22 @@ describe("scripts/import-open-api", () => { expect(getResReqTypes(responses)).toEqual("FieldListResponse"); }); + it("should return the type of application/json;charset=utf-8", () => { + const responses: Array<[string, ResponseObject]> = [ + [ + "200", + { + description: "An array of schema fields", + content: { + "application/json;charset=utf-8": { schema: { $ref: "#/components/schemas/FieldListResponse" } }, + }, + }, + ], + ]; + + expect(getResReqTypes(responses)).toEqual("FieldListResponse"); + }); + it("should return the type of application/octet-stream if we don't have application/json response", () => { const responses: Array<[string, ResponseObject]> = [ [