Sanitize Flask blueprint identifiers

This commit is contained in:
2026-01-18 18:03:56 +00:00
parent 91969e8494
commit 01f81ffb35

View File

@@ -4,11 +4,20 @@ export function generateFlaskBlueprint(blueprint: FlaskBlueprint): string {
let code = `from flask import Blueprint, request, jsonify\n`
code += `from typing import Dict, Any\n\n`
const blueprintVarName = blueprint.name.toLowerCase().replace(/\s+/g, '_')
const sanitizeName = (value: string): string => {
const normalized = value
.toLowerCase()
.replace(/[^a-z0-9_]+/gi, '_')
.replace(/_+/g, '_')
const safe = normalized.length > 0 ? normalized : '_'
return /^[a-z_]/i.test(safe) ? safe : `_${safe}`
}
const blueprintVarName = sanitizeName(blueprint.name)
code += `${blueprintVarName}_bp = Blueprint('${blueprintVarName}', __name__, url_prefix='${blueprint.urlPrefix}')\n\n`
blueprint.endpoints.forEach(endpoint => {
const functionName = endpoint.name.toLowerCase().replace(/\s+/g, '_')
const functionName = sanitizeName(endpoint.name)
code += `@${blueprintVarName}_bp.route('${endpoint.path}', methods=['${endpoint.method}'])\n`
code += `def ${functionName}():\n`
code += ` """\n`