From 7c58195a93d3f66bb8786ba218edddd98c74927f Mon Sep 17 00:00:00 2001 From: David Wilson Date: Thu, 11 Jun 2020 10:37:08 -0700 Subject: [PATCH] Fix recursive traversal of allOf/anyOf/oneOf in object schemas --- modelerfour/modeler/modelerfour.ts | 38 ++++++++++++++---------------- modelerfour/prenamer/prenamer.ts | 2 +- 2 files changed, 19 insertions(+), 21 deletions(-) diff --git a/modelerfour/modeler/modelerfour.ts b/modelerfour/modeler/modelerfour.ts index 866d919..042ec3d 100644 --- a/modelerfour/modeler/modelerfour.ts +++ b/modelerfour/modeler/modelerfour.ts @@ -630,25 +630,12 @@ export class ModelerFour { return objectSchema; } - processObjectSchema(name: string, aSchema: OpenAPI.Schema): ObjectSchema | DictionarySchema | OrSchema | XorSchema | AnySchema { - let i = 0; - const parents: Array = values(aSchema.allOf).select(sch => this.use(sch, (n, s) => { - return this.processSchema(n || `${name}.allOf.${i++}`, s); - })).toArray(); - const orTypes = values(aSchema.anyOf).select(sch => this.use(sch, (n, s) => { - return this.processSchema(n || `${name}.anyOf.${i++}`, s); - })).toArray(); - const xorTypes = values(aSchema.oneOf).select(sch => this.use(sch, (n, s) => { - return this.processSchema(n || `${name}.oneOf.${i++}`, s); - })).toArray(); - - const dictionaryDef = aSchema.additionalProperties; - - const schema = aSchema; - + processObjectSchema(name: string, schema: OpenAPI.Schema): ObjectSchema | DictionarySchema | OrSchema | XorSchema | AnySchema { + const dictionaryDef = schema.additionalProperties; // is this more than a straightforward object? - const isMoreThanObject = (parents.length + orTypes.length + xorTypes.length) > 0 || !!dictionaryDef; + const parentCount = length(schema.allOf); + const isMoreThanObject = (parentCount + length(schema.anyOf) + length(schema.oneOf)) > 0 || !!dictionaryDef; // do we have properties at all? const hasProperties = length(schema.properties) > 0; @@ -659,13 +646,24 @@ export class ModelerFour { return this.anySchema; } - const dictionarySchema = dictionaryDef ? this.processDictionarySchema(name, aSchema) : undefined; - if (parents.length === 0 && !hasProperties && dictionarySchema) { + const dictionarySchema = dictionaryDef ? this.processDictionarySchema(name, schema) : undefined; + if (parentCount === 0 && !hasProperties && dictionarySchema) { return dictionarySchema; } const objectSchema = this.createObjectSchema(name, schema); + let i = 0; + const parents: Array = values(schema.allOf).select(sch => this.use(sch, (n, s) => { + return this.processSchema(n || `${name}.allOf.${i++}`, s); + })).toArray(); + const orTypes = values(schema.anyOf).select(sch => this.use(sch, (n, s) => { + return this.processSchema(n || `${name}.anyOf.${i++}`, s); + })).toArray(); + const xorTypes = values(schema.oneOf).select(sch => this.use(sch, (n, s) => { + return this.processSchema(n || `${name}.oneOf.${i++}`, s); + })).toArray(); + // add it to the upcoming and schema set // andTypes.unshift(objectSchema); @@ -769,7 +767,7 @@ export class ModelerFour { trap = new Set(); processSchemaImpl(schema: OpenAPI.Schema, name: string): Schema { if (this.trap.has(schema)) { - throw new Error('RECURSING!'); + throw new Error(`RECURSING! Saw schema ${schema.title} more than once.`); } this.trap.add(schema); diff --git a/modelerfour/prenamer/prenamer.ts b/modelerfour/prenamer/prenamer.ts index 05f5c49..800347b 100644 --- a/modelerfour/prenamer/prenamer.ts +++ b/modelerfour/prenamer/prenamer.ts @@ -286,7 +286,7 @@ export class PreNamer { } for (const operationGroup of this.codeModel.operationGroups) { - setNameAllowEmpty(operationGroup, this.format.operationGroup, operationGroup.$key, this.format.override); + setNameAllowEmpty(operationGroup, this.format.operationGroup, operationGroup.$key, this.format.override, { removeDuplicates: false }); for (const operation of operationGroup.operations) { setName(operation, this.format.operation, '', this.format.override);