Swagger 生成时验证路径参数与路由占位符的匹配性 (Closes #5428)#5520
Conversation
…ger generation (zeromicro#5428) Fix swagger generation to only include path parameters when the route path contains matching placeholders (:id or {id}). - Add extractPathPlaceholders() to parse both :id and {id} style placeholders - Make parametersFromType() accept routePath and filter path parameters - Unmatched path parameters are silently skipped to avoid invalid swagger output - Add comprehensive tests for placeholder extraction and validation logic
Fixes zeromicro#4710 Problem: Android's Base64.encodeToString() method adds line breaks by default, causing IllegalHeaderValueException when used with Ktor HTTP clients. HTTP headers cannot contain line breaks or other special characters. Root Cause: The DecryptBase64() function used base64.StdEncoding.DecodeString() which doesn't handle strings with newlines or whitespace. Clients using Base64.encodeToString() without NO_WRAP flag would send corrupted Base64 strings in X-Content-Security headers, causing decryption failures. Solution: Modified DecryptBase64() to strip newlines (\n), carriage returns (\r), and whitespace from Base64 strings before decoding. This makes the server compatible with: - Android Base64.encodeToString() (with default line wrapping) - Standard Base64 without wrapping - Strings with extra whitespace or carriage returns This change is backward compatible - it doesn't affect clients already sending clean Base64 strings, while enabling compatibility with clients that add line breaks. Changes: - Modified core/codec/rsa.go: DecryptBase64() method - Added test TestBase64WithNewlines() in core/codec/rsa_test.go
Strip newline characters from the inner Base64-encoded key after RSA decryption in ParseContentSecurity. This fixes compatibility with Android clients where Base64.encodeToString() adds line breaks by default (MIME-style), which causes base64.StdEncoding.DecodeString to fail on the server side. Closes zeromicro#4710
去除 ParseContentSecurity 中 signature 字段的 Base64 编码字符串中的换行符 (\n 和 \r),以支持使用 Base64.encodeToString() 默认添加换行符的平台 (如 Android)。同时添加新的测试用例验证签名带换行符时的正确性 Closes zeromicro#4710
Co-Authored-By: Oz <oz-agent@warp.dev>
|
Translation / English summary: This draft PR adds validation in
ReviewConcept: Correct and necessary — the OpenAPI spec requires all Changes:
Concerns:
Good work overall. Please mark ready for review once you've addressed the signature change concern and considered the silent-drop behavior. Also, please include an English description in your PR body to help all contributors review. |
当请求结构体中声明了
path:"id"标记的字段,但路由路径中不包含对应的占位符(如/:id或{id})时,goctl 目前仍会生成in: path的参数,导致 Swagger 文档语义不一致(OpenAPI 规范要求路径参数必须出现在 URL 模板中)。问题示例
生成的 Swagger 结果(错误):
{ "type": "string", "name": "id", "in": "path", // 参数位置为 path "required": true }但实际路径是
/foo,不包含{id},这是无效的 Swagger 文档。解决方案
在生成路径参数前,解析路由路径中的占位符(支持
:id和{id}两种格式),仅当路径中存在匹配的占位符时才生成对应的路径参数。对于未匹配的路径参数,静默跳过以避免生成无效的 Swagger 文档。核心改动
1. 新增
extractPathPlaceholders()函数 (tools/goctl/api/swagger/vars.go):id风格的 go-zero 占位符{id}风格的 OpenAPI 占位符2. 修改
parametersFromType()函数 (tools/goctl/api/swagger/parameter.go)routePath string参数,接收原始路由路径extractPathPlaceholders()获取允许的占位符集pathParameterTag.Name在占位符集合中时才生成参数3. 更新
spec2Path()函数 (tools/goctl/api/swagger/path.go)parametersFromType()时传入route.Path4. 新增单元测试
vars_test.go: 测试占位符提取逻辑(14 个测试用例):id和{id})parameter_path_filter_test.go: 测试路径参数验证逻辑(6 个测试用例)修复
parameter_test.go: 兼容新的参数签名测试结果