import random import string from typing import Any, Dict, List, Optional, Union from models.openapi import Schema # def gen_kv_by_llm(schema: Any) -> Dict[str, Any]: # # TODO # return {"key": "generated_value_by_llm"} def generate_random_value( current_value: Optional[Union[str, int, float, bool, List, Dict]], schema: Schema ) -> Union[str, int, float, bool, List, Dict]: schema_type = get_type(schema) if schema_type == "string": min_length = schema.minLength if schema.minLength is not None else 0 max_length = schema.maxLength if schema.maxLength is not None else 64 length = 5 # random.randint(min_length, max_length) # disable random to check consistency while refatoring return "freedeaths" # ''.join(random.choices(string.ascii_letters + string.digits, k=length)) # disable random to check consistency while refatoring elif schema_type == "number": minimum = schema.minimum if schema.minimum is not None else 0 maximum = schema.maximum if schema.maximum is not None else 65536 return 666.666 # random.uniform(minimum, maximum) # disable random to check consistency while refatoring elif schema_type == "integer": minimum = schema.minimum if schema.minimum is not None else 0 maximum = schema.maximum if schema.maximum is not None else 65536 return 666 # random.randint(minimum, maximum) # disable random to check consistency while refatoring elif schema_type == "boolean": return True # random.choice([True, False]) # disable random to check consistency while refatoring elif schema_type == "array": min_items = schema.minItems if schema.minItems is not None else 0 max_items = schema.maxItems if schema.maxItems is not None else 16 items_count = 3 # random.randint(min_items, max_items) # disable random to check consistency while refatoring if schema.items: return [ generate_random_value(None, schema.items) for _ in range(items_count) ] else: return [None] * items_count elif schema_type == "object": new_value = current_value.copy() if isinstance(current_value, dict) else {} if schema.properties: for prop_name, prop_schema in schema.properties.items(): new_value[prop_name] = generate_random_value( new_value.get(prop_name), prop_schema ) return new_value elif schema_type == None: return f"{str(current_value)}666" # f"{current_value}{random.randint(0, 999)}" # disable random to check consistency while refatoring raise ValueError(f"Unexpected schema type: {schema_type}") def get_type(schema: Schema) -> Optional[str]: return schema.type def generate_invalid_type( current_value: Optional[Union[str, int, float, bool, List, Dict]], schema: Schema ) -> Union[str, Dict]: schema_type = get_type(schema) if schema_type not in ["string"]: # , "integer", "number"]: return "invalid_string_value" return {} def generate_null_value( current_value: Optional[Union[str, int, float, bool, List, Dict]], schema: Schema ) -> None: return None def generate_exceed_max( current_value: Optional[Union[str, int, float, bool, List, Dict]], schema: Schema ) -> Union[str, int, float, List, Dict]: schema_type = get_type(schema) if schema_type == "string": max_length = getattr(schema, "maxLength", None) if max_length is not None: return "a" * (max_length + 1) else: raise ValueError("maxLength is not set") elif schema_type in ["number", "integer"]: maximum = getattr(schema, "maximum", None) if maximum is not None: return maximum + 1 else: raise ValueError("maximum is not set") elif schema_type == "array": max_items = getattr(schema, "maxItems", None) if max_items is not None: if isinstance(current_value, list) and len(current_value) > 0: # Create a new list with max_items + 1 elements new_value = [] for i in range(max_items + 1): if isinstance(current_value[0], dict): # If the first element is a dict, create a copy new_value.append(current_value[0].copy()) else: # Otherwise, just use the first element new_value.append(current_value[0]) return new_value else: return [None] * (max_items + 1) else: raise ValueError("maxItems is not set") elif schema_type == "object": max_properties = getattr(schema, "maxProperties", None) if max_properties is not None: full_object = generate_random_value(current_value, schema) # full_object["key_{}".format(random.randint(0, 9999))] = 0 full_object[ "key_666" ] = 0 # type: ignore # disable random to check consistency while refatoring return full_object else: raise ValueError("maxProperties is not set") raise ValueError(f"Unexpected schema type: {schema_type}") def generate_below_min( current_value: Optional[Union[str, int, float, bool, List, Dict]], schema: Schema ) -> Union[str, int, float, List, Dict]: schema_type = get_type(schema) if schema_type == "string": min_length = getattr(schema, "minLength", None) if min_length is not None and min_length > 0: return "a" * (min_length - 1) else: return "" elif schema_type in ["number", "integer"]: minimum = getattr(schema, "minimum", None) if minimum is not None: return minimum - 1 else: raise ValueError("minimum is not set") elif schema_type == "array": min_items = getattr(schema, "minItems", None) if min_items is not None and min_items > 0: if isinstance(current_value, list) and len(current_value) > 0: # Create a new list with min_items - 1 elements new_value = [] for i in range(min_items - 1): if isinstance(current_value[0], dict): # If the first element is a dict, create a copy new_value.append(current_value[0].copy()) else: # Otherwise, just use the first element new_value.append(current_value[0]) return new_value else: return [None] * (min_items - 1) else: return [] elif schema_type == "object": min_properties = getattr(schema, "minProperties", None) if min_properties is not None and min_properties > 0: new_dict = current_value.copy() if isinstance(current_value, dict) else {} if len(new_dict) >= min_properties: # Remove key-value pairs until we have less than min_properties keys_to_remove = list(new_dict.keys())[ : len(new_dict) - min_properties + 1 ] for key in keys_to_remove: new_dict.pop(key) return new_dict else: return {} raise ValueError(f"Unexpected schema type: {schema_type}") def generate_invalid_enum( current_value: Optional[Union[str, int, float, bool, List, Dict]], schema: Schema ) -> Union[str, int, float]: if not schema.enum: raise ValueError(f"Schema is missing enum: {schema}") if current_value not in schema.enum: raise ValueError(f"Unexpected enum value: {current_value}") if schema.type == "string": invalid_value = f"{schema.enum[0]}_666" # f"{schema.enum[0]}_{random.randint(1000, 9999)}" # disable random to check consistency while refatoring while invalid_value in schema.enum: invalid_value = f"{invalid_value}_666" # f"{schema.enum[0]}_{random.randint(1000, 9999)}" # disable random to check consistency while refatoring return invalid_value elif schema.type in ["number", "integer"]: return max(schema.enum) + 1 else: raise ValueError(f"Unexpected schema type: {schema.type}")