使用Groovy的JSONBuilder方法触发StackOverflowError

tmb3ates  于 2023-08-08  发布在  其他
关注(0)|答案(1)|浏览(128)

我相信我们遇到了一个与https://issues.apache.org/jira/browse/GROOVY-8916类似的bug,如果可以证实我的理论,这个问题发生在Groovy版本2.4.21中,而不是2.4.12中。

import groovy.json.JsonBuilder

    void update(Map request, String env=null, String apiUrl=null, String credentialsId=null){
        env = env ? env : getAthenaEnv()
        apiUrl = apiUrl ? apiUrl : (props.athena.apiUrl ?:  props.athena[env].apiUrl)
        credentialsId = credentialsId ? credentialsId : (props.athena.credentialsId ?: props.athena[env].credentialsId)

        print "Updating athena record for ${request.appName} in ${env} environment"
        request = replaceNull(request)
        withCredentials([string(credentialsId: credentialsId, variable: 'apikey')]) {
            
            def requestData = new JsonBuilder(request).toString()
           
            def sanitizer = load "src/utils/SanitizerUtil.groovy"
            def projectName = sanitizer.sanitize(request.appName)
            def requestUrl = "${apiUrl}/applications/${projectName}"
            print "requestData: ${requestData}"

            def response = httpRequest url: requestUrl,
                    customHeaders:  [[name: 'apikey', value: apikey]],
                    httpMode: 'PUT',
                    contentType: 'APPLICATION_JSON',
                    requestBody: requestData,
                    validResponseCodes: '200,404'

            print "response.status: ${response.status}"
        }
    }

个字符
是否可以确认以下内容,我是否需要升级到Groovy 2.5并使用JsonGenerator而不是基于Jira票证的JsonBuilder

wtzytmuj

wtzytmuj1#

解决方案/解决方案
JsonBuilder StackOverflowError与实际的json对象格式不正确有关,我们在调用JsonBuilder时删除了.toString()

//Original
def requestData = new JsonBuilder(request).toString()

//Change 
def requestData = new JsonBuilder(request)

字符串

相关问题