VerifyTests / Verify

Verify is a snapshot tool that simplifies the assertion of complex data models and documents.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

$ref results in nulls in VerifyJson

karol-gro opened this issue · comments

When I call VerifyJson method with JSON containing $ref tags, the result the tags are replaced with nulls.

According to http://jsonref.org/#ref this particular JSON:

{ "a": { "$id": "x", "b": 1 }, "b": 2, "c": { "$ref": "#x/b" }, "d": { "$ref": "#/b" } }

can be dereferenced this this:

{ "a": { "$id": "x", "b": 1 }, "b": 2, "c": 1, "d": 2 }

I'd expect Verify to keep JSON with $refs either in dereferenced form or original. Though, the JSON is modified in other ways. This makes it hard to test generation of JSON Schema with Verify library.

My assumption is that some Argon's mechanism to handle refs and ids kicks in.

Steps to reproduce

Run following test with (tested on .NET 8, Verify.Xunit 23.5.2, run from VS 17.8.4):

[Fact]
public async Task Ref()
{
	var schema = """{ "a": { "$id": "x", "b": 1 }, "b": 2, "c": { "$ref": "#x/b" }, "d": { "$ref": "#/b" } }""";
	await VerifyJson(schema);
}

Expected result

I'd expect the test to fail and open the diff tool with one of following contents:

dereferenced JSON

{
  a: {
    $id: x
    b: 1
  },
  b: 2,
  c: 1,
  d: 2
}

original JSON

{
  a: {
    $id: x
    b: 1
  },
  b: 2,
  c: {
    $ref: #x/b
  }
  d: {
    $ref: #/b
  }
}

Actual result

I get following:

{
  a: {
    b: 1
  },
  b: 2,
  c: null,
  d: null
}

Which has 2 differences from expected results:

  1. References are replaces with null
  2. $id is removed

unfortunately VerifyJson doesnt fully de-reference the objects. it is more designed to "pretty print and respect scrubbing or properties".

If you update to 23.6.0 the output will be:

{
  a: {
    $id: x,
    b: 1
  },
  b: 2,
  c: {
    $ref: #x/b
  },
  d: {
    $ref: #/b
  }
}

is that sufficient?

Yes, that works!

I've seen that you replaced some .ToObject<Dictionary<string, object>> with your own foreach loop. I was just about to post a workaround I found today - this issue can be fixed by calling VerifierSettings.AddExtraSettings(s => s.MetadataPropertyHandling = MetadataPropertyHandling.Ignore);. It makes Argon ignore those $ properties and treat them as all other properties. So it gives exactly the same behavior, just a different solution.

Maybe you would prefer setting this in your Argon settings instead of writing own foreach loop if you consider it a cleaner solution. Anyway, I'm closing this issue as it's solved and it's just a matter of your code preference