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

JSON marshal Escape #1127

Open
salmonsec opened this issue Dec 13, 2023 · 2 comments
Open

JSON marshal Escape #1127

salmonsec opened this issue Dec 13, 2023 · 2 comments
Assignees
Labels
bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. kind/bug A reported bug.

Comments

@salmonsec
Copy link

Is there a particular reason for using fmt %q to return the inner Raw Json struct for a JSON type field?

// MarshalJSON returns m as the JSON encoding of m.
func (m JSON) MarshalJSON() ([]byte, error) {
	if m == nil {
		return []byte("null"), nil
	}
	return []byte(fmt.Sprintf("%q", m)), nil
}

I've found that when I try to marshal a generated data type with a JSON field, I get results like this:

{"id":1,"customer_id":1,"data":[{"id":1,"context_bundle_id":1,"plugin_name":"test","method_name":"test","data":"{\"Objects\":{\"list/default/pod/app-9178234h-123n128\":{\"metadata\":{\"creationTimestamp\":null},\"template\":{\"metadata\":{\"creationTimestamp\":null},\"spec\":{\"containers\":[{\"name\":\"test\",\"image\":\"test\",\"resources\":{}}]}}}}}"}]}

Where data.data is of type JSON, it is escaping the quotes (treating it as a string) rather than printing the JSON object. This adds an extra step of processing at the end to have a generic JSON viewer display this correctly.

When MarshalJSON is replaced with a copy of the built-in functionality, it is written as expected.

func (m JSON) MarshalJSON() ([]byte, error) {
	if m == nil {
		return []byte("null"), nil
	}
	return m, nil
}

Result:

{"id":1,"customer_id":1,"data":[{"id":1,"context_bundle_id":1,"plugin_name":"test","method_name":"test","data":{"Objects":{"list/default/pod/app-9178234h-123n128":{"metadata":{"creationTimestamp":null},"template":{"metadata":{"creationTimestamp":null},"spec":{"containers":[{"name":"test","image":"test","resources":{}}]}}}}}}]}
@steebchen
Copy link
Owner

Thanks for the report. The reason %q is used internally is because the Prisma query engine stringifies JSON. However, this should obviously not happen in the marshal func for the end user. I'll look into it in the next few days.

@steebchen steebchen added bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. kind/bug A reported bug. labels Dec 14, 2023
@steebchen steebchen self-assigned this Dec 14, 2023
@hypecode-tech
Copy link

hypecode-tech commented Sep 24, 2024

Is this bug still open ? Because our team while they have been developing an api faced with this issue. Data have been written in postgesql and stored as json object when they fetch data from db it gives string json? marked field in schema.prisma. Could you inform me ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. kind/bug A reported bug.
Projects
Development

No branches or pull requests

3 participants