Skip to content

Commit

Permalink
CPPSDK: set title for properties inside x-schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
HaseenaSainul committed Nov 10, 2023
1 parent c6fae1d commit a0c4996
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
${if.namespace.notsame}${info.Title}::${end.if.namespace.notsame}${title} response = (*proxyResponse)->Value();
${if.namespace.notsame}${info.Title}::${end.if.namespace.notsame}${title} response = proxyResponse->Value();
7 changes: 3 additions & 4 deletions src/macrofier/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
28 changes: 23 additions & 5 deletions src/macrofier/types.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
})
})
}
Expand Down Expand Up @@ -353,7 +371,7 @@ const insertObjectMacros = (content, schema, module, title, property, options) =

const regex = new RegExp("\\$\\{" + macro + "\\}", "g")
content = content.replace(regex, properties.join('\n')).replace(/\$\{level}/g, options.parentLevel > 0 ? options.parentLevel : '')
if (!schema.properties && !schema.additionalProperties) {
if (!schema.properties && !schema.propertyNames && !schema.additionalProperties) {
content = getTemplate(path.join(options.templateDir, 'object-empty-property'))
}
})
Expand Down
50 changes: 49 additions & 1 deletion src/shared/modules.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -986,6 +986,51 @@ 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]) => {
if (name === 'Intents') {
schemas = addComponentSubSchemasName(obj, schemas)
}
})
}
return obj
}

const getPathFromModule = (module, path) => {
console.error("DEPRECATED: getPathFromModule")

Expand Down Expand Up @@ -1170,7 +1215,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() + '.'))
Expand Down Expand Up @@ -1230,6 +1275,9 @@ const getModule = (name, json, copySchemas) => {
}

openrpc = setPath(destination, schema, openrpc)
if (extractSubSchemas) {
openrpc = promoteAndNameXSchemas(openrpc)
}
}
})
}
Expand Down

0 comments on commit a0c4996

Please sign in to comment.