From 59c50586d3f1ac4e72c0b37125c1c14d309b08fe Mon Sep 17 00:00:00 2001 From: Xaver Schulz Date: Wed, 4 Oct 2023 18:28:55 +0200 Subject: [PATCH] fix #1593 --- src/NJsonSchema.Tests/Schema/JsonSchemaTests.cs | 16 ++++++++++++++++ src/NJsonSchema/SampleJsonSchemaGenerator.cs | 5 ++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/NJsonSchema.Tests/Schema/JsonSchemaTests.cs b/src/NJsonSchema.Tests/Schema/JsonSchemaTests.cs index 30b838394..20ec550dd 100644 --- a/src/NJsonSchema.Tests/Schema/JsonSchemaTests.cs +++ b/src/NJsonSchema.Tests/Schema/JsonSchemaTests.cs @@ -2,6 +2,7 @@ using System.Linq; using System.Threading.Tasks; using Newtonsoft.Json.Linq; +using Newtonsoft.Json.Schema; using Xunit; namespace NJsonSchema.Tests.Schema @@ -425,5 +426,20 @@ public async Task When_azure_schema_is_loaded_then_no_exception() Assert.NotNull(schema); Assert.Contains("The identity type.", json); } + + [Fact] + public void When_sample_json_has_mixed_type_array_anyOf_should_be_used_for_schema_items() + { + //https://github.com/RicoSuter/NJsonSchema/issues/1593 + + //// Arrange + var schema = JsonSchema.FromSampleJson("[1, { \"a\": \"b\"}, \"value\"]"); + + //// Act + var json = schema.ToJson(); + + //// Assert + Assert.Contains("anyOf", json); + } } } diff --git a/src/NJsonSchema/SampleJsonSchemaGenerator.cs b/src/NJsonSchema/SampleJsonSchemaGenerator.cs index 534ccfe03..9fb32a03c 100644 --- a/src/NJsonSchema/SampleJsonSchemaGenerator.cs +++ b/src/NJsonSchema/SampleJsonSchemaGenerator.cs @@ -196,7 +196,10 @@ private void GenerateArray(JToken token, JsonSchema schema, JsonSchema rootSchem } else { - schema.Item = itemSchemas.First(); + schema.Item = new JsonSchema + { + AnyOf = itemSchemas + }; } }