Skip to content

Commit

Permalink
fix: type
Browse files Browse the repository at this point in the history
  • Loading branch information
Himenon committed Feb 11, 2024
1 parent 7f50512 commit f8a8036
Show file tree
Hide file tree
Showing 7 changed files with 417 additions and 1,545 deletions.
8 changes: 7 additions & 1 deletion src/internal/OpenApiTools/Extractor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,13 @@ const extractResponseNamesByStatusCode = (type: "success" | "error", responses:
};

const getRequestContentTypeList = (requestBody: OpenApi.RequestBody): string[] => {
return Object.keys(requestBody.content);
return Object.entries(requestBody.content).reduce<string[]>((list, [key, mediaType]) => {
const hasValidContent = Object.values(mediaType).length > 0;
if (hasValidContent) {
return list.concat(key);
}
return list;
}, []);
};

const getSuccessResponseContentTypeList = (responses: { [statusCode: string]: OpenApi.Response }): string[] => {
Expand Down
5 changes: 4 additions & 1 deletion src/internal/OpenApiTools/Walker/Operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,15 @@ export const create = (rootSchema: OpenApi.Document): State => {
}
const parameters = [...(pathItem.parameters || []), ...(operation.parameters || [])] as OpenApi.Parameter[];

const requestBody = operation.requestBody as OpenApi.RequestBody | undefined;
const hasRequestBody = requestBody ? Object.values(requestBody.content).filter(Boolean).length > 0 : false;

state[operation.operationId] = {
httpMethod,
requestUri,
comment: [operation.summary, operation.description].filter(Boolean).join(EOL),
deprecated: !!operation.deprecated,
requestBody: operation.requestBody as OpenApi.RequestBody,
requestBody: hasRequestBody ? requestBody : undefined,
parameters: uniqParameters(parameters),
responses: operation.responses as CodeGenerator.OpenApiResponses,
};
Expand Down
21 changes: 15 additions & 6 deletions src/internal/OpenApiTools/components/Operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,21 @@ export const generateStatements = (
});
}
} else {
statements.push(
RequestBody.generateInterface(entryPoint, currentPoint, factory, requestBodyName, operation.requestBody, context, converterContext),
);
store.updateOperationState(httpMethod, requestUri, operationId, {
requestBodyName: requestBodyName,
});
/**
* requestBody:
* content:
* application/json: {}
*/
const hasValidMediaType =
Object.values(operation.requestBody.content).filter(mediaType => Object.values(mediaType).length > 0).length > 0;
if (hasValidMediaType) {
statements.push(
RequestBody.generateInterface(entryPoint, currentPoint, factory, requestBodyName, operation.requestBody, context, converterContext),
);
store.updateOperationState(httpMethod, requestUri, operationId, {
requestBodyName: requestBodyName,
});
}
}
}

Expand Down
8 changes: 7 additions & 1 deletion src/internal/OpenApiTools/components/RequestBody.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,17 @@ export const generateInterface = (
context: ToTypeNode.Context,
converterContext: ConverterContext.Types,
): ts.InterfaceDeclaration => {
/**
* requestBody:
* content:
* application/json: {}
*/
const hasValidMediaType = Object.values(requestBody.content).filter(mediaType => Object.values(mediaType).length > 0).length > 0;
const contentSignatures = MediaType.generatePropertySignatures(
entryPoint,
currentPoint,
factory,
requestBody.content || {},
hasValidMediaType ? requestBody.content : {},
context,
converterContext,
);
Expand Down
Loading

0 comments on commit f8a8036

Please sign in to comment.