diff --git a/languages/cpp/templates/callback-result-instantiation/enum.cpp b/languages/cpp/templates/callback-result-instantiation/enum.cpp index 30fdf717..d5bc4150 100644 --- a/languages/cpp/templates/callback-result-instantiation/enum.cpp +++ b/languages/cpp/templates/callback-result-instantiation/enum.cpp @@ -1 +1 @@ - ${if.namespace.notsame}${info.Title}::${end.if.namespace.notsame}${title} response = (*proxyResponse)->Value(); \ No newline at end of file + ${if.namespace.notsame}${info.Title}::${end.if.namespace.notsame}${title} response = proxyResponse->Value(); \ No newline at end of file diff --git a/src/macrofier/index.mjs b/src/macrofier/index.mjs index 04cdd9fb..b3ad15db 100644 --- a/src/macrofier/index.mjs +++ b/src/macrofier/index.mjs @@ -133,12 +133,11 @@ const macrofy = async ( const staticModules = staticModuleNames.map(name => ( { info: { title: name } } )) let modules - if (hidePrivate) { - modules = moduleList.map(name => getModule(name, openrpc, copySchemasIntoModules)).filter(hasPublicAPIs) + modules = moduleList.map(name => getModule(name, openrpc, copySchemasIntoModules, extractSubSchemas)).filter(hasPublicAPIs) } else { - modules = moduleList.map(name => getModule(name, openrpc, copySchemasIntoModules)) + modules = moduleList.map(name => getModule(name, openrpc, copySchemasIntoModules, extractSubSchemas)) } const aggregateMacros = engine.generateAggregateMacros(openrpc, modules.concat(staticModules), templates, libraryName) @@ -160,7 +159,7 @@ const macrofy = async ( if (isPrimary) { primaryOutput.push(path.join(output, outputFile)) - } + } const content = engine.insertAggregateMacros(templates[file], aggregateMacros) outputFiles[path.join(output, outputFile)] = content diff --git a/src/macrofier/types.mjs b/src/macrofier/types.mjs index fcb64eff..f2a8089e 100644 --- a/src/macrofier/types.mjs +++ b/src/macrofier/types.mjs @@ -126,16 +126,34 @@ const getTemplate = (name) => { return templates[Object.keys(templates).find(k => k === name)] || templates[Object.keys(templates).find(k => k.startsWith(name.split('.').shift() + '.'))] || '' } +const getXSchemaGroupFromProperties = (schema, title, properties, group) => { + if (properties) { + Object.entries(properties).forEach(([name, prop]) => { + if (schema.title === prop.title) { + group = title + } else { + group = getXSchemaGroupFromProperties(schema, title, prop.properties, group) + } + }) + } + return group +} + // TODO: this assumes the same title doesn't exist in multiple x-schema groups! const getXSchemaGroup = (schema, module) => { let group = module.info.title if (schema.title && module['x-schemas']) { Object.entries(module['x-schemas']).forEach(([title, module]) => { - Object.values(module).forEach(s => { - if (schema.title === s.title) { - group = title - } + Object.values(module).forEach(moduleSchema => { + let schemas = moduleSchema.allOf ? moduleSchema.allOf : [moduleSchema] + schemas.forEach((s) => { + if (schema.title === s.title || schema.title === moduleSchema.title) { + group = title + } else { + group = getXSchemaGroupFromProperties(schema, title, s.properties, group) + } + }) }) }) } diff --git a/src/shared/modules.mjs b/src/shared/modules.mjs index 9f85bb1d..105ab8f7 100644 --- a/src/shared/modules.mjs +++ b/src/shared/modules.mjs @@ -986,6 +986,49 @@ const createPolymorphicMethods = (method, json) => { return polymorphicMethodSchemas } +const isSubSchema = (schema) => schema.type === 'object' || (schema.type === 'string' && schema.enum) + +const addComponentSubSchemasNameForProperties = (key, schema) => { + if ((schema.type === "object") && schema.properties) { + Object.entries(schema.properties).forEach(([name, propSchema]) => { + if (isSubSchema(propSchema)) { + key = key + name.charAt(0).toUpperCase() + name.substring(1) + if (!propSchema.title) { + propSchema.title = key + } + propSchema = addComponentSubSchemasNameForProperties(key, propSchema) + } + }) + } + + return schema +} + +const addComponentSubSchemasName = (obj, schemas) => { + Object.entries(schemas).forEach(([key, schema]) => { + let componentSchemaProperties = schema.allOf ? schema.allOf : [schema] + componentSchemaProperties.forEach((componentSchema) => { + key = key.charAt(0).toUpperCase() + key.substring(1) + componentSchema = addComponentSubSchemasNameForProperties(key, componentSchema) + }) + if (!schema.title && !key) { + schema.title = capitalize(key) + } + }) + + return schemas +} + +const promoteAndNameXSchemas = (obj) => { + obj = JSON.parse(JSON.stringify(obj)) + if (obj['x-schemas']) { + Object.entries(obj['x-schemas']).forEach(([name, schemas]) => { + schemas = addComponentSubSchemasName(obj, schemas) + }) + } + return obj +} + const getPathFromModule = (module, path) => { console.error("DEPRECATED: getPathFromModule") @@ -1170,7 +1213,7 @@ const removeUnusedSchemas = (json) => { return schema } -const getModule = (name, json, copySchemas) => { +const getModule = (name, json, copySchemas, extractSubSchemas) => { let openrpc = JSON.parse(JSON.stringify(json)) openrpc.methods = openrpc.methods .filter(method => method.name.toLowerCase().startsWith(name.toLowerCase() + '.')) @@ -1230,6 +1273,9 @@ const getModule = (name, json, copySchemas) => { } openrpc = setPath(destination, schema, openrpc) + if (extractSubSchemas) { + openrpc = promoteAndNameXSchemas(openrpc) + } } }) }