Skip to content

Commit

Permalink
CPPSDK: enum array inside component schema also added
Browse files Browse the repository at this point in the history
  • Loading branch information
HaseenaSainul committed Oct 24, 2023
1 parent 8882b2c commit be30f02
Showing 1 changed file with 28 additions and 14 deletions.
42 changes: 28 additions & 14 deletions src/macrofier/engine.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,10 @@ const promoteSchema = (location, property, title, document, destinationPath) =>
}

// only consider sub-objects and sub enums to be sub-schemas
const isSubSchema = (schema) => schema.type === 'object' || (schema.type === 'string' && schema.enum) // || (schema.type === 'array' && schema.items)
const isSubSchema = (schema) => schema.type === 'object' || (schema.type === 'string' && schema.enum)

// check schema is sub enum of array
const isSubEnumOfArraySchema = (schema) => (schema.type === 'array' && schema.items.enum)

const promoteAndNameSubSchemas = (obj) => {
// make a copy so we don't polute our inputs
Expand All @@ -403,19 +406,30 @@ const promoteAndNameSubSchemas = (obj) => {
while (more) {
more = false
Object.entries(obj.components.schemas).forEach(([key, schema]) => {
if ((schema.type === "object") && schema.properties) {
Object.entries(schema.properties).forEach(([name, propSchema]) => {
if (isSubSchema(propSchema)) {
more = true
const descriptor = {
name: name,
schema: propSchema
let componentSchemaProperties = schema.allOf ? schema.allOf : [schema]
componentSchemaProperties.forEach((componentSchema) => {
if ((componentSchema.type === "object") && componentSchema.properties) {
Object.entries(componentSchema.properties).forEach(([name, propSchema]) => {
if (isSubSchema(propSchema)) {
more = true
const descriptor = {
name: name,
schema: propSchema
}
addContentDescriptorSubSchema(descriptor, key, obj)
componentSchema.properties[name] = descriptor.schema
}
addContentDescriptorSubSchema(descriptor, key, obj)
schema.properties[name] = descriptor.schema
}
})
}
if (isSubEnumOfArraySchema(propSchema)) {
const descriptor = {
name: name,
schema: propSchema.items
}
addContentDescriptorSubSchema(descriptor, key, obj)
componentSchema.properties[name].items = descriptor.schema
}
})
}
})

if (!schema.title) {
schema.title = capitalize(key)
Expand Down Expand Up @@ -691,7 +705,7 @@ const convertEnumTemplate = (schema, templateName, templates) => {
return template[i].replace(/\$\{key\}/g, safeName)
.replace(/\$\{value\}/g, value)
}).join('\n')
if (!templateName.includes(".cpp")) {
if (!templateName.includes(config.enumSuffix)) {
template[i] = template[i].replace(/,*$/, '');
}
}
Expand Down

0 comments on commit be30f02

Please sign in to comment.