public static createInstance()

in tools/sdk-testgen/src/core/model.ts [124:232]


    public static createInstance(
        session: Session<TestCodeModel>,
        rawValue: any,
        usedProperties: Set<string[]>,
        schema: Schema,
        language: Languages,
        extensions: Record<string, any>,
        searchDescents = true,
    ): ExampleValue {
        const xMsFormat = 'x-ms-format';
        const xMsFormatElementType = 'x-ms-format-element-type';
        const instance = new ExampleValue(schema, language);
        if (!schema) {
            instance.rawValue = rawValue;
            return instance;
        }

        function addParentValue(parent: ComplexSchema) {
            const parentValue = ExampleValue.createInstance(session, rawValue, usedProperties, parent, parent.language, parent.extensions, false);
            if ((parentValue.properties && Object.keys(parentValue.properties).length > 0) || (parentValue.parentsValue && Object.keys(parentValue.parentsValue).length > 0)) {
                instance.parentsValue[parent.language.default.name] = parentValue;
            }
        }

        if (schema.type === SchemaType.Array && Array.isArray(rawValue)) {
            instance.elements = rawValue.map((x) => this.createInstance(session, x, new Set(), (schema as ArraySchema).elementType, undefined, undefined));
        } else if (schema.type === SchemaType.Object && rawValue === Object(rawValue)) {
            const childSchema: ComplexSchema = searchDescents ? Helper.findInDescents(schema as ObjectSchema, rawValue) : schema;
            instance.schema = childSchema;

            instance.properties = {};
            const splitParentsValue = TestCodeModeler.instance.testConfig.getValue(Config.splitParentsValue);
            for (const property of Helper.getAllProperties(childSchema, !splitParentsValue)) {
                if (property.flattenedNames) {
                    if (!Helper.pathIsIncluded(usedProperties, property.flattenedNames)) {
                        const value = Helper.queryByPath(rawValue, property.flattenedNames);
                        if (value.length === 1) {
                            instance.properties[property.serializedName] = this.createInstance(
                                session,
                                value[0],
                                Helper.filterPathsByPrefix(usedProperties, property.flattenedNames),
                                property.schema,
                                property.language,
                                property.extensions,
                            );
                            instance.properties[property.serializedName].flattenedNames = property.flattenedNames;
                            usedProperties.add(property.flattenedNames);
                        }
                    }
                } else {
                    if (Object.prototype.hasOwnProperty.call(rawValue, property.serializedName) && !Helper.pathIsIncluded(usedProperties, [property.serializedName])) {
                        instance.properties[property.serializedName] = this.createInstance(
                            session,
                            rawValue[property.serializedName],
                            Helper.filterPathsByPrefix(usedProperties, [property.serializedName]),
                            property.schema,
                            property.language,
                            property.extensions,
                        );
                        usedProperties.add([property.serializedName]);
                    }
                }
            }

            instance.parentsValue = {};
            // Add normal parentValues
            if (splitParentsValue && Object.prototype.hasOwnProperty.call(childSchema, 'parents') && (childSchema as ObjectSchema).parents) {
                for (const parent of (childSchema as ObjectSchema).parents.immediate) {
                    if (childSchema.type === SchemaType.Object) {
                        addParentValue(parent);
                    } else {
                        console.warn(`${parent.language.default.name} is NOT a object type of parent of ${childSchema.language.default.name}!`);
                    }
                }
            }

            // Add AdditionalProperties as ParentValue
            if (Object.prototype.hasOwnProperty.call(childSchema, 'parents') && (childSchema as ObjectSchema).parents) {
                for (const parent of (childSchema as ObjectSchema).parents.immediate) {
                    if (parent.type === SchemaType.Dictionary) {
                        addParentValue(parent);
                    }
                }
            }
        } else if (schema.type === SchemaType.Dictionary && rawValue === Object(rawValue)) {
            instance.properties = {};
            for (const [key, value] of Object.entries(rawValue)) {
                if (!Helper.pathIsIncluded(usedProperties, [key])) {
                    instance.properties[key] = this.createInstance(session, value, new Set([...usedProperties]), (schema as DictionarySchema).elementType, undefined, undefined);
                    usedProperties.add([key]);
                }
            }
        } else if (schema.type === SchemaType.AnyObject && extensions && extensions[xMsFormat] && extensions[xMsFormat].startsWith('dfe-')) {
            // Becuase DataFactoryElement is defined as AnyObject schema, so have to explicitly build it's example value according to x_ms_format here
            const format = extensions[xMsFormat];
            const elementFormat = extensions[xMsFormatElementType];

            const dfeObjSchema = ExampleValue.createSchemaForDfeObject(session, rawValue, format);
            if (dfeObjSchema) {
                return this.createInstance(session, rawValue, usedProperties, dfeObjSchema, language, undefined, searchDescents);
            } else {
                const dfeLiterlSchema = ExampleValue.createSchemaForDfeLiteral(session, rawValue, format, elementFormat);
                return this.createInstance(session, rawValue, usedProperties, dfeLiterlSchema, language, undefined, searchDescents);
            }
        } else {
            instance.rawValue = rawValue;
        }
        return instance;
    }