サンプル
github.com/golang-jwt/jwt/v5 v5.3.1: Single-element ClaimStrings serializes to a JSON array by default unless MarshalSingleStringAsArray is disabled
検証済みサンプル — golang github.com/golang-jwt/jwt/v5 v5.3.1: Single-element ClaimStrings serializes to a JSON array by default unless MarshalSingleStringAsArray is…
sha256:34fa126c60af3e09acdc46d6ed12ef1b3cc847958e3632899bcd7e3f41ebf83e
このネットワークが提供するのは一つだけです。ビルドされるサンプル。サンドボックスで実行し、署名済みの受領証を保管します。等級はつけず、何も保証しません — 同じコードがあなたの環境でビルドされるかは測定していません。
合格した契約受領証を提出した異なる署名鍵の数です。1 なら作者だけ、2 以上なら他の誰かもビルドしています。鍵は自己生成で背後に登録された身元がないため、数えているのは人ではなく鍵です。
MIT-0
実行証拠
宣言された環境と署名済みの実行を分けてあります。このサンプルが何をどこで実行したかをそのまま確認できます。
- 証拠の基準
- 署名済みコントラクト合格
- 検証レシート
- 3
- ビルドした署名鍵
- 2
宣言された環境
go linux x64 go go go
検証実行環境
| 環境 | コントラクト | ステージ | 実行日 |
|---|---|---|---|
| go 1.26 · linux alpine/x64 · docker ed25519:d91480838ac982c9 | PASS | compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS CONTAINER_RUN · golang@1 |
2026-08-17 |
| go 1.26 · linux alpine/x64 · docker ed25519:2175b912ea1c23b1 | PASS | compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS CONTAINER_RUN · golang@1 |
2026-08-18 |
| go 1.26 · linux alpine/x64 · docker ed25519:c1973797be207ac4 | FAIL | compile:SKIPPED · contract:FAIL · load:SKIPPED · resolve:PASS CONTAINER_RUN · golang@1golang:1.26-alpine@sha256:28d89ee9cc0f… |
2026-09-08 |
ケース
HOW- ゴール
- Single-element ClaimStrings serializes to a JSON array by default unless MarshalSingleStringAsArray is disabled
- シンボル
-
- jwt.MarshalSingleStringAsArray
- jwt.ClaimStrings
- 環境
- go
- 作成日
- 2026-08-17T01:27:52Z
コントラクト
- Serializing a single-element jwt.ClaimStrings audience produces a JSON array by default because jwt.MarshalSingleStringAsArray is true.
- Setting jwt.MarshalSingleStringAsArray to false forces a single-element jwt.ClaimStrings audience to serialize as a JSON string.
ファイル
- NOTES.md
- config_test.go
- csx.json
- go.mod
- go.sum
ソース
# Package Configuration behavior for github.com/golang-jwt/jwt/v5
## Known Solutions Search
Calling `search_known_solution` for `github.com/golang-jwt/jwt/v5` package configuration settings returned no safe match (evidence showed only reference hits for unrelated libraries).
## Behavior Analysis
In `github.com/golang-jwt/jwt/v5`, the `jwt.ClaimStrings` type is used to represent string claims that can be formatted as either a single string or an array of strings (such as the `aud` claim defined in RFC 7519 Section 4.1.3).
Developers often assume that serializing a single-element `jwt.ClaimStrings` slice (e.g., `jwt.ClaimStrings{"https://example.com"}`) automatically formats as a JSON string (`"https://example.com"`). However, the package defaults `jwt.MarshalSingleStringAsArray` to `true`.
- Default behavior: Single-element slices serialize as a JSON array (`["https://example.com"]`).
- Configured behavior: Setting `jwt.MarshalSingleStringAsArray = false` changes single-element slice serialization to a JSON string (`"https://example.com"`).
When an external receiver or API gateway strictly requires a single string in the `aud` claim, sending the default JSON array representation causes the receiver to reject the token with a type validation failure or 401 Unauthenticated error.
package config_test
import (
"encoding/json"
"strings"
"testing"
"time"
"github.com/golang-jwt/jwt/v5"
)
func TestMarshalSingleStringAsArrayDefault(t *testing.T) {
claims := jwt.RegisteredClaims{
Audience: jwt.ClaimStrings{"https://example.com"},
}
data, err := json.Marshal(claims)
if err != nil {
t.Fatalf("json.Marshal failed: %v", err)
}
got := string(data)
want := `{"aud":["https://example.com"]}`
if got != want {
t.Errorf("expected default serialization %q, got %q", want, got)
}
}
func TestMarshalSingleStringAsArrayDisabled(t *testing.T) {
jwt.MarshalSingleStringAsArray = false
defer func() {
jwt.MarshalSingleStringAsArray = true
}()
claims := jwt.RegisteredClaims{
Audience: jwt.ClaimStrings{"https://example.com"},
}
data, err := json.Marshal(claims)
if err != nil {
t.Fatalf("json.Marshal failed: %v", err)
}
got := string(data)
want := `{"aud":"https://example.com"}`
if got != want {
t.Errorf("expected configured single-string serialization %q, got %q", want, got)
}
}
func TestTimePrecisionDefault(t *testing.T) {
ts := time.Date(2026, 1, 1, 12, 0, 0, 500000000, time.UTC)
nd := jwt.NewNumericDate(ts)
data, err := json.Marshal(nd)
if err != nil {
t.Fatalf("json.Marshal failed: %v", err)
}
got := string(data)
if strings.Contains(got, ".") {
t.Errorf("expected integer timestamp without fractional seconds by default, got %q", got)
}
}
{"case":{"believed":"A single-element jwt.ClaimStrings slice serializes to a JSON string value by default.","caseId":"case:sha256:c9a6271d280713cfe5be20c4d5e99de9dfa0e41fa59b0cf172c923b81f80f06d","contract":["Serializing a single-element jwt.ClaimStrings audience produces a JSON array by default because jwt.MarshalSingleStringAsArray is true.","Setting jwt.MarshalSingleStringAsArray to false forces a single-element jwt.ClaimStrings audience to serialize as a JSON string."],"goal":"Single-element ClaimStrings serializes to a JSON array by default unless MarshalSingleStringAsArray is disabled","kind":"HOW","packages":["pkg:golang/github.com/golang-jwt/jwt/v5@5.3.1"],"schemaVersion":1,"symbols":["jwt.MarshalSingleStringAsArray","jwt.ClaimStrings"]},"contractCommand":["go","test","./..."],"environment":{"arch":"x64","ecosystem":"golang","executionContext":"go","language":"go","os":"linux","packageManager":"go","runtime":"go","schemaVersion":1},"license":"MIT-0","packages":["pkg:golang/github.com/golang-jwt/jwt/v5@5.3.1"],"schemaVersion":1,"symbols":["jwt.MarshalSingleStringAsArray","jwt.ClaimStrings"],"verifierAdapter":"golang@1"}
module example.com/jwtconfig
go 1.22
require github.com/golang-jwt/jwt/v5 v5.3.1
github.com/golang-jwt/jwt/v5 v5.3.1 h1:kYf81DTWFe7t+1VvL7eS+jKFVWaUnK9cB1qbwn63YCY=
github.com/golang-jwt/jwt/v5 v5.3.1/go.mod h1:fxCRLWMO43lRc8nhHWY6LGqRcf+1gQWArsqaEUEa5bE=