From 9f5bb5450424240e2789b77d7ad44b20e27f6ebe Mon Sep 17 00:00:00 2001 From: Daniel Brondani Date: Fri, 17 Oct 2025 09:01:01 +0200 Subject: [PATCH] [codegen] Improve request parameters generation for reuse of common structs --- api/csolution-openapi.yml | 73 ++++++++++++++------------------------- codegen/src/codegen.ts | 36 +++++++++++++++---- 2 files changed, 54 insertions(+), 55 deletions(-) diff --git a/api/csolution-openapi.yml b/api/csolution-openapi.yml index 1388f02..88bc9e2 100644 --- a/api/csolution-openapi.yml +++ b/api/csolution-openapi.yml @@ -963,6 +963,18 @@ components: - properties: result: $ref: '#/components/schemas/SuccessResult' + LoadSolutionParams: + type: object + properties: + solution: + type: string + description: Path to .csolution.yml + activeTarget: + type: string + description: Active target set in the format [@] + required: + - solution + - activeTarget LoadSolutionRequest: allOf: - $ref: '#/x-jsonrpc-envelope-request-with-params' @@ -971,17 +983,7 @@ components: type: string const: LoadSolution params: - type: object - properties: - solution: - type: string - description: Path to .csolution.yml - activeTarget: - type: string - description: Active target set in the format [@] - required: - - solution - - activeTarget + $ref: '#/components/schemas/LoadSolutionParams' LoadSolutionResponse: allOf: - $ref: '#/x-jsonrpc-envelope-response' @@ -1281,6 +1283,15 @@ components: - properties: result: $ref: '#/components/schemas/DraftProjectsInfo' + ConvertSolutionParams: + allOf: + - $ref: '#/components/schemas/LoadSolutionParams' + - properties: + updateRte: + type: boolean + description: Create/update configuration files + required: + - updateRte ConvertSolutionRequest: allOf: - $ref: '#/x-jsonrpc-envelope-request-with-params' @@ -1289,21 +1300,7 @@ components: type: string const: ConvertSolution params: - type: object - properties: - solution: - type: string - description: Path to .csolution.yml - activeTarget: - type: string - description: Active target set in the format [@] - updateRte: - type: boolean - description: Create/update configuration files - required: - - solution - - activeTarget - - updateRte + $ref: '#/components/schemas/ConvertSolutionParams' ConvertSolutionResult: allOf: - $ref: '#/components/schemas/SuccessResult' @@ -1332,17 +1329,7 @@ components: type: string const: DiscoverLayers params: - type: object - properties: - solution: - type: string - description: Path to .csolution.yml - activeTarget: - type: string - description: Active target set in the format [@] - required: - - solution - - activeTarget + $ref: '#/components/schemas/LoadSolutionParams' DiscoverLayersResponse: allOf: - $ref: '#/x-jsonrpc-envelope-response' @@ -1357,17 +1344,7 @@ components: type: string const: ListMissingPacks params: - type: object - properties: - solution: - type: string - description: Path to .csolution.yml - activeTarget: - type: string - description: Active target set in the format [@] - required: - - solution - - activeTarget + $ref: '#/components/schemas/LoadSolutionParams' ListMissingPacksResult: allOf: - $ref: '#/components/schemas/SuccessResult' diff --git a/codegen/src/codegen.ts b/codegen/src/codegen.ts index 4817fa5..fe74710 100644 --- a/codegen/src/codegen.ts +++ b/codegen/src/codegen.ts @@ -271,6 +271,26 @@ using namespace jsonrpccxx;\n`; } } + public genCppParams(name: string, cppParams: string[], cppRegParams: string[]) { + const struct = this.structs[name]; + if (struct) { + if (struct.extends) { + for (const parent of struct.extends) { + if (parent in this.structs) { + this.genCppParams(parent, cppParams, cppRegParams); + } + } + } + if (struct.members) { + for (const element of struct.members) { + const cppType = element.cppType in this.structs ? `RpcArgs::${element.cppType}` : element.cppType; + cppParams.push(`const ${cppType}& ${element.name}`); + cppRegParams.push(`"${element.name}"`); + } + } + } + } + public collectFunction(name: string, params: any, result: any, description?: string) { const cppResult = this.getType(name, result, 'Result', 'RpcArgs::').cpp; let cppFunction = `virtual ${cppResult} ${name}(`; @@ -282,13 +302,15 @@ using namespace jsonrpccxx;\n`; const tsFunction = `${this.pascalToCamel(name)}(${tsParamsType ? `args: ${tsParamsType}` : ``}): Promise<${tsResultType}>`; const tsImplementation = `this.get('${name}'${tsParamsType ? `, args` : ``})`; - if (params && params.properties) { - const cppParams: string[] = []; - const cppRegParams: string[] = []; - for (const [param, item] of Object.entries(params.properties)) { - cppParams.push(`const ${this.getType(name, item, '', 'RpcArgs::').cpp}& ${param}`); - cppRegParams.push(`"${param}"`); - } + let paramsName = this.getTypeName(name, 'Params'); + if ((params as any)?.$ref) { + const ref = (params as any).$ref.match(/^#\/components\/schemas\/(.*)/); + paramsName = ref[1]; + } + const cppParams: string[] = []; + const cppRegParams: string[] = []; + this.genCppParams(paramsName, cppParams, cppRegParams); + if (cppParams.length > 0) { cppFunction += cppParams.join(", "); cppRegistration += `, { ${cppRegParams.join(', ')} }`; } else {