fix: asset_type参数改为强制覆盖而非验证

问题:
- 原设计要求asset_type与自动检测结果一致
- 如果自动检测逻辑有问题,用户无法覆盖

修改:
- 指定asset_type后直接使用,不再验证
- 用户指定的类型强制覆盖自动检测结果
- 返回type_override字段提示覆盖情况

返回示例(覆盖时):
{
  "asset_type": "us_index",  // 用户指定
  "type_override": {
    "detected": "hk_index",  // 自动检测结果
    "specified": "us_index", // 用户指定
    "hint": "用户强制覆盖了自动检测结果"
  }
}
This commit is contained in:
2026-05-12 23:34:36 +08:00
parent 95c7a091f5
commit cf48c4418f

View File

@@ -411,7 +411,7 @@ def index():
"asset_type": "/api/v1/asset-type?code={code}",
"ohlcv": "/api/v1/ohlcv?code={code}&start={YYYY-MM-DD}&end={YYYY-MM-DD}&asset_type={type}",
"ohlcv_nocache": "/api/v1/ohlcv?code={code}&nocache=true",
"ohlcv_asset_type": "/api/v1/ohlcv?code={code}&asset_type=china_index (验证类型)",
"ohlcv_asset_type": "/api/v1/ohlcv?code={code}&asset_type=china_index (强制覆盖类型)",
"batch": "POST /api/v1/ohlcv/batch",
"etf_nav": "/api/v1/etf/nav?code={code}",
"cache_clear": "POST /api/v1/cache/clear",
@@ -478,13 +478,14 @@ def get_ohlcv():
code: 标的代码 (required)
start: 开始日期 YYYY-MM-DD (optional, 默认90天前)
end: 结束日期 YYYY-MM-DD (optional, 默认今天)
asset_type: 资产类型 (optional, 用于验证或强制指定)
asset_type: 资产类型 (optional, 强制覆盖自动检测结果)
- china_index: 中国指数
- china_etf: 中国ETF
- us_index: 美股指数
- hk_index: 港股指数
- futures: 期货
- crypto: 加密货币
注:指定后会覆盖自动检测,用于修复检测逻辑问题
nocache: 是否跳过缓存 (optional, 默认false)
"""
code = request.args.get('code', '').strip()
@@ -516,19 +517,12 @@ def get_ohlcv():
# 自动检测资产类型
detected_type = AssetTypeDetector.detect(code)
# 如果指定了 asset_type 参数,验证是否匹配
# 最终使用的类型:优先使用用户指定的类型
final_type = detected_type
if asset_type_param:
try:
# 将字符串转换为 AssetType
expected_type = AssetType(asset_type_param)
if detected_type != expected_type:
return jsonify({
"error": f"Asset type mismatch",
"code": code,
"detected_type": detected_type.value,
"expected_type": expected_type.value,
"hint": f"代码 {code} 自动检测为 {detected_type.value}, 但指定了 {expected_type.value}",
}), 400
# 将字符串转换为 AssetType(强制覆盖自动检测结果)
final_type = AssetType(asset_type_param)
except ValueError:
return jsonify({
"error": f"Invalid asset_type: {asset_type_param}",
@@ -541,7 +535,8 @@ def get_ohlcv():
if result is None:
return jsonify({
"code": code,
"asset_type": detected_type.value,
"asset_type": final_type.value,
"detected_type": detected_type.value if asset_type_param else None, # 仅当用户指定时显示
"error": "No data available",
"start": start,
"end": end,
@@ -550,12 +545,20 @@ def get_ohlcv():
if "error" in result:
return jsonify({
"code": code,
"asset_type": detected_type.value,
"asset_type": final_type.value,
"detected_type": detected_type.value if asset_type_param else None,
"error": result["error"],
}), 500
result['cached'] = is_cached
result['asset_type'] = detected_type.value # 确保返回类型
result['asset_type'] = final_type.value # 使用最终类型
# 如果用户指定了类型但与自动检测不同,显示提示
if asset_type_param and detected_type != final_type:
result['type_override'] = {
"detected": detected_type.value,
"specified": final_type.value,
"hint": "用户强制覆盖了自动检测结果",
}
return jsonify(result)