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

Temporarily revert NJsonSchema v11 changes #2302

Merged
merged 3 commits into from
Aug 30, 2024
Merged
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
2 changes: 1 addition & 1 deletion examples/JsonWithReferences/JsonWithReferences.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<ProjectTypeGuids>{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<AssemblyName>JsonWithReferences</AssemblyName>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
<LangVersion>7.1</LangVersion>
</PropertyGroup>

Expand Down
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 NewtonsoftJsonSchemaGeneratorSettings
var jsonSchemaGeneratorSettings = new JsonSchemaGeneratorSettings
{
SerializerSettings = new JsonSerializerSettings
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,14 @@
<Title>Confluent.SchemaRegistry.Serdes.Json</Title>
<AssemblyName>Confluent.SchemaRegistry.Serdes.Json</AssemblyName>
<VersionPrefix>2.5.2</VersionPrefix>
<TargetFrameworks>netstandard2.0;net6.0;net8.0</TargetFrameworks>
<TargetFrameworks>netstandard2.0;net6.0</TargetFrameworks>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>Confluent.SchemaRegistry.Serdes.Json.snk</AssemblyOriginatorKeyFile>
</PropertyGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net8.0' ">
<PackageReference Include="NJsonSchema.NewtonsoftJson" Version="11.0.2" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' != 'net8.0' ">
<ItemGroup>
<PackageReference Include="NJsonSchema" Version="10.9.0" />
</ItemGroup>

Expand Down
37 changes: 2 additions & 35 deletions src/Confluent.SchemaRegistry.Serdes.Json/JsonDeserializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,7 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NJsonSchema;
#if NET8_0_OR_GREATER
using NJsonSchema.NewtonsoftJson.Generation;
#else
using NJsonSchema.Generation;
#endif
using NJsonSchema.Validation;


Expand Down Expand Up @@ -57,11 +53,7 @@ namespace Confluent.SchemaRegistry.Serdes
/// </remarks>
public class JsonDeserializer<T> : AsyncDeserializer<T, JsonSchema> where T : class
{
#if NET8_0_OR_GREATER
private readonly NewtonsoftJsonSchemaGeneratorSettings jsonSchemaGeneratorSettings;
#else
private readonly JsonSchemaGeneratorSettings jsonSchemaGeneratorSettings;
#endif

private JsonSchemaValidator validator = new JsonSchemaValidator();

Expand All @@ -77,30 +69,18 @@ public class JsonDeserializer<T> : AsyncDeserializer<T, JsonSchema> where T : cl
/// <param name="jsonSchemaGeneratorSettings">
/// JSON schema generator settings.
/// </param>
#if NET8_0_OR_GREATER
public JsonDeserializer(IEnumerable<KeyValuePair<string, string>> config = null, NewtonsoftJsonSchemaGeneratorSettings jsonSchemaGeneratorSettings = null) :
#else
public JsonDeserializer(IEnumerable<KeyValuePair<string, string>> config = null, JsonSchemaGeneratorSettings jsonSchemaGeneratorSettings = null) :
#endif
this(null, config, jsonSchemaGeneratorSettings)
{
}

#if NET8_0_OR_GREATER
public JsonDeserializer(ISchemaRegistryClient schemaRegistryClient, IEnumerable<KeyValuePair<string, string>> config = null, NewtonsoftJsonSchemaGeneratorSettings jsonSchemaGeneratorSettings = null)
#else
public JsonDeserializer(ISchemaRegistryClient schemaRegistryClient, IEnumerable<KeyValuePair<string, string>> config = null, JsonSchemaGeneratorSettings jsonSchemaGeneratorSettings = null)
#endif
public JsonDeserializer(ISchemaRegistryClient schemaRegistryClient, IEnumerable<KeyValuePair<string, string>> config = null, JsonSchemaGeneratorSettings jsonSchemaGeneratorSettings = null)
: this(schemaRegistryClient, config != null ? new JsonDeserializerConfig(config) : null, jsonSchemaGeneratorSettings)
{
}

public JsonDeserializer(ISchemaRegistryClient schemaRegistryClient, JsonDeserializerConfig config,
#if NET8_0_OR_GREATER
NewtonsoftJsonSchemaGeneratorSettings jsonSchemaGeneratorSettings = null, IList<IRuleExecutor> ruleExecutors = null)
#else
JsonSchemaGeneratorSettings jsonSchemaGeneratorSettings = null, IList<IRuleExecutor> ruleExecutors = null)
#endif
: base(schemaRegistryClient, config, ruleExecutors)
{
this.jsonSchemaGeneratorSettings = jsonSchemaGeneratorSettings;
Expand Down Expand Up @@ -141,12 +121,7 @@ public JsonDeserializer(ISchemaRegistryClient schemaRegistryClient, JsonDeserial
/// JSON schema generator settings.
/// </param>
public JsonDeserializer(ISchemaRegistryClient schemaRegistryClient, Schema schema, IEnumerable<KeyValuePair<string, string>> config = null,
#if NET8_0_OR_GREATER
NewtonsoftJsonSchemaGeneratorSettings jsonSchemaGeneratorSettings = null)
#else
JsonSchemaGeneratorSettings jsonSchemaGeneratorSettings = null)
#endif
: this(schemaRegistryClient, config, jsonSchemaGeneratorSettings)
JsonSchemaGeneratorSettings jsonSchemaGeneratorSettings = null) : this(schemaRegistryClient, config, jsonSchemaGeneratorSettings)
{
JsonSchemaResolver utils = new JsonSchemaResolver(
schemaRegistryClient, schema, this.jsonSchemaGeneratorSettings);
Expand Down Expand Up @@ -231,11 +206,7 @@ public override async Task<T> DeserializeAsync(ReadOnlyMemory<byte> data, bool i
using (var jsonStream = new MemoryStream(array, headerSize, array.Length - headerSize))
using (var jsonReader = new StreamReader(jsonStream, Encoding.UTF8))
{
#if NET8_0_OR_GREATER
JToken json = Newtonsoft.Json.JsonConvert.DeserializeObject<JToken>(jsonReader.ReadToEnd(), this.jsonSchemaGeneratorSettings?.SerializerSettings);
#else
JToken json = Newtonsoft.Json.JsonConvert.DeserializeObject<JToken>(jsonReader.ReadToEnd(), this.jsonSchemaGeneratorSettings?.ActualSerializerSettings);
#endif
json = await ExecuteMigrations(migrations, isKey, subject, topic, context.Headers, json)
.ContinueWith(t => (JToken)t.Result)
.ConfigureAwait(continueOnCapturedContext: false);
Expand Down Expand Up @@ -272,11 +243,7 @@ public override async Task<T> DeserializeAsync(ReadOnlyMemory<byte> data, bool i
}
}

#if NET8_0_OR_GREATER
value = Newtonsoft.Json.JsonConvert.DeserializeObject<T>(serializedString, this.jsonSchemaGeneratorSettings?.SerializerSettings);
#else
value = Newtonsoft.Json.JsonConvert.DeserializeObject<T>(serializedString, this.jsonSchemaGeneratorSettings?.ActualSerializerSettings);
#endif
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@
using NJsonSchema;
using NJsonSchema.Generation;
using Newtonsoft.Json.Linq;
#if NET8_0_OR_GREATER
using NJsonSchema.NewtonsoftJson.Generation;
#endif


namespace Confluent.SchemaRegistry.Serdes
Expand Down Expand Up @@ -115,11 +112,7 @@ private async Task<JsonSchema> GetSchemaUtil(Schema root)
{
NJsonSchema.Generation.JsonSchemaResolver schemaResolver =
new NJsonSchema.Generation.JsonSchemaResolver(rootObject, this.jsonSchemaGeneratorSettings ??
#if NET8_0_OR_GREATER
new NewtonsoftJsonSchemaGeneratorSettings());
#else
new JsonSchemaGeneratorSettings());
#endif

JsonReferenceResolver referenceResolver =
new JsonReferenceResolver(schemaResolver);
Expand Down
33 changes: 4 additions & 29 deletions src/Confluent.SchemaRegistry.Serdes.Json/JsonSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,9 @@
using System.Net;
using System.Threading.Tasks;
using NJsonSchema;
using NJsonSchema.Generation;
using NJsonSchema.Validation;
using Confluent.Kafka;
#if NET8_0_OR_GREATER
using NJsonSchema.NewtonsoftJson.Generation;
#else
using NJsonSchema.Generation;
#endif


namespace Confluent.SchemaRegistry.Serdes
Expand Down Expand Up @@ -58,11 +54,7 @@ namespace Confluent.SchemaRegistry.Serdes
/// </remarks>
public class JsonSerializer<T> : AsyncSerializer<T, JsonSchema> where T : class
{
#if NET8_0_OR_GREATER
private readonly NewtonsoftJsonSchemaGeneratorSettings jsonSchemaGeneratorSettings;
#else
private readonly JsonSchemaGeneratorSettings jsonSchemaGeneratorSettings;
#endif
private readonly List<SchemaReference> ReferenceList = new List<SchemaReference>();

private JsonSchemaValidator validator = new JsonSchemaValidator();
Expand Down Expand Up @@ -90,23 +82,14 @@ public class JsonSerializer<T> : AsyncSerializer<T, JsonSchema> where T : class
/// JSON schema generator settings.
/// </param>
public JsonSerializer(ISchemaRegistryClient schemaRegistryClient, JsonSerializerConfig config = null,
#if NET8_0_OR_GREATER
NewtonsoftJsonSchemaGeneratorSettings jsonSchemaGeneratorSettings = null, IList<IRuleExecutor> ruleExecutors = null)
#else
JsonSchemaGeneratorSettings jsonSchemaGeneratorSettings = null, IList<IRuleExecutor> ruleExecutors = null)
#endif
: base(schemaRegistryClient, config, ruleExecutors)
{
this.jsonSchemaGeneratorSettings = jsonSchemaGeneratorSettings;

this.schema = this.jsonSchemaGeneratorSettings == null
#if NET8_0_OR_GREATER
? NewtonsoftJsonSchemaGenerator.FromType<T>()
: NewtonsoftJsonSchemaGenerator.FromType<T>(this.jsonSchemaGeneratorSettings);
#else
? JsonSchema.FromType<T>()
: JsonSchema.FromType<T>(this.jsonSchemaGeneratorSettings);
#endif
this.schemaText = schema.ToJson();
this.schemaFullname = schema.Title;

Expand Down Expand Up @@ -153,11 +136,7 @@ public JsonSerializer(ISchemaRegistryClient schemaRegistryClient, JsonSerializer
/// JSON schema generator settings.
/// </param>
public JsonSerializer(ISchemaRegistryClient schemaRegistryClient, Schema schema, JsonSerializerConfig config = null,
#if NET8_0_OR_GREATER
NewtonsoftJsonSchemaGeneratorSettings jsonSchemaGeneratorSettings = null, IList<IRuleExecutor> ruleExecutors = null)
#else
JsonSchemaGeneratorSettings jsonSchemaGeneratorSettings = null, IList<IRuleExecutor> ruleExecutors = null)
#endif
JsonSchemaGeneratorSettings jsonSchemaGeneratorSettings = null, IList<IRuleExecutor> ruleExecutors = null)
: this(schemaRegistryClient, config, jsonSchemaGeneratorSettings, ruleExecutors)
{
foreach (var reference in schema.References)
Expand All @@ -175,7 +154,7 @@ public JsonSerializer(ISchemaRegistryClient schemaRegistryClient, Schema schema,

/// <summary>
/// Serialize an instance of type <typeparamref name="T"/> to a UTF8 encoded JSON
/// representation. The serialized data is preceded by:
/// represenation. The serialized data is preceeded 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 @@ -190,7 +169,7 @@ public JsonSerializer(ISchemaRegistryClient schemaRegistryClient, Schema schema,
/// Context relevant to the serialize operation.
/// </param>
/// <returns>
/// A <see cref="System.Threading.Tasks.Task" /> that completes with
/// A <see cref="System.Threading.Tasks.Task" /> that completes with
/// <paramref name="value" /> serialized as a byte array.
/// </returns>
public override async Task<byte[]> SerializeAsync(T value, SerializationContext context)
Expand Down Expand Up @@ -254,11 +233,7 @@ public override async Task<byte[]> SerializeAsync(T value, SerializationContext
.ConfigureAwait(continueOnCapturedContext: false);
}

#if NET8_0_OR_GREATER
var serializedString = Newtonsoft.Json.JsonConvert.SerializeObject(value, this.jsonSchemaGeneratorSettings?.SerializerSettings);
#else
var serializedString = Newtonsoft.Json.JsonConvert.SerializeObject(value, this.jsonSchemaGeneratorSettings?.ActualSerializerSettings);
#endif
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 @@ -4,7 +4,7 @@
<ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<TestProjectType>UnitTest</TestProjectType>
<AssemblyName>Confluent.SchemaRegistry.Serdes.IntegrationTests</AssemblyName>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
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.NewtonsoftJson.Generation;
using NJsonSchema.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 NewtonsoftJsonSchemaGeneratorSettings
var jsonSchemaGeneratorSettings = new JsonSchemaGeneratorSettings
{
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.SerializerSettings);
jsonSchemaGeneratorSettings.ActualSerializerSettings);
var jsonObject = JObject.Parse(serializedString);

using (var producer =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<TestProjectType>UnitTest</TestProjectType>
<AssemblyName>Confluent.SchemaRegistry.Serdes.UnitTests</AssemblyName>
<TargetFrameworks>net6.0;net8.0</TargetFrameworks>
<TargetFramework>net6.0</TargetFramework>
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>..\..\src\Confluent.SchemaRegistry.Serdes.Protobuf\Confluent.SchemaRegistry.Serdes.Protobuf.snk</AssemblyOriginatorKeyFile>
Expand Down
Loading