arakelian / java-jq

Lightweight Java wrapper around JQ, a flexible JSON processor available for multiple platforms
MIT License
82 stars 10 forks source link

Query works on M1 Mac but fails in Jenkins #28

Open jimshowalter opened 3 months ago

jimshowalter commented 3 months ago

First, thank you very much for this wonderful library! We had feared needing to write one, or having to live with using exec to run jq from the command line.

The problem we ran into is that a query that works fine on an M1 Mac fails in Jenkins running on a t3.medium image in AWS: https://aws.amazon.com/ec2/instance-types/

Is this perhaps due to not having a binding for that particular hardware?

This unit test shows the problem (when executed in Jenkins):

package org.problem;

import com.arakelian.jq.ImmutableJqLibrary;
import com.arakelian.jq.ImmutableJqRequest;
import com.arakelian.jq.JqResponse;
import org.junit.jupiter.api.Test;

class Problem {

  static final String INPUT =
      "{\n"
          + "    \"apiVersion\": 1,\n"
          + "    \"kind\": \"Foo\",\n"
          + "    \"spec\": {\n"
          + "        \"workloads\": {\n"
          + "            \"settings\": {\n"
          + "                \"clusters\": {}\n"
          + "            }\n"
          + "        }\n"
          + "    }\n"
          + "}";

  static final String UNHAPPY_JQ =
      "del(.spec.workloads.[].clusters[].envs.[].tracks.[].containers[].useArtifactoryCacheServers) | .spec.workloads.[].clusters[].envs.[].tracks.[] |= with_entries(if .key == \"featureFlags\" then .key = \"rolloutFlags\" else . end)";

  static String responseOutput(JqResponse response) {
    if (response == null) {
      throw new RuntimeException("JQ response is null");
    }
    if (response.hasErrors()) {
      throw new RuntimeException(response.getErrors().toString());
    }
    return response.getOutput();
  }

  static String doQuery(String input, String query) {
    JqResponse response =
        ImmutableJqRequest.builder()
            .lib(ImmutableJqLibrary.of())
            .input(input)
            .filter(query)
            .build()
            .execute();
    return responseOutput(response);
  }

  @Test
  void simpleQueryWorks() {
    doQuery("{\"a\":[1,2,3,4,5],\"b\":\"hello\"}", ".");
  }

  @Test
  void makeProblemHappen() {
    doQuery(INPUT, UNHAPPY_JQ);
  }
}

The error is:

[jq: error: syntax error, unexpected '[', expecting FORMAT or QQSTRING_START (Unix shell quoting issues?) at <top-level>, line 1:
del(.spec.workloads.[].clusters[].envs.[].tracks.[].containers[].useArtifactoryCacheServers) | .spec.workloads.[].clusters[].envs.[].tracks.[] |= with_entries(if .key == "featureFlags" then .key = "rolloutFlags" else . end), jq: 1 compile error]
arakelian commented 3 months ago

Thanks for nice comment, and bug report. This falls under the heading "no good deed goes unpunished" 😄 I'm guessing that the version of jq that I've embedded for M1 is jq 1.7, and that the others are jq 1.5 or 1.6