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

cannot unmarshal object into Go struct field InteractionCallback.state of type string #816

Closed
sambaiz opened this issue Oct 6, 2020 · 12 comments · Fixed by #820 or zaiminc/gocat#1082
Labels
bug [issue] bug

Comments

@sambaiz
Copy link

sambaiz commented Oct 6, 2020

Following error occurred when handling block_actions callback.

cannot unmarshal object into Go struct field InteractionCallback.state of type string

The field is documented as dictionary of objects but the field of struct is string.

https://api.slack.com/reference/interaction-payloads/block-actions#reference-interactive-components-payloads__fields

state.values or view.state.values | A dictionary of objects keyed with the block_ids of all blocks containing stateful, interactive components .

{
    "type": "block_actions",
    ...
    "state": {
        "values": {
            "<block_id>": {
                "<action_id>": {
                    "type": "static_select",
                    "selected_option": {
                        "text": {
                            "type": "plain_text",
                            "text": "foo",
                            "emoji": true
                        },
                        "value": "foo"
                    }
                }
            }
        }
    },
    ...
}
@kanata2
Copy link
Member

kanata2 commented Oct 6, 2020

Thank you for reporting.
Umm, my old app works correctly for now 🤔
The behavior may be different between existing apps and new one.
Can you share some reproducible code if you'd like?

@MathiasGr
Copy link

MathiasGr commented Oct 6, 2020

Hi there - Im seeing the same error in prod this morning - I think Slack deployed a schema change within the last 24-48h that is breaking unmarshalling.

This is happening on interaction callbacks (webhooks), the payload has changed and thus breaks Golang's quite strict JSON unmarshalling.

cannot unmarshal object into Go struct field InteractionCallback.state of type string"

EDIT:
checked broken payload and it looks exactly like the one mentioned above, and the culprit is there:

State string `json:"state,omitempty"`

State is defined as string.

I'm willing to push a fix, but such nested values are a real pain in Go. You need to decide whether it's worth the effort. For everyone looking for a quick fix, you just have to remove that line so that go doesn't try to unmarshal the State field.

EDIT 2:
Here's a fix that doesn't touch dependencies, by removing the state field before unmarshalling:

	fixMap := make(map[string]json.RawMessage)
	if err := json.Unmarshal(body, &fixMap); err != nil {
		// handle error
	}

	delete(fixMap, "state")
	fixedJSON, err := json.Marshal(fixMap)
	if err != nil {
		// handle err
	}

	// unmarshal fixedJSON into slack.InteractionCallback

@kanata2
Copy link
Member

kanata2 commented Oct 6, 2020

I may have figured out this problem.
Perhaps you receive string state if you submit from Dialogs and
you receive dictionary array state if you submit from Block Kit like HomeTab.

UPDATED: I'm sorry. I'm wrong about the type of interactions in Slack. Please ignore this comment X)

@plilius
Copy link

plilius commented Oct 7, 2020

Happening in prod for us as well. Can confirm that issue seems to be happening with block kit.
Do you think this might be what is causing issues? https://api.slack.com/changelog/2020-09-full-state-on-view-submisson-and-block-actions

@prgres
Copy link
Contributor

prgres commented Oct 7, 2020

Same issue for me, which force me to delete state from the payload as a workaround

@kanata2
Copy link
Member

kanata2 commented Oct 7, 2020

Thanks.
Let me give some consideration to how to fix.

@clockworkcoding
Copy link

I'm seeing this error for mobile users, but desktop users are currently unaffected

@prgres
Copy link
Contributor

prgres commented Oct 7, 2020

I'm seeing this error for mobile users, but desktop users are currently unaffected

The issue occurs on desktop as well.

@mzduke
Copy link
Contributor

mzduke commented Oct 8, 2020

Just my 2 cents.

This is definitely related to that update: https://api.slack.com/changelog/2020-09-full-state-on-view-submisson-and-block-actions
It was not supposed to be a breaking change but it is for Go apps (at least based on that library).

The source of the problem that the library is using InteractionCallback struct for multiple types of interactive submissions (e.g. view_submission or block_actions) and dialog_submission.
It looks like the Slack platform team forgot (or ignored) that dialog_submission already contains the state property which is a string while other types of submissions contain the new state property which is a nested structure.

The possible solution from my point of view is to write a dynamic JSON unmarshaling and based on the type of submission treat state either as a string or as a struct and put into two separate fields (e.g. stateValues for struct and keep state for string case).

Suggested earlier workaround with deletion of state will help to avoid errors, but will brake Dialogs which uses state.
And an alternative option is to abandon the support of legacy Dialogs (means remove support of string-typed state at all).

kanata2 added a commit that referenced this issue Oct 9, 2020
The type of state field in a Slack response to interactons now depends
on the type of interaction. Specifically, the correspondence is as
follows.

- type: dialog_submisson -> state: string
- type: block_actions -> state: dictionary array

This commit is trying to handle these properly while maintaining backwards
compatibility, so futureversions may change to a more approproate design.

See Also: https://api.slack.com/changelog/2020-09-full-state-on-view-submisson-and-block-actions

resolved: #816
@kanata2 kanata2 added the bug [issue] bug label Oct 9, 2020
kanata2 added a commit that referenced this issue Oct 9, 2020
The type of state field in a Slack response to interactons now depends
on the type of interaction. Specifically, the correspondence is as
follows.

- type: dialog_submisson -> state: string
- type: block_actions -> state: dictionary array

This commit is trying to handle these properly while maintaining backwards
compatibility, so futureversions may change to a more approproate design.

See Also: https://api.slack.com/changelog/2020-09-full-state-on-view-submisson-and-block-actions

resolved: #816
@alyosha
Copy link
Contributor

alyosha commented Oct 10, 2020

thanks @kanata2 for jumping on this, would be great if we could get a fix prioritized 🙏

@kanata2
Copy link
Member

kanata2 commented Oct 11, 2020

I'll merge #820 to fix this issue.
If the problem recurs, please let me know again.

@kanata2
Copy link
Member

kanata2 commented Oct 12, 2020

released v0.7.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug [issue] bug
Projects
None yet
8 participants