Skip to content

Commit

Permalink
Correctly handle JArray in generator, closes RicoSuter/NSwag#755
Browse files Browse the repository at this point in the history
  • Loading branch information
RicoSuter committed Jun 14, 2018
1 parent 55ac39e commit 3bacb48
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/NJsonSchema.Tests/Generation/ArrayGenerationTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using NJsonSchema.Generation;
using Xunit;

namespace NJsonSchema.Tests.Generation
{
public class ArrayGenerationTests
{
public class ClassWithJArray
{
public string Foo { get; set; }

public JArray Array { get; set; }
}

[Fact]
public async Task When_property_is_JArray_then_schema_with_any_array_is_generated()
{
//// Act
var schema = await JsonSchema4.FromTypeAsync<ClassWithJArray>(new JsonSchemaGeneratorSettings { SchemaType = SchemaType.OpenApi3 });
var json = schema.ToJson();

//// Assert
Assert.Equal(2, schema.ActualProperties.Count);
var arrayProperty = schema.ActualProperties["Array"].ActualTypeSchema;
Assert.Equal(JsonObjectType.Array, arrayProperty.Type);
Assert.True(arrayProperty.Item.ActualTypeSchema.IsAnyType);
}
}
}
3 changes: 3 additions & 0 deletions src/NJsonSchema/Generation/DefaultReflectionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ public virtual JsonTypeDescription GetDescription(Type type, IEnumerable<Attribu
if (type == typeof(byte[]))
return JsonTypeDescription.Create(type, JsonObjectType.String, isNullable, JsonFormatStrings.Byte);

if (type == typeof(JArray))
return JsonTypeDescription.Create(type, JsonObjectType.Array, isNullable, null);

if (type == typeof(JObject) ||
type == typeof(JToken) ||
type.FullName == "System.Dynamic.ExpandoObject" ||
Expand Down

0 comments on commit 3bacb48

Please sign in to comment.