This commit is contained in:
gongwenxin
2025-08-07 15:07:38 +08:00
parent 1901cf611e
commit fa343eb111
55 changed files with 17850 additions and 16283 deletions
+11 -6
View File
@@ -64,7 +64,8 @@ class DataGenerator:
schema_type = schema.get('type')
if schema_type == 'object':
# Handle both 'object' and 'Object' (case-insensitive)
if schema_type and schema_type.lower() == 'object':
result = {}
properties = schema.get('properties', {})
self.logger.debug(f"{log_prefix}Generating object data for{context_log}. Properties: {list(properties.keys())}")
@@ -78,7 +79,8 @@ class DataGenerator:
result['additionalProp1'] = self.generate_data_from_schema(additional_properties, f"{context_name}.additionalProp1", operation_id)
return result
elif schema_type == 'array':
# Handle both 'array' and 'Array' (case-insensitive)
elif schema_type and schema_type.lower() == 'array':
items_schema = schema.get('items', {})
min_items = schema.get('minItems', 1)
self.logger.debug(f"{log_prefix}Generating array data for{context_log}. Items schema: {items_schema}, minItems: {min_items}")
@@ -90,7 +92,8 @@ class DataGenerator:
generated_array.append(self.generate_data_from_schema(items_schema, item_context, operation_id))
return generated_array
elif schema_type == 'string':
# Handle both 'string' and 'String' (case-insensitive)
elif schema_type and schema_type.lower() == 'string':
string_format = schema.get('format', '')
if 'enum' in schema and schema['enum']: return schema['enum'][0]
if string_format == 'date': return datetime.date.today().isoformat()
@@ -99,12 +102,14 @@ class DataGenerator:
if string_format == 'uuid': return str(uuid.uuid4())
return 'example_string'
elif schema_type in ['number', 'integer']:
# Handle both 'number'/'Number' and 'integer'/'Integer' (case-insensitive)
elif schema_type and schema_type.lower() in ['number', 'integer']:
minimum = schema.get('minimum')
if minimum is not None: return minimum
return 0 if schema_type == 'integer' else 0.0
return 0 if schema_type.lower() == 'integer' else 0.0
elif schema_type == 'boolean':
# Handle both 'boolean' and 'Boolean' (case-insensitive)
elif schema_type and schema_type.lower() == 'boolean':
return schema.get('default', False)
elif schema_type == 'null':
+32 -23
View File
@@ -415,35 +415,37 @@ def generate_mismatched_value(
# 优先考虑 schema 中的 enum,选择一个不在 enum 中且类型不匹配的值
if field_schema and "enum" in field_schema and isinstance(field_schema["enum"], list):
enum_values = field_schema["enum"]
if original_type == "string":
# Handle case-insensitive type comparisons
if original_type and original_type.lower() == "string":
if 123 not in enum_values: return 123
if False not in enum_values: return False
# 如果数字和布尔都在枚举中,尝试一个与已知枚举值不同的字符串
# (虽然这仍然是字符串类型,但目的是为了触发非枚举值的验证)
# 或者,如果目的是严格类型不匹配,这里应该返回非字符串。
# 当前逻辑倾向于返回一个肯定非字符串的值。
elif original_type == "integer":
elif original_type and original_type.lower() == "integer":
if "not-an-integer" not in enum_values: return "not-an-integer"
if 3.14 not in enum_values: return 3.14
elif original_type == "number": # Includes float/double
if 3.14 not in enum_values: return 3.14
elif original_type and original_type.lower() == "number": # Includes float/double
if "not-a-number" not in enum_values: return "not-a-number"
elif original_type == "boolean":
elif original_type and original_type.lower() == "boolean":
if "not-a-boolean" not in enum_values: return "not-a-boolean"
if 1 not in enum_values: return 1
if 1 not in enum_values: return 1
# 如果枚举覆盖了所有简单备选,则回退到下面的通用逻辑
# 通用类型不匹配逻辑 (当 enum 不存在或 enum 检查未返回时)
if original_type == "string":
# Handle case-insensitive type comparisons
if original_type and original_type.lower() == "string":
return 12345 # Number instead of string
elif original_type == "integer":
elif original_type and original_type.lower() == "integer":
return "not-an-integer" # String instead of integer
elif original_type == "number": # Includes float/double
elif original_type and original_type.lower() == "number": # Includes float/double
return "not-a-number" # String instead of number
elif original_type == "boolean":
elif original_type and original_type.lower() == "boolean":
return "not-a-boolean" # String instead of boolean
elif original_type == "array":
elif original_type and original_type.lower() == "array":
return {"value": "not-an-array"} # Object instead of array
elif original_type == "object":
elif original_type and original_type.lower() == "object":
return ["not", "an", "object"] # Array instead of object
effective_logger.warning(f"generate_mismatched_value: 原始类型 '{original_type}' 未知或无法生成不匹配值。将返回固定字符串 'mismatch_test_default'")
@@ -552,7 +554,8 @@ def find_first_simple_type_field_recursive(
schema_type = current_schema.get("type")
# effective_logger.debug(f"Searching in path: {'.'.join(map(str, path_so_far))}, Schema Type: '{schema_type}'")
if schema_type == "object":
# Handle both 'object' and 'Object' (case-insensitive)
if schema_type and schema_type.lower() == "object":
properties = current_schema.get("properties", {})
for name, prop_schema in properties.items():
if not isinstance(prop_schema, dict):
@@ -560,11 +563,12 @@ def find_first_simple_type_field_recursive(
continue
prop_type = prop_schema.get("type")
if prop_type in ["string", "integer", "number", "boolean"]:
# Handle case-insensitive type checking for simple types
if prop_type and prop_type.lower() in ["string", "integer", "number", "boolean"]:
field_path = path_so_far + [name]
effective_logger.info(f"Found simple type field: Path={'.'.join(map(str, field_path))}, Type={prop_type}")
return field_path, prop_type, prop_schema
elif prop_type == "object":
elif prop_type and prop_type.lower() == "object":
found_in_nested_object = find_first_simple_type_field_recursive(
prop_schema,
path_so_far + [name],
@@ -572,16 +576,17 @@ def find_first_simple_type_field_recursive(
)
if found_in_nested_object:
return found_in_nested_object
elif prop_type == "array":
elif prop_type and prop_type.lower() == "array":
items_schema = prop_schema.get("items")
if isinstance(items_schema, dict):
# Look for simple type or object within array items
item_type = items_schema.get("type")
if item_type in ["string", "integer", "number", "boolean"]:
# Handle case-insensitive type checking for simple types
if item_type and item_type.lower() in ["string", "integer", "number", "boolean"]:
field_path = path_so_far + [name, 0] # Target first item of the array
effective_logger.info(f"Found simple type field in array item: Path={'.'.join(map(str, field_path))}, Type={item_type}")
return field_path, item_type, items_schema
elif item_type == "object":
elif item_type and item_type.lower() == "object":
# Path to the first item of the array, then recurse into that item's object schema
found_in_array_item_object = find_first_simple_type_field_recursive(
items_schema,
@@ -591,15 +596,17 @@ def find_first_simple_type_field_recursive(
if found_in_array_item_object:
return found_in_array_item_object
elif schema_type == "array": # If the current_schema itself is an array (e.g., root schema is an array)
# Handle both 'array' and 'Array' (case-insensitive)
elif schema_type and schema_type.lower() == "array": # If the current_schema itself is an array (e.g., root schema is an array)
items_schema = current_schema.get("items")
if isinstance(items_schema, dict):
item_type = items_schema.get("type")
if item_type in ["string", "integer", "number", "boolean"]:
# Handle case-insensitive type checking for simple types
if item_type and item_type.lower() in ["string", "integer", "number", "boolean"]:
field_path = path_so_far + [0] # Target first item of this root/current array
effective_logger.info(f"Found simple type field in root/current array item: Path={'.'.join(map(str, field_path))}, Type={item_type}")
return field_path, item_type, items_schema
elif item_type == "object":
elif item_type and item_type.lower() == "object":
# Path to the first item of this root/current array, then recurse
found_in_root_array_item_object = find_first_simple_type_field_recursive(
items_schema,
@@ -684,13 +691,15 @@ def util_find_ranged_field_recursive(
return path, schema, schema_type, min_val, max_val, description
# 递归
if schema_type == 'object' and 'properties' in schema:
# Handle both 'object' and 'Object' (case-insensitive)
if schema_type and schema_type.lower() == 'object' and 'properties' in schema:
for prop_name, prop_schema in schema['properties'].items():
result = util_find_ranged_field_recursive(prop_schema, path + [prop_name], effective_logger)
if result:
return result
if schema_type == 'array' and 'items' in schema and isinstance(schema['items'], dict):
# Handle both 'array' and 'Array' (case-insensitive)
if schema_type and schema_type.lower() == 'array' and 'items' in schema and isinstance(schema['items'], dict):
result = util_find_ranged_field_recursive(schema['items'], path + [0], effective_logger)
if result:
return result