Skip to content

The generated patch is not correct when this example #180

@imfms

Description

@imfms

Specifications

  • zjsonpatch version: 0.4.14
  • java version: 11
  • jdk
    • jdk11(my local env: 11.0.15 2022-04-19 LTS)
    • openjdk11(docker: adoptopenjdk/maven-openjdk11, See the following problem reproduction steps)

Expected Behavior / Actual Behavior

{
  "source": {
    "1": 1,
    "3": 4
  },
  "target": {
    "2": 4,
    "5": "asdf"
  },
  "patch": [
    {
      "op": "remove",
      "path": "/1"
    },
    {
      "op": "move",
      "from": "/3",
      "path": "/1" // should be "path": "/2"
    },
    {
      "op": "add",
      "path": "/5",
      "value": "asdf"
    }
  ],
  "patched": {
    "1": 4, // should be "2": 4
    "5": "asdf"
  }
}

Steps to Reproduce the Problem

  1. test-case.sh
mkdir -p test-case/src/test/java/com/example/test/
cd test-case
(cat << "EOF"
package com.example.test;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.flipkart.zjsonpatch.JsonDiff;
import com.flipkart.zjsonpatch.JsonPatch;
import org.junit.Test;
import java.util.LinkedHashMap;
import java.util.Map;
import static org.junit.Assert.assertEquals;

public class TestJsonPatch {
    @Test
    public void testPatch() throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();

        JsonNode source = objectMapper.valueToTree(Map.of(
                "1", 1,
                "3", 4
        ));
        JsonNode target = objectMapper.valueToTree(Map.of(
                "2", 4,
                "5", "asdf"
        ));

        JsonNode patch = JsonDiff.asJson(source, target);
        JsonNode patched = JsonPatch.apply(patch, source);

        System.out.println(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(new LinkedHashMap<>() {{
            put("source", source);
            put("target", target);
            put("patch", patch);
            put("patched", patched);
        }}));

        assertEquals(target, patched);
    }
}
EOF
)>src/test/java/com/example/test/TestJsonPatch.java
(cat << "EOF"
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>test</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>test</name>
    <description>test</description>
    <properties>
        <java.version>11</java.version>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>
        <dependency>
            <groupId>com.flipkart.zjsonpatch</groupId>
            <artifactId>zjsonpatch</artifactId>
            <version>0.4.14</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>
EOF
)>pom.xml
mvn test
  1. run test: can replay in play-with-docker quickly
docker run -v $PWD/test-case.sh:/data/test-case.sh adoptopenjdk/maven-openjdk11 sh /data/test-case.sh
  1. test result:

Mostly fails, occasionally succeeds

org.opentest4j.AssertionFailedError: 
Expected :{"5":"asdf","2":4}
Actual   :{"1":4,"5":"asdf"}

{
  "source" : {
    "1" : 1,
    "3" : 4
  },
  "target" : {
    "5" : "asdf",
    "2" : 4
  },
  "patch" : [ {
    "op" : "remove",
    "path" : "/1"
  }, {
    "op" : "move",
    "from" : "/3",
    "path" : "/1"
  }, {
    "op" : "add",
    "path" : "/5",
    "value" : "asdf"
  } ],
  "patched" : {
    "1" : 4,
    "5" : "asdf"
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions