Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

legacyprovision: fix RespError json serialization #115

Merged
merged 1 commit into from
Feb 11, 2025
Merged

Conversation

rnons
Copy link
Contributor

@rnons rnons commented Feb 9, 2025

A POC snippet

package main

import (
	"encoding/json"
	"log"
	"net/http"

	"go.mau.fi/mautrix-meta/pkg/connector"
)

func jsonResponse(w http.ResponseWriter, status int, response any) {
	w.Header().Add("Content-Type", "application/json")
	w.WriteHeader(status)
	_ = json.NewEncoder(w).Encode(response)
}

func main() {
	err := connector.ErrLoginChallenge
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		// Returns {"ErrCode": "...", "Err": "..."}
		jsonResponse(w, err.StatusCode, err)
		// Returns {"errcode": "...", "err": "..."}
		// err.Write(w)
	})

	log.Fatal(http.ListenAndServe(":8080", nil))
}

Seems RespError becomes any in jsonResponse, so func (e *RespError) MarshalJSON() ([]byte, error) is not used.

@rnons rnons requested a review from tulir February 9, 2025 11:37
Copy link
Member

@tulir tulir left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Becoming any doesn't prevent methods from being called, the actual issue is that bridgev2.RespError doesn't have MarshalJSON at all. The fix is good either way though

@rnons rnons merged commit 0fffc12 into main Feb 11, 2025
11 checks passed
@rnons rnons deleted the rnons/plat-27418 branch February 11, 2025 23:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants