Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions problem_details.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@ const BlankURL = "about:blank"

// ProblemDetails conforms to RFC 9457, providing a standard format for describing errors in HTTP APIs.
type ProblemDetails struct {
Type string `json:"type"`
Title string `json:"title"`
Status int `json:"status"`
Detail string `json:"detail,omitempty"`
Instance string `json:"instance,omitempty"`
Extensions map[string]interface{} `json:"extensions,omitempty"`
Type string `json:"type"`
Title string `json:"title"`
Status int `json:"status"`
Detail string `json:"detail,omitempty"`
Instance string `json:"instance,omitempty"`
// Extensions holds RFC 9457 extension members. MarshalJSON and
// UnmarshalJSON flatten them to the top level; there is no nested
// "extensions" object on the wire.
Extensions map[string]interface{} `json:"-"`
}

// ValidationErrorDetail provides structured details about a single validation error.
Expand Down
25 changes: 25 additions & 0 deletions problem_details_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,28 @@ func TestProblemDetailsMarshalJSONFlattensExtensions(t *testing.T) {
t.Fatalf("expected trace_id extension, got %#v", payload["trace_id"])
}
}

func TestProblemDetailsMarshalJSONHasNoNestedExtensions(t *testing.T) {
t.Parallel()

body, err := json.Marshal(ProblemDetails{
Type: BlankURL,
Title: "Bad Request",
Status: 400,
Extensions: map[string]interface{}{"code": "E1"},
})
if err != nil {
t.Fatalf("marshal: %v", err)
}

var payload map[string]any
if err := json.Unmarshal(body, &payload); err != nil {
t.Fatalf("unmarshal: %v", err)
}
if _, ok := payload["extensions"]; ok {
t.Fatalf("unexpected nested extensions key: %s", body)
}
if payload["code"] != "E1" {
t.Fatalf("expected flattened extension, got %s", body)
}
}
Loading