Skip to content

Commit

Permalink
generate seperate type definitions for empty objects from sample data (
Browse files Browse the repository at this point in the history
  • Loading branch information
schulz3000 committed Oct 30, 2023
1 parent 85fdd00 commit a1f49d7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/NJsonSchema.Tests/Schema/JsonSchemaTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using Xunit;
Expand Down Expand Up @@ -425,5 +426,28 @@ public async Task When_azure_schema_is_loaded_then_no_exception()
Assert.NotNull(schema);
Assert.Contains("The identity type.", json);
}

[Fact]
public void When_there_are_multiple_empty_objects_in_json_sample_data_generate_different_type_definitions_foreach()
{
//https://github.com/RicoSuter/NJsonSchema/issues/1415

var schema = JsonSchema.FromSampleJson(@"
[
{
""Data"": {
""EmptyObj"": {},
""ArrayObj"": {
""AnotherEmptyObj"": {}
}
}
}
]
");

var json = schema.ToJson();

Assert.Equal(3, Regex.Matches(json, "AnotherEmptyObj").Count);
}
}
}
1 change: 1 addition & 0 deletions src/NJsonSchema/SampleJsonSchemaGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ private void Generate(JToken token, JsonSchema schema, JsonSchema rootSchema, st
.Select(t => t.Value)
.FirstOrDefault(s =>
s.Type == JsonObjectType.Object &&
properties.Any() &&
properties.All(p => s.Properties.ContainsKey(p.Name)));
}

Expand Down

0 comments on commit a1f49d7

Please sign in to comment.