Skip to content

Commit

Permalink
[FIXED JENKINS-41503] Allow null values in JSON
Browse files Browse the repository at this point in the history
Also required changing org.json -> Jackson conversion to go through a
string rather than use a mapper due to the mapper not liking nulls at
all.
  • Loading branch information
abayer committed Feb 22, 2017
1 parent c40dd71 commit 6e518e3
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 6 deletions.
3 changes: 2 additions & 1 deletion pipeline-model-api/src/main/resources/ast-schema.json
Expand Up @@ -10,7 +10,8 @@
"type": [
"number",
"string",
"boolean"
"boolean",
"null"
]
}
},
Expand Down
Expand Up @@ -24,6 +24,7 @@
package org.jenkinsci.plugins.pipeline.modeldefinition.parser

import com.cloudbees.groovy.cps.NonCPS
import com.github.fge.jsonschema.util.JsonLoader
import org.jenkinsci.plugins.pipeline.modeldefinition.shaded.com.fasterxml.jackson.databind.JsonNode
import org.jenkinsci.plugins.pipeline.modeldefinition.shaded.com.fasterxml.jackson.databind.ObjectMapper
import org.jenkinsci.plugins.pipeline.modeldefinition.shaded.com.fasterxml.jackson.datatype.jsonorg.JsonOrgModule
Expand Down Expand Up @@ -87,10 +88,7 @@ public class Converter {
* @return The converted {@link JsonNode}
*/
public static JsonNode jacksonJSONFromJSONObject(JSONObject input) {
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JsonOrgModule());

return mapper.valueToTree(input);
return JsonLoader.fromString(input.toString())
}

public static JsonTree jsonTreeFromJSONObject(JSONObject input) {
Expand Down
Expand Up @@ -139,7 +139,8 @@ public void setUp() throws Exception {
"whenEnvFalse",
"parallelPipelineWithSpaceInBranch",
"parallelPipelineQuoteEscaping",
"nestedTreeSteps"
"nestedTreeSteps",
"jsonSchemaNull"
);

public static Iterable<Object[]> configsWithErrors() {
Expand Down
@@ -0,0 +1,49 @@
{"pipeline": {
"stages": [ {
"name": "foo",
"branches": [ {
"name": "default",
"steps": [
{
"name": "sh",
"arguments": [ {
"key": "script",
"value": {
"isLiteral": true,
"value": "cat /usr/local/apache2/conf/extra/httpd-userdir.conf"
}
}]
},
{
"name": "sh",
"arguments": [ {
"key": "script",
"value": {
"isLiteral": true,
"value": "echo \"The answer is 42\""
}
}]
}
]
}]
}],
"agent": {
"type": "docker",
"arguments": [
{
"key": "image",
"value": {
"isLiteral": true,
"value": "httpd:2.4.12"
}
},
{
"key": "args",
"value": {
"isLiteral": true,
"value": null
}
}
]
}
}}
43 changes: 43 additions & 0 deletions pipeline-model-definition/src/test/resources/jsonSchemaNull.groovy
@@ -0,0 +1,43 @@
/*
* The MIT License
*
* Copyright (c) 2017, CloudBees, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

pipeline {
agent {
docker {
image "httpd:2.4.12"
args null
}
}
stages {
stage("foo") {
steps {
sh 'cat /usr/local/apache2/conf/extra/httpd-userdir.conf'
sh 'echo "The answer is 42"'
}
}
}
}



0 comments on commit 6e518e3

Please sign in to comment.