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

use anyOf for mixed type arrays from Json sample data #1632

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/NJsonSchema.Tests/Schema/JsonSchemaTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}
}
5 changes: 4 additions & 1 deletion src/NJsonSchema/SampleJsonSchemaGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,10 @@ private void GenerateArray(JToken token, JsonSchema schema, JsonSchema rootSchem
}
else
{
schema.Item = itemSchemas.First();
schema.Item = new JsonSchema
{
AnyOf = itemSchemas
};
}
}

Expand Down