Skip to content

Commit 26d551a

Browse files
authored
fix: response body (#127)
1 parent 3a721bd commit 26d551a

File tree

4 files changed

+76
-143
lines changed

4 files changed

+76
-143
lines changed

src/internal/OpenApiTools/Extractor.ts

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@ const extractPickedParameter = (parameter: OpenApi.Parameter): CodeGenerator.Pic
1414

1515
const extractResponseNamesByStatusCode = (type: "success" | "error", responses: { [statusCode: string]: OpenApi.Response }): string[] => {
1616
const statusCodeList: string[] = [];
17-
Object.entries(responses || {}).forEach(([statusCodeLike, response]) => {
17+
for (const [statusCodeLike, response] of Object.entries(responses || {})) {
18+
const hasValidMediaType = Object.values(response.content || {}).filter(mediaType => Object.values(mediaType).length > 0).length > 0;
1819
// ContentTypeの定義が存在しない場合はstatusCodeを読み取らない
19-
if (Object.keys(response.content || {}).length === 0) {
20-
return;
20+
if (!hasValidMediaType) {
21+
continue;
2122
}
2223
if (typeof statusCodeLike === "string") {
2324
const statusCodeNumberValue = parseInt(statusCodeLike, 10);
@@ -31,7 +32,7 @@ const extractResponseNamesByStatusCode = (type: "success" | "error", responses:
3132
}
3233
}
3334
}
34-
});
35+
}
3536
return statusCodeList;
3637
};
3738

@@ -46,11 +47,21 @@ const getRequestContentTypeList = (requestBody: OpenApi.RequestBody): string[] =
4647
};
4748

4849
const getSuccessResponseContentTypeList = (responses: { [statusCode: string]: OpenApi.Response }): string[] => {
49-
let contentTypeList: string[] = [];
50-
extractResponseNamesByStatusCode("success", responses).forEach(statusCode => {
50+
const contentTypeList: string[] = [];
51+
for (const statusCode of extractResponseNamesByStatusCode("success", responses)) {
5152
const response = responses[statusCode];
52-
contentTypeList = contentTypeList.concat(Object.keys(response.content || {}));
53-
});
53+
/**
54+
* responses:
55+
* 200:
56+
* content:
57+
* application/json: {}
58+
*/
59+
for (const [key, mediaType] of Object.entries(response.content || {})) {
60+
if (Object.values(mediaType).length > 0) {
61+
contentTypeList.push(key);
62+
}
63+
}
64+
}
5465
return Array.from(new Set(contentTypeList));
5566
};
5667

@@ -68,9 +79,9 @@ export const generateCodeGeneratorParamsArray = (
6879
): CodeGenerator.Params[] => {
6980
const operationState = store.getNoReferenceOperationState();
7081
const params: CodeGenerator.Params[] = [];
71-
Object.entries(operationState).forEach(([operationId, item]) => {
82+
for (const [operationId, item] of Object.entries(operationState)) {
7283
if (allowOperationIds && !allowOperationIds.includes(operationId)) {
73-
return;
84+
continue;
7485
}
7586
const responseSuccessNames = extractResponseNamesByStatusCode("success", item.responses).map(statusCode =>
7687
converterContext.generateResponseName(operationId, statusCode),
@@ -114,7 +125,7 @@ export const generateCodeGeneratorParamsArray = (
114125
operationParams: item,
115126
};
116127
params.push(formatParams);
117-
});
128+
}
118129

119130
return params;
120131
};

test/__tests__/class/__snapshots__/cloudflare-test.ts.snap

Lines changed: 18 additions & 44 deletions
Large diffs are not rendered by default.

test/__tests__/currying-functional/__snapshots__/coudflare-test.ts.snap

Lines changed: 18 additions & 44 deletions
Large diffs are not rendered by default.

test/__tests__/functional/__snapshots__/coudflare-test.ts.snap

Lines changed: 18 additions & 44 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)