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 13, 2023
1 parent c19dda8 commit 48b7c21
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 10 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
26 changes: 22 additions & 4 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
48 changes: 47 additions & 1 deletion src/shared/modules.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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")

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

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

0 comments on commit 48b7c21

Please sign in to comment.