Skip to content

fix(goctl/swagger): emit items for nested interface arrays#5553

Open
0xC3B6 wants to merge 1 commit intozeromicro:masterfrom
0xC3B6:fix/goctl-swagger-nested-array
Open

fix(goctl/swagger): emit items for nested interface arrays#5553
0xC3B6 wants to merge 1 commit intozeromicro:masterfrom
0xC3B6:fix/goctl-swagger-nested-array

Conversation

@0xC3B6
Copy link
Copy Markdown

@0xC3B6 0xC3B6 commented Apr 26, 2026

Fixes #5426

Summary

  • Map API interface{} types to Swagger object schemas when building array item schemas.
  • Preserve nested array item chains for shapes like [][]interface{} so generated Swagger 2.0 includes the required nested items field.

Test Plan

  • GOWORK=/tmp/go-zero-swagger-work/go.work GOCACHE=/tmp/go-build GOPROXY=https://goproxy.cn,direct go test ./api/swagger

@kevwan kevwan added kind/bug Something isn't working area/goctl Categorizes issue or PR as related to goctl. labels Apr 30, 2026
@kevwan
Copy link
Copy Markdown
Contributor

kevwan commented Apr 30, 2026

Review

Clean and targeted fix for swagger generation of interface{} array types (fixes #5426).

Problem: itemFromGoType() had no case for apiSpec.InterfaceType, causing the function to return nil for []interface{} fields. This meant the generated Swagger had items: null, which is invalid per OpenAPI 2.0 spec.

Changes:

  1. itemFromGoType — Added case apiSpec.InterfaceType that returns {type: "object"}. This is the correct representation: interface{} can be any JSON value, and object is the closest Swagger 2.0 approximation.

  2. typeFromGoType / sampleTypeFromGoType — Added apiSpec.InterfaceType to the struct/map case, so these two helpers also return "object" for interface{} values. This ensures consistency across all three type-mapping functions.

Test: TestNestedArrayInterfaceItems validates a [][]interface{} field, confirming the nested array has correct items → items → {type: object} chain.

One note: Using "object" for interface{} is a pragmatic choice — the technically correct Swagger 2.0 representation would be to omit type constraints (empty schema {}), which matches any JSON type. However, "object" is widely accepted and consistent with how other generators handle this case.

LGTM — the fix is minimal, correct, and well-tested.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/goctl Categorizes issue or PR as related to goctl. kind/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug(api): Nested Array Types Generate Swagger Missing items Field

2 participants