Skip to content

Commit b2596d5

Browse files
tsolbjorDoHoon Kim
authored and
DoHoon Kim
committed
Feature/Azure#156 list of recursive models (Azure#233)
1 parent 503462d commit b2596d5

File tree

7 files changed

+78
-15
lines changed

7 files changed

+78
-15
lines changed

samples/Microsoft.Azure.WebJobs.Extensions.OpenApi.FunctionApp.Models/DummyResponseModel.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,9 @@ public class DummyResponseModel
5454
public DummySubResponseModel SubResponse2 { get; set; }
5555

5656
public DummyGenericModel<DummyModel> DummyGenericDummyModel { get; set; }
57+
58+
public List<DummyResponseModel> Children { get; set; }
59+
60+
public Dictionary<string, DummyResponseModel> Children2 { get; set; }
5761
}
5862
}

src/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core/Visitors/DictionaryObjectTypeVisitor.cs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ namespace Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Visitors
1616
/// </summary>
1717
public class DictionaryObjectTypeVisitor : TypeVisitor
1818
{
19+
private readonly Dictionary<Type, OpenApiSchemaAcceptor> visitedTypes = new Dictionary<Type, OpenApiSchemaAcceptor>();
20+
1921
/// <inheritdoc />
2022
public DictionaryObjectTypeVisitor(VisitorCollection visitorCollection)
2123
: base(visitorCollection)
@@ -54,14 +56,20 @@ public override void Visit(IAcceptor acceptor, KeyValuePair<string, Type> type,
5456
};
5557
var schemas = new Dictionary<string, OpenApiSchema>();
5658

57-
var subAcceptor = new OpenApiSchemaAcceptor()
59+
OpenApiSchemaAcceptor subAcceptor;
60+
if (!this.visitedTypes.ContainsKey(underlyingType))
5861
{
59-
Types = types,
60-
RootSchemas = instance.RootSchemas,
61-
Schemas = schemas,
62-
};
63-
64-
subAcceptor.Accept(this.VisitorCollection, namingStrategy);
62+
subAcceptor = new OpenApiSchemaAcceptor()
63+
{
64+
Types = types, RootSchemas = instance.RootSchemas, Schemas = schemas,
65+
};
66+
this.visitedTypes.Add(underlyingType, subAcceptor);
67+
subAcceptor.Accept(this.VisitorCollection, namingStrategy);
68+
}
69+
else
70+
{
71+
subAcceptor = this.visitedTypes[underlyingType];
72+
}
6573

6674
var properties = subAcceptor.Schemas.First().Value;
6775

src/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core/Visitors/ListObjectTypeVisitor.cs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ namespace Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Visitors
1515
/// </summary>
1616
public class ListObjectTypeVisitor : TypeVisitor
1717
{
18+
private readonly Dictionary<Type, OpenApiSchemaAcceptor> visitedTypes = new Dictionary<Type, OpenApiSchemaAcceptor>();
19+
1820
/// <inheritdoc />
1921
public ListObjectTypeVisitor(VisitorCollection visitorCollection)
2022
: base(visitorCollection)
@@ -53,14 +55,21 @@ public override void Visit(IAcceptor acceptor, KeyValuePair<string, Type> type,
5355
};
5456
var schemas = new Dictionary<string, OpenApiSchema>();
5557

56-
var subAcceptor = new OpenApiSchemaAcceptor()
58+
OpenApiSchemaAcceptor subAcceptor;
59+
if (!this.visitedTypes.ContainsKey(underlyingType))
5760
{
58-
Types = types,
59-
RootSchemas = instance.RootSchemas,
60-
Schemas = schemas,
61-
};
61+
subAcceptor = new OpenApiSchemaAcceptor()
62+
{
63+
Types = types, RootSchemas = instance.RootSchemas, Schemas = schemas,
64+
};
65+
this.visitedTypes.Add(underlyingType, subAcceptor);
66+
subAcceptor.Accept(this.VisitorCollection, namingStrategy);
6267

63-
subAcceptor.Accept(this.VisitorCollection, namingStrategy);
68+
}
69+
else
70+
{
71+
subAcceptor = this.visitedTypes[underlyingType];
72+
}
6473

6574
var items = subAcceptor.Schemas.First().Value;
6675

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System.Net;
2+
using System.Threading.Tasks;
3+
using Microsoft.AspNetCore.Http;
4+
using Microsoft.AspNetCore.Mvc;
5+
using Microsoft.Azure.WebJobs.Extensions.Http;
6+
using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Attributes;
7+
using Microsoft.Azure.WebJobs.Extensions.OpenApi.TestApp.Models;
8+
using Microsoft.Extensions.Logging;
9+
using Microsoft.OpenApi.Models;
10+
11+
namespace Microsoft.Azure.WebJobs.Extensions.OpenApi.TestApp
12+
{
13+
public static class Get_ApplicationJson_GenericAndRecursive_HttpTrigger
14+
{
15+
[FunctionName(nameof(Get_ApplicationJson_GenericAndRecursive_HttpTrigger))]
16+
[OpenApiOperation(operationId: nameof(Get_ApplicationJson_GenericAndRecursive), tags: new[] { "genericAndRecursive" })]
17+
[OpenApiResponseWithBody(statusCode: HttpStatusCode.OK, contentType: "application/json", bodyType: typeof(GenericAndRecursiveModel), Description = "The OK response")]
18+
public static async Task<IActionResult> Get_ApplicationJson_GenericAndRecursive(
19+
[HttpTrigger(AuthorizationLevel.Anonymous, "GET", Route = "get-applicationjson-genericandrecursive")] HttpRequest req,
20+
ILogger log)
21+
{
22+
var result = new OkResult();
23+
24+
return await Task.FromResult(result).ConfigureAwait(false);
25+
}
26+
}
27+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System.Collections.Generic;
2+
3+
namespace Microsoft.Azure.WebJobs.Extensions.OpenApi.TestApp.Models
4+
{
5+
public class GenericAndRecursiveModel
6+
{
7+
public List<GenericAndRecursiveModel> Children { get; set; }
8+
9+
public Dictionary<string, GenericAndRecursiveModel> Children2 { get; set; }
10+
11+
}
12+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
using System.Collections.Generic;
2+
13
namespace Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Tests.Fakes
24
{
35
public class FakeClassModel
46
{
57
public int Number { get; set; }
68
public string Text { get; set; }
9+
public List<FakeClassModel> Children { get; set; }
710
}
811
}

test/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Tests/DocumentHelperTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,15 @@ public void GetOpenApiSchemas_Result_Should_Contain_Schema_For_Function_Classes(
4545

4646
schemas.Should().ContainKey("FakeClassModel");
4747

48-
schemas["FakeClassModel"].Properties.Count.Should().Be(2);
48+
schemas["FakeClassModel"].Properties.Count.Should().Be(3);
4949
schemas["FakeClassModel"].Type.Should().Be("object");
5050

5151
schemas.Should().ContainKey("FakeOtherClassModel");
5252

5353
schemas["FakeOtherClassModel"].Properties.Count.Should().Be(2);
5454
schemas["FakeOtherClassModel"].Type.Should().Be("object");
5555

56-
schemas["FakeClassModel"].Properties.Count.Should().Be(2);
56+
schemas["FakeClassModel"].Properties.Count.Should().Be(3);
5757
schemas["FakeClassModel"].Type.Should().Be("object");
5858

5959
schemas.Should().ContainKey("FakeListModel");

0 commit comments

Comments
 (0)