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

Update NJsonSchema to v11 #2185

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,6 @@ UpgradeLog*.htm
*~
\#*
core

# JetBrains Rider
.idea/
4 changes: 2 additions & 2 deletions examples/JsonWithReferences/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using NJsonSchema.Generation;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using NJsonSchema.NewtonsoftJson.Generation;


/// <summary>
Expand Down Expand Up @@ -150,7 +150,7 @@ static async Task Main(string[] args)
// from default one to camelCase.
// It's also possible to add JsonProperty attributes to customize
// serialization mapping and all available NJson attributes.
var jsonSchemaGeneratorSettings = new JsonSchemaGeneratorSettings
var jsonSchemaGeneratorSettings = new NewtonsoftJsonSchemaGeneratorSettings
{
SerializerSettings = new JsonSerializerSettings
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="NJsonSchema" Version="10.6.3" />
<PackageReference Include="NJsonSchema.NewtonsoftJson" Version="11.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
10 changes: 5 additions & 5 deletions src/Confluent.SchemaRegistry.Serdes.Json/JsonDeserializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
using System.Threading.Tasks;
using Confluent.Kafka;
using NJsonSchema;
using NJsonSchema.Generation;
using NJsonSchema.NewtonsoftJson.Generation;
using NJsonSchema.Validation;


Expand Down Expand Up @@ -52,7 +52,7 @@ public class JsonDeserializer<T> : IAsyncDeserializer<T> where T : class
{
private readonly int headerSize = sizeof(int) + sizeof(byte);

private readonly JsonSchemaGeneratorSettings jsonSchemaGeneratorSettings;
private readonly NewtonsoftJsonSchemaGeneratorSettings jsonSchemaGeneratorSettings;
private JsonSchema schema = null;
private ISchemaRegistryClient schemaRegistryClient;

Expand All @@ -78,7 +78,7 @@ public class JsonDeserializer<T> : IAsyncDeserializer<T> where T : class
/// JSON schema generator settings.
/// </param>
public JsonDeserializer(ISchemaRegistryClient schemaRegistryClient, Schema schema, IEnumerable<KeyValuePair<string, string>> config = null,
JsonSchemaGeneratorSettings jsonSchemaGeneratorSettings = null)
NewtonsoftJsonSchemaGeneratorSettings jsonSchemaGeneratorSettings = null)
{
this.schemaRegistryClient = schemaRegistryClient;
this.jsonSchemaGeneratorSettings = jsonSchemaGeneratorSettings;
Expand Down Expand Up @@ -106,7 +106,7 @@ public JsonDeserializer(ISchemaRegistryClient schemaRegistryClient, Schema schem
/// <param name="jsonSchemaGeneratorSettings">
/// JSON schema generator settings.
/// </param>
public JsonDeserializer(IEnumerable<KeyValuePair<string, string>> config = null, JsonSchemaGeneratorSettings jsonSchemaGeneratorSettings = null)
public JsonDeserializer(IEnumerable<KeyValuePair<string, string>> config = null, NewtonsoftJsonSchemaGeneratorSettings jsonSchemaGeneratorSettings = null)
{
this.jsonSchemaGeneratorSettings = jsonSchemaGeneratorSettings;

Expand Down Expand Up @@ -169,7 +169,7 @@ public Task<T> DeserializeAsync(ReadOnlyMemory<byte> data, bool isNull, Serializ
throw new InvalidDataException("Schema validation failed for properties: [" + string.Join(", ", validationResult.Select(r => r.Path)) + "]");
}
}
return Task.FromResult(Newtonsoft.Json.JsonConvert.DeserializeObject<T>(serializedString, this.jsonSchemaGeneratorSettings?.ActualSerializerSettings));
return Task.FromResult(Newtonsoft.Json.JsonConvert.DeserializeObject<T>(serializedString, this.jsonSchemaGeneratorSettings?.SerializerSettings));
}
}
catch (AggregateException e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
using NJsonSchema;
using NJsonSchema.Generation;
using Newtonsoft.Json.Linq;
using NJsonSchema.NewtonsoftJson.Generation;


namespace Confluent.SchemaRegistry.Serdes
Expand Down Expand Up @@ -71,7 +72,7 @@ private JsonSchema GetSchemaUtil(Schema root)
{
NJsonSchema.Generation.JsonSchemaResolver schemaResolver =
new NJsonSchema.Generation.JsonSchemaResolver(rootObject, this.jsonSchemaGeneratorSettings ??
new JsonSchemaGeneratorSettings());
new NewtonsoftJsonSchemaGeneratorSettings());

JsonReferenceResolver referenceResolver =
new JsonReferenceResolver(schemaResolver);
Expand Down
12 changes: 6 additions & 6 deletions src/Confluent.SchemaRegistry.Serdes.Json/JsonSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
using System.Threading;
using System.Threading.Tasks;
using NJsonSchema;
using NJsonSchema.Generation;
using NJsonSchema.Validation;
using Confluent.Kafka;
using NJsonSchema.NewtonsoftJson.Generation;


namespace Confluent.SchemaRegistry.Serdes
Expand Down Expand Up @@ -64,7 +64,7 @@ public class JsonSerializer<T> : IAsyncSerializer<T> where T : class
private int initialBufferSize = DefaultInitialBufferSize;
private SubjectNameStrategyDelegate subjectNameStrategy = null;
private ISchemaRegistryClient schemaRegistryClient;
private readonly JsonSchemaGeneratorSettings jsonSchemaGeneratorSettings;
private readonly NewtonsoftJsonSchemaGeneratorSettings jsonSchemaGeneratorSettings;
private HashSet<string> subjectsRegistered = new HashSet<string>();
private SemaphoreSlim serializeMutex = new SemaphoreSlim(1);
private readonly List<SchemaReference> ReferenceList = new List<SchemaReference>();
Expand Down Expand Up @@ -115,7 +115,7 @@ private void SetConfigUtil(JsonSerializerConfig config)
/// <param name="jsonSchemaGeneratorSettings">
/// JSON schema generator settings.
/// </param>
public JsonSerializer(ISchemaRegistryClient schemaRegistryClient, JsonSerializerConfig config = null, JsonSchemaGeneratorSettings jsonSchemaGeneratorSettings = null)
public JsonSerializer(ISchemaRegistryClient schemaRegistryClient, JsonSerializerConfig config = null, NewtonsoftJsonSchemaGeneratorSettings jsonSchemaGeneratorSettings = null)
{
this.schemaRegistryClient = schemaRegistryClient;
this.jsonSchemaGeneratorSettings = jsonSchemaGeneratorSettings;
Expand Down Expand Up @@ -148,7 +148,7 @@ public JsonSerializer(ISchemaRegistryClient schemaRegistryClient, JsonSerializer
/// <param name="jsonSchemaGeneratorSettings">
/// JSON schema generator settings.
/// </param>
public JsonSerializer(ISchemaRegistryClient schemaRegistryClient, Schema schema, JsonSerializerConfig config = null, JsonSchemaGeneratorSettings jsonSchemaGeneratorSettings = null)
public JsonSerializer(ISchemaRegistryClient schemaRegistryClient, Schema schema, JsonSerializerConfig config = null, NewtonsoftJsonSchemaGeneratorSettings jsonSchemaGeneratorSettings = null)
{
this.schemaRegistryClient = schemaRegistryClient;
this.jsonSchemaGeneratorSettings = jsonSchemaGeneratorSettings;
Expand All @@ -170,7 +170,7 @@ public JsonSerializer(ISchemaRegistryClient schemaRegistryClient, Schema schema,

/// <summary>
/// Serialize an instance of type <typeparamref name="T"/> to a UTF8 encoded JSON
/// represenation. The serialized data is preceeded by:
/// representation. The serialized data is preceded by:
/// 1. A "magic byte" (1 byte) that identifies this as a message with
/// Confluent Platform framing.
/// 2. The id of the schema as registered in Confluent's Schema Registry
Expand All @@ -192,7 +192,7 @@ public async Task<byte[]> SerializeAsync(T value, SerializationContext context)
{
if (value == null) { return null; }

var serializedString = Newtonsoft.Json.JsonConvert.SerializeObject(value, this.jsonSchemaGeneratorSettings?.ActualSerializerSettings);
var serializedString = Newtonsoft.Json.JsonConvert.SerializeObject(value, this.jsonSchemaGeneratorSettings?.SerializerSettings);
var validationResult = validator.Validate(serializedString, this.schema);
if (validationResult.Count > 0)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using Newtonsoft.Json.Linq;
using NJsonSchema.Generation;
using NJsonSchema.NewtonsoftJson.Generation;


namespace Confluent.SchemaRegistry.Serdes.IntegrationTests
Expand Down Expand Up @@ -139,7 +139,7 @@ public static void UseReferences(string bootstrapServers, string schemaRegistryS
var schemaRegistryConfig = new SchemaRegistryConfig { Url = schemaRegistryServers };
var sr = new CachedSchemaRegistryClient(schemaRegistryConfig);

var jsonSchemaGeneratorSettings = new JsonSchemaGeneratorSettings
var jsonSchemaGeneratorSettings = new NewtonsoftJsonSchemaGeneratorSettings
{
SerializerSettings = new JsonSerializerSettings
{
Expand Down Expand Up @@ -209,7 +209,7 @@ public static void UseReferences(string bootstrapServers, string schemaRegistryS

// Test producing and consuming directly a JObject
var serializedString = Newtonsoft.Json.JsonConvert.SerializeObject(order,
jsonSchemaGeneratorSettings.ActualSerializerSettings);
jsonSchemaGeneratorSettings.SerializerSettings);
var jsonObject = JObject.Parse(serializedString);

using (var producer =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@
using Confluent.Kafka;
using Moq;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
using NJsonSchema.Generation;
using NJsonSchema.NewtonsoftJson.Generation;
using System;
using System.Collections.Generic;
using System.IO;
Expand Down Expand Up @@ -205,7 +206,7 @@ public async Task WithJsonSerializerSettingsSerDe()
{
const int value = 1234;
var expectedJson = $"{{\"Value\":{value * 2}}}";
var jsonSchemaGeneratorSettings = new JsonSchemaGeneratorSettings
var jsonSchemaGeneratorSettings = new NewtonsoftJsonSchemaGeneratorSettings
{
SerializerSettings = new JsonSerializerSettings
{
Expand Down Expand Up @@ -249,7 +250,7 @@ public async Task WithJsonSchemaExternalReferencesAsync()
SubjectNameStrategy = SubjectNameStrategy.TopicRecord
};

var jsonSchemaGeneratorSettings = new JsonSchemaGeneratorSettings
var jsonSchemaGeneratorSettings = new NewtonsoftJsonSchemaGeneratorSettings
{
SerializerSettings = new JsonSerializerSettings
{
Expand Down Expand Up @@ -279,14 +280,22 @@ public async Task WithJsonSchemaExternalReferencesAsync()
}

[Theory]
[InlineData(EnumHandling.CamelCaseString, EnumType.EnumValue, "{\"Value\":\"enumValue\"}")]
[InlineData(EnumHandling.String, EnumType.None, "{\"Value\":\"None\"}")]
[InlineData(EnumHandling.Integer, EnumType.OtherValue, "{\"Value\":5678}")]
public async Task WithJsonSchemaGeneratorSettingsSerDe(EnumHandling enumHandling, EnumType value, string expectedJson)
[InlineData("CamelCaseString", EnumType.EnumValue, "{\"Value\":\"enumValue\"}")]
[InlineData("String", EnumType.None, "{\"Value\":\"None\"}")]
[InlineData("Integer", EnumType.OtherValue, "{\"Value\":5678}")]
public async Task WithJsonSchemaGeneratorSettingsSerDe(string enumHandling, EnumType value, string expectedJson)
{
var jsonSchemaGeneratorSettings = new JsonSchemaGeneratorSettings
var serializerSettings = enumHandling switch
{
DefaultEnumHandling = enumHandling
"CamelCaseString" => new JsonSerializerSettings { Converters = { new StringEnumConverter(new CamelCaseNamingStrategy()) } },
"String" => new JsonSerializerSettings { Converters = { new StringEnumConverter() } },
"Integer" => new JsonSerializerSettings(),
_ => throw new ArgumentException("Invalid enumHandling value", nameof(enumHandling)),
};

var jsonSchemaGeneratorSettings = new NewtonsoftJsonSchemaGeneratorSettings
{
SerializerSettings = serializerSettings,
};

var jsonSerializer = new JsonSerializer<EnumObject>(schemaRegistryClient, jsonSchemaGeneratorSettings: jsonSchemaGeneratorSettings);
Expand Down