Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serialization settings not passed on to JToken converters #691

Closed
hennys opened this issue Oct 31, 2022 · 2 comments
Closed

Serialization settings not passed on to JToken converters #691

hennys opened this issue Oct 31, 2022 · 2 comments
Labels
Milestone

Comments

@hennys
Copy link

hennys commented Oct 31, 2022

Using Verify: v3.0.0 on .NET 6.0 in VS 2022 (17.3.6)

While using Verify to verify JSON output from OpenAPI specification generation, I realized that $ref properties in the document were replaced with null by Verify.

Simplified, this JSON:

{
   "schema": {
       "$ref": "#/components/schemas/MyComponent"
   }
}

Became serialized by Verify as:

{
   "schema": null
}

I tracked this down to how the JToken is converted to a dictionary and how the serializer treats 'metadata properties'. I could reproduce the behavior in Argon and also show how you could control this behavior by passing in a serializer with MetadataPropertyHandling set to Ignore.

var json = JToken.Parse("{'$ref': '#/no/ref'}");

var withDefaultSerializer = json.ToObject<OrderedDictionary>();
Assert.Null(withDefaultSerializer);

var serializer = JsonSerializer.Create(new JsonSerializerSettings { MetadataPropertyHandling = MetadataPropertyHandling.Ignore });
var withCustomSerializer = json.ToObject<OrderedDictionary>(serializer);

Assert.NotNull(withCustomSerializer);

I tried to apply this to the serializer settings using .AddExtraSettings(s => s.MetadataPropertyHandling = Argon.MetadataPropertyHandling.Ignore), but since those settings are not passed into the ToObject method in the JObjectConverter they do not apply. I believe the same is true for JArrayConverter.

I had some issues getting the solution running properly in it's default configuration, so apologies for lack of PR. I can make a further attempt if you would prefer this.

Using xUnit:

[Fact]
public async Task VerifyJsonShouldRespectSerializerSettings()
{
    var json = "{'$ref': '#/no/ref'}";

    await Verify(json)
        .AddExtraSettings(s => s.MetadataPropertyHandling = Argon.MetadataPropertyHandling.Ignore);
}
@SimonCropp SimonCropp added this to the 18.2.1 milestone Nov 1, 2022
@SimonCropp SimonCropp added the Bug label Nov 1, 2022
@SimonCropp
Copy link
Member

can u try 18.2.1

@hennys
Copy link
Author

hennys commented Nov 1, 2022

Works perfectly. Many thanks! Much appreciated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

2 participants