Skip to content

Commit 37e5bea

Browse files
committed
Refactor request body handling to support $ref resolution
- Introduced a utility function `getRequestBodyContent` to handle both direct content and $ref references in request bodies. - Updated `CSharpApiGenerator`, `exampleGenerator`, `mainContentBuilder`, `rightPanel`, and `rightPanelHandlers` to utilize the new utility function for consistent request body processing. - Enhanced error handling and logging for unresolved references. - Improved code readability and maintainability by reducing redundancy in request body processing logic.
1 parent 0d5c2c6 commit 37e5bea

File tree

9 files changed

+986
-755
lines changed

9 files changed

+986
-755
lines changed

src/core/demo-dist/bundle.js

Lines changed: 327 additions & 250 deletions
Large diffs are not rendered by default.

src/core/demo-dist/demo-info.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@
2222
"description": "A comprehensive test API demonstrating all supported OpenAPI security schemes"
2323
}
2424
],
25-
"buildDate": "2025-06-11T14:04:43.171Z"
25+
"buildDate": "2025-07-30T17:21:51.894Z"
2626
}

src/core/dist/bundle.js

Lines changed: 327 additions & 250 deletions
Large diffs are not rendered by default.

src/core/js/codeApiGenerators/csharpApiClientGenerator.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,9 @@ class CSharpApiGenerator {
475475
}
476476

477477
if (operation.requestBody) {
478-
const content = operation.requestBody.content;
478+
// Use the utility function to get request body content, handling both direct content and $ref
479+
const resolvedRequestBody = window.utils.getRequestBodyContent(operation.requestBody, this.swaggerData);
480+
const content = resolvedRequestBody ? resolvedRequestBody.content : null;
479481
if (content && content["application/json"]) {
480482
const schema = content["application/json"].schema;
481483
const type = this.mapToCSharpType(schema);

src/core/js/exampleGenerator.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,11 +293,18 @@ function generatePrimitiveExample(propSchema, components, indent) {
293293

294294
// Main function to generate compliant request body examples
295295
function generateRequestBodyExample(operation, swaggerSpec) {
296-
if (!operation.requestBody || !operation.requestBody.content) {
296+
if (!operation.requestBody) {
297297
return null;
298298
}
299299

300-
const content = operation.requestBody.content;
300+
// Use the utility function to get request body content, handling both direct content and $ref
301+
const resolvedRequestBody = window.utils.getRequestBodyContent(operation.requestBody, swaggerSpec);
302+
303+
if (!resolvedRequestBody || !resolvedRequestBody.content) {
304+
return null;
305+
}
306+
307+
const content = resolvedRequestBody.content;
301308
const contentTypes = Object.keys(content);
302309

303310
if (contentTypes.length === 0) {

src/core/js/mainContentBuilder.js

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -468,10 +468,10 @@ function buildMainContent() {
468468
}
469469
}
470470

471-
// Add request body section if exists
472-
if (operation.requestBody) {
473-
sectionHTML += buildRequestBodySection(operation, sectionId);
474-
}
471+
// Add request body section if exists
472+
if (operation.requestBody) {
473+
sectionHTML += buildRequestBodySection(operation, sectionId);
474+
}
475475

476476
// Add responses section
477477
sectionHTML += buildResponsesSection(operation, sectionId);
@@ -722,11 +722,15 @@ function addMainContentEventListeners(section) {
722722

723723
// Add request body dropdown handling
724724
function addRequestBodyHandlers(operation, sectionId) {
725+
// Use the utility function to get request body content, handling both direct content and $ref
726+
const resolvedRequestBody = window.utils.getRequestBodyContent(operation.requestBody, swaggerData);
727+
728+
if (!resolvedRequestBody || !resolvedRequestBody.content) {
729+
return; // No request body content to handle
730+
}
731+
725732
// Add event listener for the request body dropdown after section is added to DOM
726-
if (
727-
operation.requestBody &&
728-
Object.keys(operation.requestBody.content).length > 0
729-
) {
733+
if (Object.keys(resolvedRequestBody.content || {}).length > 0) {
730734
const requestBodyIdBase = `request-body-${sectionId}`;
731735
const selectElement = document.getElementById(
732736
`${requestBodyIdBase}-content-type-select`
@@ -743,7 +747,7 @@ function addRequestBodyHandlers(operation, sectionId) {
743747

744748
if (isSelectElement) {
745749
// Set 'application/json' as default if available for select elements
746-
const contentTypes = Object.keys(operation.requestBody.content);
750+
const contentTypes = Object.keys(resolvedRequestBody.content || {});
747751
if (contentTypes.includes("application/json")) {
748752
selectElement.value = "application/json";
749753
}
@@ -761,11 +765,11 @@ function addRequestBodyHandlers(operation, sectionId) {
761765
selectedContentType?.includes("json");
762766

763767
if (
764-
operation.requestBody.content[selectedContentType] &&
765-
operation.requestBody.content[selectedContentType].schema
768+
resolvedRequestBody.content[selectedContentType] &&
769+
resolvedRequestBody.content[selectedContentType].schema
766770
) {
767771
const schema =
768-
operation.requestBody.content[selectedContentType].schema;
772+
resolvedRequestBody.content[selectedContentType].schema;
769773

770774
// Update schema details
771775
const schemaDetailsElement = document.getElementById(
@@ -1067,7 +1071,16 @@ function buildRequestBodySection(operation, sectionId) {
10671071
<h3 class="text-gray-700 font-semibold mb-2 text-lg">Request Body</h3>
10681072
`;
10691073

1070-
const contentTypes = Object.keys(operation.requestBody.content);
1074+
// Use the utility function to get request body content, handling both direct content and $ref
1075+
const resolvedRequestBody = window.utils.getRequestBodyContent(operation.requestBody, swaggerData);
1076+
1077+
if (!resolvedRequestBody || !resolvedRequestBody.content) {
1078+
sectionHTML += `<p class="text-sm text-gray-500">No request body definition found.</p>`;
1079+
sectionHTML += `</div>`;
1080+
return sectionHTML;
1081+
}
1082+
1083+
const contentTypes = Object.keys(resolvedRequestBody.content || {});
10711084
if (contentTypes.length > 0) {
10721085
sectionHTML += `
10731086
<div class="flex items-center mb-4">`;
@@ -1092,7 +1105,7 @@ function buildRequestBodySection(operation, sectionId) {
10921105

10931106
sectionHTML += `
10941107
${
1095-
operation.requestBody.required
1108+
resolvedRequestBody.required
10961109
? '<span class="text-xs text-red-500">required</span>'
10971110
: ""
10981111
}

0 commit comments

Comments
 (0)