Skip to content

Commit 3bacb48

Browse files
committed
Correctly handle JArray in generator, closes RicoSuter/NSwag#755
1 parent 55ac39e commit 3bacb48

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System.Threading.Tasks;
2+
using Newtonsoft.Json.Linq;
3+
using NJsonSchema.Generation;
4+
using Xunit;
5+
6+
namespace NJsonSchema.Tests.Generation
7+
{
8+
public class ArrayGenerationTests
9+
{
10+
public class ClassWithJArray
11+
{
12+
public string Foo { get; set; }
13+
14+
public JArray Array { get; set; }
15+
}
16+
17+
[Fact]
18+
public async Task When_property_is_JArray_then_schema_with_any_array_is_generated()
19+
{
20+
//// Act
21+
var schema = await JsonSchema4.FromTypeAsync<ClassWithJArray>(new JsonSchemaGeneratorSettings { SchemaType = SchemaType.OpenApi3 });
22+
var json = schema.ToJson();
23+
24+
//// Assert
25+
Assert.Equal(2, schema.ActualProperties.Count);
26+
var arrayProperty = schema.ActualProperties["Array"].ActualTypeSchema;
27+
Assert.Equal(JsonObjectType.Array, arrayProperty.Type);
28+
Assert.True(arrayProperty.Item.ActualTypeSchema.IsAnyType);
29+
}
30+
}
31+
}

src/NJsonSchema/Generation/DefaultReflectionService.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,9 @@ public virtual JsonTypeDescription GetDescription(Type type, IEnumerable<Attribu
105105
if (type == typeof(byte[]))
106106
return JsonTypeDescription.Create(type, JsonObjectType.String, isNullable, JsonFormatStrings.Byte);
107107

108+
if (type == typeof(JArray))
109+
return JsonTypeDescription.Create(type, JsonObjectType.Array, isNullable, null);
110+
108111
if (type == typeof(JObject) ||
109112
type == typeof(JToken) ||
110113
type.FullName == "System.Dynamic.ExpandoObject" ||

0 commit comments

Comments
 (0)