Skip to content

Commit 62f9cf4

Browse files
authored
Fix object schema rendering issue (Azure#300)
1 parent 540625c commit 62f9cf4

File tree

5 files changed

+65
-22
lines changed

5 files changed

+65
-22
lines changed

src/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core/DocumentHelper.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,7 @@ public Dictionary<string, OpenApiSchema> GetOpenApiSchemas(List<MethodInfo> elem
139139
.Select(p => p.IsOpenApiArray() || p.IsOpenApiDictionary() ? p.GetOpenApiSubType() : p)
140140
.Distinct()
141141
.Where(p => !p.IsSimpleType())
142-
.Where(p => p != typeof(JObject))
143-
.Where(p => p != typeof(JToken))
142+
.Where(p => p.IsReferentialType())
144143
.Where(p => !typeof(Array).IsAssignableFrom(p))
145144
.ToList();
146145

@@ -155,6 +154,7 @@ public Dictionary<string, OpenApiSchema> GetOpenApiSchemas(List<MethodInfo> elem
155154

156155
var union = schemas.Concat(rootSchemas.Where(p => !schemas.Keys.Contains(p.Key)))
157156
.Distinct()
157+
.Where(p => p.Key.ToUpperInvariant() != "OBJECT")
158158
.OrderBy(p => p.Key)
159159
.ToDictionary(p => p.Key,
160160
p =>

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ public override void Visit(IAcceptor acceptor, KeyValuePair<string, Type> type,
7272

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

75+
// Forces to remove the title value from the additionalProperties attribute.
76+
properties.Title = null;
77+
7578
// Adds the reference to the schema for the underlying type.
7679
if (this.IsReferential(underlyingType))
7780
{

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ public override void Visit(IAcceptor acceptor, KeyValuePair<string, Type> type,
7373

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

76+
// Forces to remove the title value from the items attribute.
77+
items.Title = null;
78+
7679
// Adds the reference to the schema for the underlying type.
7780
if (this.IsReferential(underlyingType))
7881
{

test-integration/Microsoft.Azure.WebJobs.Extensions.OpenApi.Document.Tests/Get_ApplicationJson_BaseObject_Tests.cs

Lines changed: 52 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Linq;
12
using System.Net.Http;
23
using System.Threading.Tasks;
34

@@ -55,32 +56,64 @@ public void Given_OpenApiDocument_Then_It_Should_Not_Return_ReferenceSchema(stri
5556
}
5657

5758
[DataTestMethod]
58-
[DataRow("baseObjectModel", "baseObjectValue", true)]
59-
[DataRow("baseObjectModel", "nonObjectValue", false)]
60-
[DataRow("baseObjectModel", "subObjectValue", false)]
61-
[DataRow("baseSubObjectModel", "baseSubObjectValue", true)]
62-
public void Given_OpenApiDocument_And_BaseObject_Then_It_Should_Return_Expected_Type(string @ref, string propName, bool isBaseObject)
59+
[DataRow("baseObjectModel", "baseObjectValue")]
60+
[DataRow("baseSubObjectModel", "baseSubObjectValue")]
61+
public void Given_OpenApiDocument_And_BaseObject_Then_It_Should_Return_Expected_TypeOf_Object(string @ref, string propName)
6362
{
6463
var schemas = this._doc["components"]["schemas"];
6564

6665
var type = schemas?[@ref]?["properties"]?[propName]?["type"]?.Value<string>() ;
6766

68-
if (isBaseObject)
69-
{
70-
type.Should().Be("object");
71-
}
72-
else
73-
{
74-
type.Should().NotBe("object");
75-
}
67+
type.Should().Be("object");
7668
}
7769

78-
// [TestMethod]
79-
// public void Given_OpenApiDocument_Then_It_Should_Return_Null()
80-
// {
81-
// var @object = this._doc["components"]["schemas"]["object"];
70+
[DataTestMethod]
71+
[DataRow("baseObjectModel", "nonObjectValue")]
72+
[DataRow("baseObjectModel", "subObjectValue")]
73+
public void Given_OpenApiDocument_And_BaseObject_Then_It_Should_Not_Return_Expected_TypeOf_Object(string @ref, string propName)
74+
{
75+
var schemas = this._doc["components"]["schemas"];
76+
77+
var type = schemas?[@ref]?["properties"]?[propName]?["type"]?.Value<string>() ;
78+
79+
type.Should().NotBe("object");
80+
}
81+
82+
[DataTestMethod]
83+
[DataRow("baseObjectModel", "baseObjectList", "array")]
84+
[DataRow("baseObjectModel", "baseObjectDictionary", "object")]
85+
public void Given_OpenApiDocument_And_BaseObject_Then_It_Should_Return_Expected_Type(string @ref, string propName, string listType)
86+
{
87+
var schemas = this._doc["components"]["schemas"];
88+
89+
var type = schemas?[@ref]?["properties"]?[propName]?["type"]?.Value<string>();
90+
91+
type.Should().Be(listType);
92+
}
8293

83-
// @object.Should().BeNull();
84-
// }
94+
[DataTestMethod]
95+
[DataRow("baseObjectModel", "baseObjectList", "items", "object")]
96+
[DataRow("baseObjectModel", "baseObjectDictionary", "additionalProperties", "object")]
97+
public void Given_OpenApiDocument_And_BaseObject_Then_It_Should_Return_Expected_SubType(string @ref, string propName, string attrName, string subType)
98+
{
99+
var schemas = this._doc["components"]["schemas"];
100+
101+
var property = schemas?[@ref]?["properties"]?[propName];
102+
var attr = property[attrName];
103+
104+
attr["type"].Value<string>().Should().Be(subType);
105+
}
106+
107+
[DataTestMethod]
108+
[DataRow("baseObjectModel", "baseObjectList", "items")]
109+
[DataRow("baseObjectModel", "baseObjectDictionary", "additionalProperties")]
110+
public void Given_OpenApiDocument_And_BaseObject_Then_It_Should_Return_Null_Title(string @ref, string propName, string attr)
111+
{
112+
var schemas = this._doc["components"]["schemas"];
113+
114+
var title = schemas?[@ref]?["properties"]?[propName]?[attr]?["title"]?.Value<string>();
115+
116+
title.Should().BeNull();
117+
}
85118
}
86119
}
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
1+
using System.Collections.Generic;
2+
13
namespace Microsoft.Azure.WebJobs.Extensions.OpenApi.TestApp.Models
24
{
35
public class BaseSubObjectModel
46
{
5-
public object BaseSubObjectValue{ get; set; }
7+
public object BaseSubObjectValue { get; set; }
68
}
79

810
public class BaseObjectModel
911
{
1012
public object BaseObjectValue { get; set; }
1113
public int NonObjectValue { get; set; }
1214
public BaseSubObjectModel SubObjectValue { get; set; }
15+
public List<object> BaseObjectList { get; set; }
16+
public Dictionary<string, object> BaseObjectDictionary { get; set; }
1317
}
1418
}

0 commit comments

Comments
 (0)