Skip to content

Commit a313a3b

Browse files
committed
remove array of map to map
1 parent c1c5ede commit a313a3b

1 file changed

Lines changed: 40 additions & 0 deletions

File tree

tools/proto-convert/src/SchemaModifier.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export class SchemaModifier {
2222
this.handleAdditionalPropertiesUndefined(schema)
2323
this.convertNullTypeToNullValue(schema)
2424
this.collapseOrMergeOneOfArray(schema)
25+
this.removeArrayOfMapWrapper(schema)
2526
},
2627
onSchema: (schema, schemaName) => {
2728
if (!schema || isReferenceObject(schema)) return;
@@ -32,6 +33,7 @@ export class SchemaModifier {
3233
this.handleOneOfConst(schema, schemaName)
3334
this.collapseOrMergeOneOfArray(schema)
3435
this.collapseOneOfObjectPropContainsTitleSchema(schema)
36+
this.removeArrayOfMapWrapper(schema)
3537
},
3638
});
3739
const visit = new Set();
@@ -435,4 +437,42 @@ export class SchemaModifier {
435437

436438
this.logger.info(`Converted additionalProperties to named property '${propertyName}' with type: object`);
437439
}
440+
441+
/**
442+
* Removes the array wrapper if the schema is an array of maps (additionalProperties).
443+
* Converts array of objects with only additionalProperties into just the additionalProperties schema.
444+
*
445+
* Example:
446+
* Input:
447+
* {
448+
* type: "array",
449+
* items: {
450+
* type: "object",
451+
* additionalProperties: {
452+
* $ref: "#/components/schemas/Value"
453+
* }
454+
* }
455+
* }
456+
*
457+
* Output:
458+
* {
459+
* type: "object",
460+
* additionalProperties: {
461+
* $ref: "#/components/schemas/Value"
462+
* }
463+
* }
464+
**/
465+
removeArrayOfMapWrapper(schema: OpenAPIV3.SchemaObject): void {
466+
if (schema.type === 'array' && schema.items && typeof schema.items === 'object' && !('$ref' in schema.items)) {
467+
const items = schema.items as OpenAPIV3.SchemaObject;
468+
469+
if (items.type === 'object' && items.additionalProperties && !items.properties) {
470+
(schema as any).type = 'object';
471+
schema.additionalProperties = items.additionalProperties;
472+
delete (schema as any).items;
473+
474+
this.logger.info(`Removed array wrapper from array of maps schema`);
475+
}
476+
}
477+
}
438478
}

0 commit comments

Comments
 (0)