From 38ab41634ff750f367240824124c17697623c158 Mon Sep 17 00:00:00 2001 From: romilin <118544135+romilin@users.noreply.github.com> Date: Thu, 24 Aug 2023 11:18:22 +0200 Subject: [PATCH] Example : fix string without maxLenght + AllOf --- .../apis/{apiId}/editor/_util/model.util.ts | 33 ++++++++++++++++--- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/front-end/studio/src/app/pages/apis/{apiId}/editor/_util/model.util.ts b/front-end/studio/src/app/pages/apis/{apiId}/editor/_util/model.util.ts index dd21e29aa..d3edc89be 100644 --- a/front-end/studio/src/app/pages/apis/{apiId}/editor/_util/model.util.ts +++ b/front-end/studio/src/app/pages/apis/{apiId}/editor/_util/model.util.ts @@ -116,7 +116,16 @@ export class ModelUtils { */ public static generateExampleFromSchema(schema: OasSchema | AaiSchema): any { let generator: ExampleGenerator = new ExampleGenerator(); - return generator.generate(schema); + let example : any[] = []; + example.push(generator.generate(schema)); + if (schema.allOf) { + schema.allOf.forEach( inherited => { + if (inherited.$ref) { + example.push(generator.generate(inherited)); + } + }); + } + return example; } } @@ -163,14 +172,22 @@ export class ExampleGenerator { } } + /** + * Generates an exemple if no example exist + **/ private generateObject(schema: OasSchema | AaiSchema): any { let object: any = {}; if (schema.properties) { console.info("[ExampleGenerator] Schema has properties."); Object.keys(schema.properties).forEach( propertyName => { console.info("[ExampleGenerator] Processing schema property named: ", propertyName); - let propertyExample: any = this.generate(schema.properties[propertyName] as OasSchema); - object[propertyName] = propertyExample; + let example = schema.properties[propertyName].example; + if (example) { + object[propertyName] = example; + } else { + let propertyExample: any = this.generate(schema.properties[propertyName] as OasSchema); + object[propertyName] = propertyExample; + } }); } return object; @@ -271,10 +288,16 @@ export class ExampleGenerator { private generateExampleString(schema: OasSchema | AaiSchema) : string { let text = ""; - while (text.length < schema.maxLength) { + let maxLength = schema.maxLength; + + if (maxLength == null) { + maxLength = Math.floor(Math.random() * 10); + } + + while (text.length < maxLength) { text = text + Math.random().toString(36).slice(2); } - let randomLength = Math.floor(Math.random() * (schema.maxLength - schema.minLength + 1) + schema.minLength) + 2; + let randomLength = Math.floor(Math.random() * (maxLength - schema.minLength + 1) + schema.minLength) + 2; text = text.slice(2, randomLength); return text; }