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

rawjson-as-bytes returns a json.RawMessage type, should be []byte #1022

Closed
richardpark-msft opened this issue Sep 8, 2023 · 2 comments
Closed

Comments

@richardpark-msft
Copy link
Member

We had a discussion about this recently with the CloudEvent type - we didn't want to expose json.RawMessage since it'd potentially cause issues in the future when a new JSON package comes around.

I think it's as simple as using a []byte() around the returned value since json.RawMessage == []byte.

func (e *Event) UnmarshalJSON(data []byte) error {
	var rawMsg map[string]json.RawMessage
	if err := json.Unmarshal(data, &rawMsg); err != nil {
		return fmt.Errorf("unmarshalling type %T: %v", e, err)
	}
	for key, val := range rawMsg {
		var err error
		switch key {
		case "data":
			e.Data = val    // <--- I believe this is incorrect - we don't want to surface json.RawMessage as a type. 
			delete(rawMsg, key)
@richardpark-msft
Copy link
Member Author

(offline discussion, but want to mention that the Data field is typed any).

@richardpark-msft
Copy link
Member Author

Oops, okay - here's the deal:

If you choose raw-json-as-bytes Data is also changed to be of type []byte, making the assignment I outlined above safe. So we can close this one and I lose my dreams of filing a valid autorest bug.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant