Skip to content

jsonschema patch includes defaults for unmatch oneOf cases #640

@edgare-srt

Description

@edgare-srt

Describe the bug

While using json_schema::validate to generate a patch, I'm seeing some unexpected default values in the resulting json patch. Namely in a oneOf type where an unmatched object includes a default value, the unmatched default value will be included in the generated patch. When applied the patch could then make the json invalid.

I wouldn't expect defaults under unmatched oneOf cases to be included in the patch.

Include a small, self-contained example if possible

Sample unit test:

// Distributed under the Boost license, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

// See https://github.com/danielaparker/jsoncons for latest version

#include <jsoncons_ext/jsonschema/jsonschema.hpp>
#include <jsoncons_ext/jsonpatch/jsonpatch.hpp>
#include <jsoncons_ext/jsonschema/common/validator.hpp>

#include <jsoncons/json.hpp>
#include <jsoncons/utility/byte_string.hpp>

#include <catch/catch.hpp>

using jsoncons::json;
namespace jsonschema = jsoncons::jsonschema;

TEST_CASE("jsonschema oneOf patch")
{
    json schema = json::parse(R"(
{
  "$id": "https://example.com/oneOf",
  "$schema": "http://json-schema.org/draft-07/schema#",
  "oneOf": [
    {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "foo": {
          "default": "foo-default"
        }
      }
    },
    {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "bar": {
          "default": "bar-default"
        }
      }
    }
  ]
}
)");

    const json data = json::parse(R"(
{
  "bar": "bar-custom"
}
)");

    const jsonschema::json_schema<json> compiled = jsonschema::make_json_schema(std::move(schema)); 

    const json expectedPatch = json::array();

    json resultPatch;
    compiled.validate(data, resultPatch);

    CHECK(expectedPatch == resultPatch);
}

The patch check fails, with the patch includes the foo default value from the unmatched oneOf case:

jsoncons/test/jsonschema/src/jsonschema_patch_oneof_tests.cpp:61: FAILED:
  CHECK( expectedPatch == resultPatch )
with expansion:
  []
  ==
  [{"op":"add","path":"/foo","value":"foo-default"}]

What compiler, architecture, and operating system?

  • Compiler: g++ (GCC) 11.5.0 20240719 (Red Hat 11.5.0-5)
  • Architecture (e.g. x86, x64) x64
  • Operating system: RHEL 9.6

What jsoncons library version?

  • Latest release 1.4.2
  • Other release 1.3.2
  • master

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions