Skip to content

Commit

Permalink
Feature/Azure#156 list of recursive models (Azure#233)
Browse files Browse the repository at this point in the history
  • Loading branch information
tsolbjor authored and DoHoon Kim committed Oct 5, 2021
1 parent 503462d commit b2596d5
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,9 @@ public class DummyResponseModel
public DummySubResponseModel SubResponse2 { get; set; }

public DummyGenericModel<DummyModel> DummyGenericDummyModel { get; set; }

public List<DummyResponseModel> Children { get; set; }

public Dictionary<string, DummyResponseModel> Children2 { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ namespace Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Visitors
/// </summary>
public class DictionaryObjectTypeVisitor : TypeVisitor
{
private readonly Dictionary<Type, OpenApiSchemaAcceptor> visitedTypes = new Dictionary<Type, OpenApiSchemaAcceptor>();

/// <inheritdoc />
public DictionaryObjectTypeVisitor(VisitorCollection visitorCollection)
: base(visitorCollection)
Expand Down Expand Up @@ -54,14 +56,20 @@ public override void Visit(IAcceptor acceptor, KeyValuePair<string, Type> type,
};
var schemas = new Dictionary<string, OpenApiSchema>();

var subAcceptor = new OpenApiSchemaAcceptor()
OpenApiSchemaAcceptor subAcceptor;
if (!this.visitedTypes.ContainsKey(underlyingType))
{
Types = types,
RootSchemas = instance.RootSchemas,
Schemas = schemas,
};

subAcceptor.Accept(this.VisitorCollection, namingStrategy);
subAcceptor = new OpenApiSchemaAcceptor()
{
Types = types, RootSchemas = instance.RootSchemas, Schemas = schemas,
};
this.visitedTypes.Add(underlyingType, subAcceptor);
subAcceptor.Accept(this.VisitorCollection, namingStrategy);
}
else
{
subAcceptor = this.visitedTypes[underlyingType];
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ namespace Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Visitors
/// </summary>
public class ListObjectTypeVisitor : TypeVisitor
{
private readonly Dictionary<Type, OpenApiSchemaAcceptor> visitedTypes = new Dictionary<Type, OpenApiSchemaAcceptor>();

/// <inheritdoc />
public ListObjectTypeVisitor(VisitorCollection visitorCollection)
: base(visitorCollection)
Expand Down Expand Up @@ -53,14 +55,21 @@ public override void Visit(IAcceptor acceptor, KeyValuePair<string, Type> type,
};
var schemas = new Dictionary<string, OpenApiSchema>();

var subAcceptor = new OpenApiSchemaAcceptor()
OpenApiSchemaAcceptor subAcceptor;
if (!this.visitedTypes.ContainsKey(underlyingType))
{
Types = types,
RootSchemas = instance.RootSchemas,
Schemas = schemas,
};
subAcceptor = new OpenApiSchemaAcceptor()
{
Types = types, RootSchemas = instance.RootSchemas, Schemas = schemas,
};
this.visitedTypes.Add(underlyingType, subAcceptor);
subAcceptor.Accept(this.VisitorCollection, namingStrategy);

subAcceptor.Accept(this.VisitorCollection, namingStrategy);
}
else
{
subAcceptor = this.visitedTypes[underlyingType];
}

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

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System.Net;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Attributes;
using Microsoft.Azure.WebJobs.Extensions.OpenApi.TestApp.Models;
using Microsoft.Extensions.Logging;
using Microsoft.OpenApi.Models;

namespace Microsoft.Azure.WebJobs.Extensions.OpenApi.TestApp
{
public static class Get_ApplicationJson_GenericAndRecursive_HttpTrigger
{
[FunctionName(nameof(Get_ApplicationJson_GenericAndRecursive_HttpTrigger))]
[OpenApiOperation(operationId: nameof(Get_ApplicationJson_GenericAndRecursive), tags: new[] { "genericAndRecursive" })]
[OpenApiResponseWithBody(statusCode: HttpStatusCode.OK, contentType: "application/json", bodyType: typeof(GenericAndRecursiveModel), Description = "The OK response")]
public static async Task<IActionResult> Get_ApplicationJson_GenericAndRecursive(
[HttpTrigger(AuthorizationLevel.Anonymous, "GET", Route = "get-applicationjson-genericandrecursive")] HttpRequest req,
ILogger log)
{
var result = new OkResult();

return await Task.FromResult(result).ConfigureAwait(false);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Collections.Generic;

namespace Microsoft.Azure.WebJobs.Extensions.OpenApi.TestApp.Models
{
public class GenericAndRecursiveModel
{
public List<GenericAndRecursiveModel> Children { get; set; }

public Dictionary<string, GenericAndRecursiveModel> Children2 { get; set; }

}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
using System.Collections.Generic;

namespace Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Tests.Fakes
{
public class FakeClassModel
{
public int Number { get; set; }
public string Text { get; set; }
public List<FakeClassModel> Children { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,15 @@ public void GetOpenApiSchemas_Result_Should_Contain_Schema_For_Function_Classes(

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

schemas["FakeClassModel"].Properties.Count.Should().Be(2);
schemas["FakeClassModel"].Properties.Count.Should().Be(3);
schemas["FakeClassModel"].Type.Should().Be("object");

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

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

schemas["FakeClassModel"].Properties.Count.Should().Be(2);
schemas["FakeClassModel"].Properties.Count.Should().Be(3);
schemas["FakeClassModel"].Type.Should().Be("object");

schemas.Should().ContainKey("FakeListModel");
Expand Down

0 comments on commit b2596d5

Please sign in to comment.