Skip to content

Commit

Permalink
Fix async enumerable handling, closes RicoSuter/NSwag#3332
Browse files Browse the repository at this point in the history
  • Loading branch information
RicoSuter committed Feb 26, 2021
1 parent ad366fd commit f98ac01
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
33 changes: 33 additions & 0 deletions src/NJsonSchema.Tests/Generation/ArrayGenerationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,38 @@ public async Task When_class_inherits_from_list_then_schema_is_inlined_and_type_
Assert.Equal(JsonObjectType.Array, schema.Definitions["SomeModelCollectionResponse"].Type);
Assert.NotNull(schema.Definitions["SomeModelCollectionResponse"].Item);
}

#if NET5_0

public class ClassWithAsyncEnumerable
{
public IAsyncEnumerable<Apple> AsyncApples { get; set; }

public List<Apple> AppleList { get; set; }
}

public class Apple
{
public string Name { get; set; }
}

[Fact]
public async Task When_property_is_async_numerable_then_item_type_is_correct()
{
//// Act
var schema = JsonSchema.FromType<ClassWithAsyncEnumerable>(new JsonSchemaGeneratorSettings { SchemaType = SchemaType.OpenApi3 });
var json = schema.ToJson();

//// Assert
var asyncProperty = schema.ActualProperties["AsyncApples"].ActualTypeSchema;
Assert.Equal(JsonObjectType.Array, asyncProperty.Type);
Assert.True(asyncProperty.Item.HasReference);

var listProperty = schema.ActualProperties["AppleList"].ActualTypeSchema;
Assert.Equal(JsonObjectType.Array, listProperty.Type);
Assert.True(listProperty.Item.HasReference);
}

#endif
}
}
5 changes: 4 additions & 1 deletion src/NJsonSchema/Generation/JsonSchemaGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,10 @@ protected virtual void GenerateArray<TSchemaType>(
typeDescription.ApplyType(schema);

var jsonSchemaAttribute = contextualType.GetTypeAttribute<JsonSchemaAttribute>();
var itemType = jsonSchemaAttribute?.ArrayItem.ToContextualType() ?? contextualType.EnumerableItemType;
var itemType = jsonSchemaAttribute?.ArrayItem.ToContextualType() ??
contextualType.EnumerableItemType ??
contextualType.GenericArguments.FirstOrDefault();

if (itemType != null)
{
var itemIsNullable = contextualType.GetContextAttribute<ItemsCanBeNullAttribute>() != null ||
Expand Down

0 comments on commit f98ac01

Please sign in to comment.