Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: response body #127

Merged
merged 2 commits into from
Feb 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 22 additions & 11 deletions src/internal/OpenApiTools/Extractor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ const extractPickedParameter = (parameter: OpenApi.Parameter): CodeGenerator.Pic

const extractResponseNamesByStatusCode = (type: "success" | "error", responses: { [statusCode: string]: OpenApi.Response }): string[] => {
const statusCodeList: string[] = [];
Object.entries(responses || {}).forEach(([statusCodeLike, response]) => {
for (const [statusCodeLike, response] of Object.entries(responses || {})) {
const hasValidMediaType = Object.values(response.content || {}).filter(mediaType => Object.values(mediaType).length > 0).length > 0;
// ContentTypeの定義が存在しない場合はstatusCodeを読み取らない
if (Object.keys(response.content || {}).length === 0) {
return;
if (!hasValidMediaType) {
continue;
}
if (typeof statusCodeLike === "string") {
const statusCodeNumberValue = parseInt(statusCodeLike, 10);
Expand All @@ -31,7 +32,7 @@ const extractResponseNamesByStatusCode = (type: "success" | "error", responses:
}
}
}
});
}
return statusCodeList;
};

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

const getSuccessResponseContentTypeList = (responses: { [statusCode: string]: OpenApi.Response }): string[] => {
let contentTypeList: string[] = [];
extractResponseNamesByStatusCode("success", responses).forEach(statusCode => {
const contentTypeList: string[] = [];
for (const statusCode of extractResponseNamesByStatusCode("success", responses)) {
const response = responses[statusCode];
contentTypeList = contentTypeList.concat(Object.keys(response.content || {}));
});
/**
* responses:
* 200:
* content:
* application/json: {}
*/
for (const [key, mediaType] of Object.entries(response.content || {})) {
if (Object.values(mediaType).length > 0) {
contentTypeList.push(key);
}
}
}
return Array.from(new Set(contentTypeList));
};

Expand All @@ -68,9 +79,9 @@ export const generateCodeGeneratorParamsArray = (
): CodeGenerator.Params[] => {
const operationState = store.getNoReferenceOperationState();
const params: CodeGenerator.Params[] = [];
Object.entries(operationState).forEach(([operationId, item]) => {
for (const [operationId, item] of Object.entries(operationState)) {
if (allowOperationIds && !allowOperationIds.includes(operationId)) {
return;
continue;
}
const responseSuccessNames = extractResponseNamesByStatusCode("success", item.responses).map(statusCode =>
converterContext.generateResponseName(operationId, statusCode),
Expand Down Expand Up @@ -114,7 +125,7 @@ export const generateCodeGeneratorParamsArray = (
operationParams: item,
};
params.push(formatParams);
});
}

return params;
};
62 changes: 18 additions & 44 deletions test/__tests__/class/__snapshots__/cloudflare-test.ts.snap

Large diffs are not rendered by default.

Large diffs are not rendered by default.

62 changes: 18 additions & 44 deletions test/__tests__/functional/__snapshots__/coudflare-test.ts.snap

Large diffs are not rendered by default.

Loading