Skip to content

Commit

Permalink
Json schema validation (#606)
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-almeida-konkconsulting authored Apr 23, 2024
1 parent 6afcb27 commit be5beed
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="8.0.3"/>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="8.0.3" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="Newtonsoft.Json.Schema" Version="3.0.15" />
<PackageReference Include="NJsonSchema.NewtonsoftJson" Version="11.0.0" />
<PackageReference Include="ReHackt.Extensions.Options.Validation" Version="8.0.2" />
<PackageReference Include="SolidToken.SpecFlow.DependencyInjection" Version="3.9.3" />
<PackageReference Include="SpecFlow.Plus.LivingDocPlugin" Version="3.9.57" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
namespace Backbone.AdminApi.Tests.Integration.Models;

public class ChangeClientSecretResponse
{
public required string ClientId { get; set; }
public required string DisplayName { get; set; }
public required string ClientSecret { get; set; }
public required string DefaultTier { get; set; }
public required DateTime CreatedAt { get; set; }
public int? MaxIdentities { get; set; }
}
24 changes: 20 additions & 4 deletions AdminApi/test/AdminApi.Tests.Integration/Support/JsonValidators.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Schema;
using Newtonsoft.Json.Schema.Generation;
using Newtonsoft.Json.Serialization;
using NJsonSchema.NewtonsoftJson.Generation;
using JsonSchemaGenerator = NJsonSchema.Generation.JsonSchemaGenerator;

namespace Backbone.AdminApi.Tests.Integration.Support;

Expand All @@ -17,14 +19,28 @@ public static bool ValidateJsonSchema<T>(string json, out IList<string> errors)
return parsedJson.IsValid(schema, out errors);
}

var generator = new JSchemaGenerator
var settings = new NewtonsoftJsonSchemaGeneratorSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver()
SerializerSettings = new JsonSerializerSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver()
}
};

var generator = new JsonSchemaGenerator(settings);

var schemaJson = generator.Generate(typeof(T));
schema = JSchema.Parse(schemaJson.ToString());

var generatedSchema = schemaJson.ToJson();

schema = JSchema.Parse(generatedSchema);

schema.AllowAdditionalProperties = true;

CACHED_SCHEMAS.Add(typeof(T), schema);

var responseJson = JObject.Parse(json);

return responseJson.IsValid(schema, out errors);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="8.0.3"/>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="8.0.3" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
<PackageReference Include="Microsoft.AspNet.WebApi.Client" Version="6.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="Newtonsoft.Json.Schema" Version="3.0.15" />
<PackageReference Include="NJsonSchema.NewtonsoftJson" Version="11.0.0" />
<PackageReference Include="ReHackt.Extensions.Options.Validation" Version="8.0.2" />
<PackageReference Include="SolidToken.SpecFlow.DependencyInjection" Version="3.9.3" />
<PackageReference Include="SpecFlow.Plus.LivingDocPlugin" Version="3.9.57" />
Expand Down
24 changes: 20 additions & 4 deletions ConsumerApi.Tests.Integration/Support/JsonValidators.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Schema;
using Newtonsoft.Json.Schema.Generation;
using Newtonsoft.Json.Serialization;
using NJsonSchema.NewtonsoftJson.Generation;
using JsonSchemaGenerator = NJsonSchema.Generation.JsonSchemaGenerator;

namespace Backbone.ConsumerApi.Tests.Integration.Support;

Expand All @@ -17,14 +19,28 @@ public static bool ValidateJsonSchema<T>(string json, out IList<string> errors)
return parsedJson.IsValid(schema, out errors);
}

var generator = new JSchemaGenerator
var settings = new NewtonsoftJsonSchemaGeneratorSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver()
SerializerSettings = new JsonSerializerSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver()
}
};

var generator = new JsonSchemaGenerator(settings);

var schemaJson = generator.Generate(typeof(T));
schema = JSchema.Parse(schemaJson.ToString());

var generatedSchema = schemaJson.ToJson();

schema = JSchema.Parse(generatedSchema);

schema.AllowAdditionalProperties = true;

CACHED_SCHEMAS.Add(typeof(T), schema);

var responseJson = JObject.Parse(json);

return responseJson.IsValid(schema, out errors);
}
}

0 comments on commit be5beed

Please sign in to comment.