apache / apache/cloudstack-go

Generator fabricates list-response JSON wrapper tags from parseSingular(), causing recurring silent-nil-slice bugs

Đang mở
#155 0 bình luận 0 reaction 0 người được giao Xem trên GitHub
Ngôn ngữ chính
Go
Star
39
Fork
47
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Mô tả

Splitting this out as the systemic root cause behind a recurring class of bugs (#135, #136 / #137, and the four just fixed in #154).

### Problem
For `ListResponse` structs, `generate.go` derives the JSON wrapper tag from `strings.ToLower(parseSingular(listName))`. But the key CloudStack actually returns is set server-side via `setObjectName(...)`, and it is frequently **not** what `parseSingular` produces:

- camelCase: `listHypervisorCapabilities` → server `hypervisorCapabilities`, generator `hypervisorcapability`
- `-xes` plural: `…Ipv6Prefixes` → `Prefixe` (also corrupts the element **type name**), server `guestnetworkipv6prefix`
- hand-chosen keys: `listLBHealthCheckPolicies` → server `healthcheckpolicies` (drops the `lb`, stays plural), generator `lbhealthcheckpolicy`

When the tag is wrong, `json.Unmarshal` populates `Count` but leaves the slice **nil** — silent data loss — and the generated `GetByID` helper then panics on `slice[0]`.

### Current mitigation doesn’t scale
`generateResponseType` already carries a hand-maintained override `switch` (~30 cases, incl. the metrics ones) that exists precisely to patch this. Each new case is added **reactively**, only after a user hits a nil slice. #154 adds four more; there are almost certainly others not yet reported.

Note: `listApis.json` (the generator input) does **not** expose the list wrapper object name — it carries each item’s `response` fields and `related`, but not the `setObjectName` value — so the correct key can’t simply be read from there.

### Proposed directions
1. **Harden `parseSingular`** for the regular English plurals it currently mishandles — `-xes/-ses/-ches/-shes` → drop `es` (it already does `-ies`/`-sses`). This fixes the `Prefixe` type-name corruption and several tags for free, shrinking the override set.
2. **Make the override set verifiable instead of reactive:** add a CI/test check that asserts each `ListResponse` tag against the keys in the `test/testdata/*.json` fixtures (and/or a captured real response), so drift is caught at build time rather than by users in production.
3. Optionally, a curated `command → objectName` override map (cleaner than inline `switch` cases) seeded from the verified keys.

I verified the #154 keys against the CloudStack server source (`setObjectName`) and have a 4.22 environment; happy to take on (1) and/or (2) if a maintainer signals a preferred direction.

Hướng dẫn đóng góp

Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này

Hướng nghiên cứu

Bắt đầu trong generate.go, đặc biệt là parseSingular và generateResponseType, và xem lại override switch hiện có. So sánh các tags ListResponse được sinh ra với các key đã được xác minh trong test/testdata/*.json và các giá trị setObjectName phía server; listApis.json không chứa các tên wrapper đó. Được xem là hoàn tất khi cách xử lý và/hoặc xác minh số nhiều được đề xuất ngăn các slice nil im lặng mà không chỉ dựa vào các override mang tính phản ứng.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
go
Lĩnh vực
api, tooling
Loại issue
Lỗi
Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức độ hoạt động
Ít trao đổi
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
45/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.