Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Scriptrunner - StackOverflowError was thrown during script result serialization to JSON.

Steven Mustari
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 19, 2021

Using the following code to write a job to post notifications to Slack I am receiving an error I am not familiar with. Is there a better way to go about this? This code will eventually post notifications to a channel based on a filter for each result in the filter on scheduled intervals. The code is not complete but I am recieving the titled error after running it, although my initial test shows a working result. (Single issue)

I have not yet actually tested in the loop.

The error after run is:


StackOverflowError was thrown during script result serialization to JSON. This error is usually caused by attempt to serialize object graph with cycles. Please convert your script result to serializable object tree.

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.config.properties.APKeys
import groovyx.net.http.ContentType
import groovyx.net.http.RESTClient
import groovyx.net.http.HttpResponseDecorator
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.issue.customfields.option.Option
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.fields.config.FieldConfig
import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.jql.parser.JqlQueryParser
import com.atlassian.jira.web.bean.PagerFilter

log.setLevel(org.apache.log4j.Level.DEBUG)

final webhookPath = '/services/XXXXX' // it-jira-dev
final webhookBase = 'https://hooks.slack.com'

def jiraBaseUrl = ComponentAccessor.applicationProperties.getString(APKeys.JIRA_BASEURL)
def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser)
def searchService = ComponentAccessor.getComponent(SearchService)
def issueManager = ComponentAccessor.getIssueManager()
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

def query = jqlQueryParser.parseQuery("Key in (EDCO-117335)") //"Project = EDCO and Type = Escalation and 'Assigned Team' = 'Software Engineering' and resolution is empty and reporter in membersOf('jira-team-service_desk')")

def search = searchService.search(user, query, PagerFilter.getUnlimitedFilter())

log.debug("Total issues: ${search.total}")

search.results.each {documentIssue ->
log.debug(documentIssue.key)

def issue = issueManager.getIssueObject(documentIssue.id)

log.debug("${issue.key}")

//Issue detail augmentation
String summary = issue.summary

String textBlock = "${issue.issueType.name}: <$jiraBaseUrl/browse/$issue.key|${summary}>"

def body = [
text: summary,
blocks: [
[
type: 'section',
text: [
type: 'mrkdwn',
text: textBlock
]
]
]
]

def resp = new RESTClient(webhookBase).post(
path: webhookPath,
contentType: ContentType.HTML,
body: body,
requestContentType: ContentType.JSON
) as HttpResponseDecorator

assert resp.status == 200: "Request failed with status $resp.status. $resp.entity.content.text"
}

Screen Shot 2021-03-19 at 4.28.05 PM.png
Thank you for any help on this!

2 answers

1 accepted

4 votes
Answer accepted
Matthew Clark
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 20, 2021

Hi Steven

 

This is related to this bug Issue here.

The data returned by your script contains cycles.

See the comment from `Tomasz` on the bug ticket.

 

Script should return result, which doesn't have cycles in object graph, effectively it must be a tree. This is the common requirement to serialize objects to JSON.

 

The error you are reporting is the error we added from ScriptRunner 6.16.0 onwards to warn the user that the return type of the job script must be a serializable object tree

In your case, the return type of the script is likely not important anyway, so you can stop this error by just adding null to the last line of the script so its return type is null and it will not try to serialize the cycling object.

Regards

Matthew

Steven Mustari
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 30, 2021

@Matthew Clark

Thank you this was the exact problem.

Like # people like this
0 votes
PD Sheehan
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 19, 2021

That looks like perhaps the built-it JSON serializer in the HttpBuilder is getting confused.

I don't know if the RESTClient can work like that, but you can try to serialize the body yourself and send it as plain text.

Something like this

import groovy.json.JsonBuilder
def jsonBody =  new JsonBuilder(body).toPrettyString()

def resp = new RESTClient(webhookBase).post(
path: webhookPath,
contentType: ContentType.HTML,
body: jsonBody,
requestContentType: ContentType.TEXT //not sure if you should keep JSON or not
) as HttpResponseDecorator

I've had issues like this where I was successful with serializing then de-serializing ...

def newBody = new JsonSlurper().parseText(new JsonBuilder(body).toString())

In my case, it had turned out to have been a Gstring with fancy interpolation. By adding a toString() in building my body, it resolved it. But the serializing/deserializing issue was a good temporary workaround. I don't see this applying to you since you typed your variable as String.

Suggest an answer

Log in or Sign up to answer