From da1e06a9187297e3a3698695f867b1cadc69f983 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sven=20H=C3=B6ffler?= Date: Wed, 27 Aug 2025 11:30:55 +0200 Subject: [PATCH] Now generates and uses request content type if specified --- lib/utils/schemaAndComponentJsonBuilder.js | 13 +++++++++++++ templates/lib/actions/action.js | 8 +++++--- templates/lib/triggers/trigger.js | 8 +++++--- 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/lib/utils/schemaAndComponentJsonBuilder.js b/lib/utils/schemaAndComponentJsonBuilder.js index d3a4596..3aae235 100644 --- a/lib/utils/schemaAndComponentJsonBuilder.js +++ b/lib/utils/schemaAndComponentJsonBuilder.js @@ -118,6 +118,19 @@ async function schemaBuilder(api, componentJson, existingNames, outputDir) { } } + // For response content type we only care about successes, and prefer json + if (operation.responses && operation.responses['200']?.content) { + const responseContentTypes = Object.keys(operation.responses['200'].content); + action.callParams['responseContentType'] = responseContentTypes.some((c) => c === "application/json") + ? "application/json" + : `${responseContentTypes[0]}`; + } else if (operation.responses && operation.responses['201']?.content) { + const responseContentTypes = Object.keys(operation.responses['201'].content); + action.callParams['responseContentType'] = responseContentTypes.some((c) => c === "application/json") + ? "application/json" + : `${responseContentTypes[0]}`; + } + let schemaOut = {}; schema = getSchemaIn(schema); diff --git a/templates/lib/actions/action.js b/templates/lib/actions/action.js index 4e634fe..c3d3431 100644 --- a/templates/lib/actions/action.js +++ b/templates/lib/actions/action.js @@ -39,12 +39,13 @@ async function processAction(msg, cfg, snapshot, incomingMessageHeaders, tokenDa logger.info("Starting to execute action '%s'", actionFunction); const action = componentJson.actions[actionFunction]; - const { operationId, pathName, method, requestContentType } = action.callParams; + const { operationId, pathName, method, requestContentType, responseContentType } = action.callParams; logger.info( - "Found spec callParams: 'pathName': %s, 'method': %s, 'requestContentType': %s", + "Found spec callParams: 'pathName': %s, 'method': %s, 'requestContentType': %s, 'responseContentType': %t", pathName, method, - requestContentType + requestContentType, + responseContentType ); const specPath = spec.paths[pathName]; @@ -100,6 +101,7 @@ async function processAction(msg, cfg, snapshot, incomingMessageHeaders, tokenDa method: method, parameters: parameters, requestContentType: requestContentType, + responseContentType: responseContentType, requestBody: body, securities: { authorized: securities }, server: spec.servers[cfg.server] || cfg.otherServer, diff --git a/templates/lib/triggers/trigger.js b/templates/lib/triggers/trigger.js index 0c54485..54e6568 100644 --- a/templates/lib/triggers/trigger.js +++ b/templates/lib/triggers/trigger.js @@ -61,12 +61,13 @@ async function processTrigger(msg, cfg, snapshot, incomingMessageHeaders, tokenD ); const trigger = componentJson.triggers[triggerFunction]; - const { operationId, pathName, method, requestContentType } = trigger.callParams; + const { operationId, pathName, method, requestContentType, responseContentType } = trigger.callParams; logger.info( - "Found spec callParams: \"pathName\": %s, \"method\": %s, \"requestContentType\": %s", + "Found spec callParams: \"pathName\": %s, \"method\": %s, \"requestContentType\": %s, \"responseContentType\": %t", pathName, method, - requestContentType + requestContentType, + responseContentType, ); const specPath = spec.paths[pathName]; @@ -150,6 +151,7 @@ async function processTrigger(msg, cfg, snapshot, incomingMessageHeaders, tokenD method: method, parameters: parameters, requestContentType: requestContentType, + responseContentType: responseContentType, securities: { authorized: securities }, server: spec.servers[cfg.server] || cfg.otherServer };