[GH-ISSUE #6411] [Question]: should an empty allLinks response return an empty array or null? #4342

Closed
opened 2026-10-31 20:38:58 +07:00 by bsakaguchi · 1 comment
Owner

Originally created by @LineGM on GitHub (Sep 4, 2026).
Original GitHub issue: https://github.com/MHSanaei/3x-ui/issues/6411

What are you trying to do?

I am trying to clarify the intended response contract of:

GET /panel/api/inbounds/allLinks

When no links are available, should a successful response contain "obj": null or "obj": []?

A consistent array response would be easier for generated SDKs and strictly typed API clients. However, changing null to [] would technically alter the existing wire response for the empty case, so I would like to confirm the intended behavior first.

If [] is preferred, I am happy to prepare a small PR with the implementation, regression tests, and an explicit OpenAPI string[] response schema.

What have you already tried?

  • Checked the current main branch after #6409.
  • Traced the endpoint through getAllInboundLinks, GetAllInboundLinks, and LinksForInbounds.
  • Confirmed that LinksForInbounds currently starts with a nil Go slice, so an empty successful result is serialized as null.
  • Confirmed that the API documentation shows a non-empty array example, but the generated OpenAPI schema for obj is currently unconstrained.
  • Checked the existing open issues and pull requests and did not find a duplicate discussion.

3x-ui version

main @ ed6bc1d

How did you install 3x-ui?

Manual build from source

Operating system

No response

Screenshots or config snippets

Relevant implementation:

https://github.com/MHSanaei/3x-ui/blob/main/internal/sub/links.go#L44-L50

func (p *LinkProvider) LinksForInbounds(host string, inbounds []*model.Inbound) []string {
    svc := p.build(host)
    var out []string
    for _, inbound := range inbounds {
        out = append(out, svc.inboundLinks(inbound)...)
    }
    return out
}

Before submitting

  • I read the Wiki and searched existing issues.
  • This is a usage question, not a bug report.
  • This question is written in English.
Originally created by @LineGM on GitHub (Sep 4, 2026). Original GitHub issue: https://github.com/MHSanaei/3x-ui/issues/6411 ### What are you trying to do? I am trying to clarify the intended response contract of: `GET /panel/api/inbounds/allLinks` When no links are available, should a successful response contain `"obj": null` or `"obj": []`? A consistent array response would be easier for generated SDKs and strictly typed API clients. However, changing `null` to `[]` would technically alter the existing wire response for the empty case, so I would like to confirm the intended behavior first. If `[]` is preferred, I am happy to prepare a small PR with the implementation, regression tests, and an explicit OpenAPI `string[]` response schema. ### What have you already tried? - Checked the current `main` branch after #6409. - Traced the endpoint through `getAllInboundLinks`, `GetAllInboundLinks`, and `LinksForInbounds`. - Confirmed that `LinksForInbounds` currently starts with a nil Go slice, so an empty successful result is serialized as `null`. - Confirmed that the API documentation shows a non-empty array example, but the generated OpenAPI schema for `obj` is currently unconstrained. - Checked the existing open issues and pull requests and did not find a duplicate discussion. ### 3x-ui version main @ ed6bc1d ### How did you install 3x-ui? Manual build from source ### Operating system _No response_ ### Screenshots or config snippets Relevant implementation: https://github.com/MHSanaei/3x-ui/blob/main/internal/sub/links.go#L44-L50 ```go func (p *LinkProvider) LinksForInbounds(host string, inbounds []*model.Inbound) []string { svc := p.build(host) var out []string for _, inbound := range inbounds { out = append(out, svc.inboundLinks(inbound)...) } return out } ``` ### Before submitting - [x] I read the [Wiki](https://github.com/MHSanaei/3x-ui/wiki) and searched [existing issues](../issues?q=is%3Aissue). - [x] This is a usage question, not a bug report. - [x] This question is written in English.
bsakaguchi 2026-10-31 20:38:58 +07:00
  • closed this issue
  • added the
    question
    label
Author
Owner

@github-actions[bot] commented on GitHub (Sep 4, 2026):

Your trace is correct, and the null you observed is not specific to allLinks — it is the panel-wide envelope behaviour, so the answer is that the current wire shape is intended and should stay null. The gap actually worth closing is the one you named second: the unconstrained OpenAPI schema for obj. The repo already has an established pattern for exactly that case.

obj is any with no omitempty, so it is always present and always nullable:

// internal/web/entity/entity.go:15-19
type Msg struct {
	Success bool   `json:"success"`
	Msg     string `json:"msg"`
	Obj     any    `json:"obj"`
}

Every error response already emits "obj": null — jsonMsg passes a nil object unconditionally (internal/web/controller/util.go:121-123):

func jsonMsg(c *gin.Context, msg string, err error) {
	jsonMsgObj(c, msg, nil, err)
}

So a strictly typed client already has to tolerate obj: null on /panel/api/inbounds/allLinks regardless of what the success path does.

And the nil-slice-in, null-out shape is uniform across list endpoints, not an oversight in one:

  • internal/sub/links.go:46 — var out []string (the line you quoted)
  • internal/sub/service.go:376 — return nil on the client-fetch error, and :381 var out []string
  • internal/web/service/inbound.go:168 — var inbounds []*model.Inbound, so GET /panel/api/inbounds/list returns "obj": null for a user with zero inbounds
  • internal/logger/logger.go:221 — var output []string, so POST /panel/api/server/logs/:count returns null when nothing in the buffer matches the level

Changing LinksForInbounds alone to make([]string, 0) would make allLinks the single endpoint in the API that behaves differently, without removing the obj: null case a generated SDK has to handle anyway. The only in-tree consumer already defends against it (frontend/src/pages/inbounds/InboundsPage.tsx:398):

const links = msg?.success && Array.isArray(msg.obj) ? (msg.obj as string[]) : [];

For the record, the empty case is reachable: no inbounds, inbounds with no clients, or only protocols with no URL form (socks/http/mixed/wireguard/dokodemo/tunnel).

The schema, which is the part that is genuinely missing

frontend/scripts/build-openapi.mjs:234 supports a raw schema for obj:

if (ep.responseObjectSchema) objSchema = ep.responseObjectSchema;

and both halves of what you need are already used in-tree. A plain string array — frontend/src/pages/api-docs/endpoints.ts:712:

responseObjectSchema: { type: 'array', items: { type: 'string' } },

and nullable: true for arrays that are Go nil slices at runtime — frontend/src/pages/api-docs/websocket-events.ts:95 and :129:

loads: { type: 'array', nullable: true, items: { type: 'number' } },

Those two combined describe allLinks accurately without touching the wire:

responseObjectSchema: { type: 'array', nullable: true, items: { type: 'string' } },

Two things to fold into such a PR:

  1. POST /panel/api/server/logs/:count has the same mismatch — it is documented as a non-nullable string[] at endpoints.ts:712, but logger.GetLogs (internal/logger/logger.go:221) returns a nil slice when nothing matches. Its schema is pinned by toEqual in frontend/src/test/openapi-runtime-contracts.test.ts:131-134, so adding nullable: true there means updating that assertion too.
  2. CLAUDE.md's fourth route-contract step still applies: make gen, then copy frontend/public/openapi.json → docs/public/openapi.json and run cd docs && pnpm gen:api, since docs-ci.yml fires only on docs/**.

I could not find any commit, comment or test that states a deliberate nil-vs-empty policy either way — git log on internal/sub/links.go shows only 439245d4, 709b332d and 41645255, none of which touch the empty case. What I can point at is that your own #6409 (ed6bc1d8) changed response types only in wire-preserving ways (map[string]any → typed structs with identical JSON keys, plus example: tags) and left every empty-collection serialization alone. The final call on whether a nil → [] runtime change is wanted is the maintainer's; I am leaving this open for that, since the docs-only change stands on its own either way.

Verdict: Not a bug (expected behaviour)

This reply was generated automatically; a maintainer may follow up.

<!-- gh-comment-id:5541512093 --> @github-actions[bot] commented on GitHub (Sep 4, 2026): Your trace is correct, and the `null` you observed is not specific to `allLinks` — it is the panel-wide envelope behaviour, so the answer is that the current wire shape is intended and should stay `null`. The gap actually worth closing is the one you named second: the unconstrained OpenAPI schema for `obj`. The repo already has an established pattern for exactly that case. ### Why `null` is the envelope's behaviour, not an `allLinks` quirk `obj` is `any` with no `omitempty`, so it is always present and always nullable: ```go // internal/web/entity/entity.go:15-19 type Msg struct { Success bool `json:"success"` Msg string `json:"msg"` Obj any `json:"obj"` } ``` Every error response already emits `"obj": null` — `jsonMsg` passes a nil object unconditionally (`internal/web/controller/util.go:121-123`): ```go func jsonMsg(c *gin.Context, msg string, err error) { jsonMsgObj(c, msg, nil, err) } ``` So a strictly typed client already has to tolerate `obj: null` on `/panel/api/inbounds/allLinks` regardless of what the success path does. And the nil-slice-in, `null`-out shape is uniform across list endpoints, not an oversight in one: - `internal/sub/links.go:46` — `var out []string` (the line you quoted) - `internal/sub/service.go:376` — `return nil` on the client-fetch error, and `:381` `var out []string` - `internal/web/service/inbound.go:168` — `var inbounds []*model.Inbound`, so `GET /panel/api/inbounds/list` returns `"obj": null` for a user with zero inbounds - `internal/logger/logger.go:221` — `var output []string`, so `POST /panel/api/server/logs/:count` returns `null` when nothing in the buffer matches the level Changing `LinksForInbounds` alone to `make([]string, 0)` would make `allLinks` the single endpoint in the API that behaves differently, without removing the `obj: null` case a generated SDK has to handle anyway. The only in-tree consumer already defends against it (`frontend/src/pages/inbounds/InboundsPage.tsx:398`): ```ts const links = msg?.success && Array.isArray(msg.obj) ? (msg.obj as string[]) : []; ``` For the record, the empty case is reachable: no inbounds, inbounds with no clients, or only protocols with no URL form (socks/http/mixed/wireguard/dokodemo/tunnel). ### The schema, which is the part that is genuinely missing `frontend/scripts/build-openapi.mjs:234` supports a raw schema for `obj`: ```js if (ep.responseObjectSchema) objSchema = ep.responseObjectSchema; ``` and both halves of what you need are already used in-tree. A plain string array — `frontend/src/pages/api-docs/endpoints.ts:712`: ```ts responseObjectSchema: { type: 'array', items: { type: 'string' } }, ``` and `nullable: true` for arrays that are Go nil slices at runtime — `frontend/src/pages/api-docs/websocket-events.ts:95` and `:129`: ```ts loads: { type: 'array', nullable: true, items: { type: 'number' } }, ``` Those two combined describe `allLinks` accurately without touching the wire: ```ts responseObjectSchema: { type: 'array', nullable: true, items: { type: 'string' } }, ``` Two things to fold into such a PR: 1. `POST /panel/api/server/logs/:count` has the same mismatch — it is documented as a non-nullable `string[]` at `endpoints.ts:712`, but `logger.GetLogs` (`internal/logger/logger.go:221`) returns a nil slice when nothing matches. Its schema is pinned by `toEqual` in `frontend/src/test/openapi-runtime-contracts.test.ts:131-134`, so adding `nullable: true` there means updating that assertion too. 2. `CLAUDE.md`'s fourth route-contract step still applies: `make gen`, then copy `frontend/public/openapi.json` → `docs/public/openapi.json` and run `cd docs && pnpm gen:api`, since `docs-ci.yml` fires only on `docs/**`. I could not find any commit, comment or test that states a deliberate nil-vs-empty policy either way — `git log` on `internal/sub/links.go` shows only `439245d4`, `709b332d` and `41645255`, none of which touch the empty case. What I can point at is that your own #6409 (`ed6bc1d8`) changed response *types* only in wire-preserving ways (`map[string]any` → typed structs with identical JSON keys, plus `example:` tags) and left every empty-collection serialization alone. The final call on whether a `nil` → `[]` runtime change is wanted is the maintainer's; I am leaving this open for that, since the docs-only change stands on its own either way. Verdict: Not a bug (expected behaviour) *This reply was generated automatically; a maintainer may follow up.* <!-- claude-issue:analyst -->
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
MHSanaei/3x-ui#4342
No description provided.