Skip to content

Commit c1441e0

Browse files
authored
Add ByteType Support (#224)
1 parent c5d78bc commit c1441e0

File tree

7 files changed

+398
-0
lines changed

7 files changed

+398
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Abstractions;
5+
using Microsoft.OpenApi.Models;
6+
7+
using Newtonsoft.Json.Serialization;
8+
9+
namespace Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Visitors
10+
{
11+
/// <summary>
12+
/// This represents the type visitor for byte array.
13+
/// </summary>
14+
public class ByteArrayTypeVisitor : TypeVisitor
15+
{
16+
/// <inheritdoc />
17+
public ByteArrayTypeVisitor(VisitorCollection visitorCollection)
18+
: base(visitorCollection)
19+
{
20+
}
21+
22+
/// <inheritdoc />
23+
public override bool IsVisitable(Type type)
24+
{
25+
var isVisitable = this.IsVisitable(type, TypeCode.Object) && type == typeof(byte[]);
26+
27+
return isVisitable;
28+
}
29+
30+
/// <inheritdoc />
31+
public override void Visit(IAcceptor acceptor, KeyValuePair<string, Type> type, NamingStrategy namingStrategy, params Attribute[] attributes)
32+
{
33+
this.Visit(acceptor, name: type.Key, title: null, dataType: "string", dataFormat: "base64", attributes: attributes);
34+
}
35+
36+
/// <inheritdoc />
37+
public override bool IsParameterVisitable(Type type)
38+
{
39+
var isVisitable = this.IsVisitable(type);
40+
41+
return isVisitable;
42+
}
43+
44+
/// <inheritdoc />
45+
public override OpenApiSchema ParameterVisit(Type type, NamingStrategy namingStrategy)
46+
{
47+
return this.ParameterVisit(dataType: "string", dataFormat: "base64");
48+
}
49+
50+
/// <inheritdoc />
51+
public override bool IsPayloadVisitable(Type type)
52+
{
53+
var isVisitable = this.IsVisitable(type);
54+
55+
return isVisitable;
56+
}
57+
58+
/// <inheritdoc />
59+
public override OpenApiSchema PayloadVisit(Type type, NamingStrategy namingStrategy)
60+
{
61+
return this.PayloadVisit(dataType: "string", dataFormat: "base64");
62+
}
63+
}
64+
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ public override bool IsVisitable(Type type)
5050
{
5151
isVisitable = false;
5252
}
53+
else if(type == typeof(byte[]))
54+
{
55+
isVisitable = false;
56+
}
5357
else if (type.IsOpenApiArray())
5458
{
5559
isVisitable = false;

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,11 @@ protected IOpenApiAny GetOpenApiPropertyDefault(OpenApiPropertyAttribute attr)
257257
return new OpenApiDouble(Convert.ToDouble(@default));
258258
}
259259

260+
if (@default is byte[])
261+
{
262+
return new OpenApiByte((byte[]) @default);
263+
}
264+
260265
if (@default is short)
261266
{
262267
return new OpenApiInteger((short) @default);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
using System.Net.Http;
2+
using System.Threading.Tasks;
3+
4+
using FluentAssertions;
5+
6+
using Microsoft.VisualStudio.TestTools.UnitTesting;
7+
8+
using Newtonsoft.Json;
9+
using Newtonsoft.Json.Linq;
10+
11+
namespace Microsoft.Azure.WebJobs.Extensions.OpenApi.Document.Tests
12+
{
13+
[TestClass]
14+
[TestCategory(Constants.TestCategory)]
15+
public class Post_ApplicationJson_ByteArray_Tests
16+
{
17+
private static HttpClient http = new HttpClient();
18+
19+
private JObject _doc;
20+
21+
[TestInitialize]
22+
public async Task Init()
23+
{
24+
var json = await http.GetStringAsync(Constants.OpenApiDocEndpoint).ConfigureAwait(false);
25+
this._doc = JsonConvert.DeserializeObject<JObject>(json);
26+
}
27+
28+
[DataTestMethod]
29+
[DataRow("/post-applicationjson-bytearray", "post")]
30+
public void Given_OpenApiDocument_Then_It_Should_Return_OperationRequestBody(string path, string operationType)
31+
{
32+
var requestBody = this._doc["paths"][path][operationType]["requestBody"];
33+
34+
requestBody.Should().NotBeNull();
35+
}
36+
37+
[DataTestMethod]
38+
[DataRow("/post-applicationjson-bytearray", "post", "application/octet-stream")]
39+
public void Given_OpenApiDocument_Then_It_Should_Return_OperationRequestBodyContentType(string path, string operationType, string contentType)
40+
{
41+
var content = this._doc["paths"][path][operationType]["requestBody"]["content"];
42+
43+
content[contentType].Should().NotBeNull();
44+
}
45+
46+
[DataTestMethod]
47+
[DataRow("/post-applicationjson-bytearray", "post", "application/octet-stream", "string", "base64")]
48+
public void Given_OpenApiDocument_Then_It_Should_Return_OperationRequestBodyContentTypeSchema(string path, string operationType, string contentType, string propertyType, string propertyFormat)
49+
{
50+
var content = this._doc["paths"][path][operationType]["requestBody"]["content"];
51+
52+
var value = content[contentType]["schema"];
53+
54+
value.Should().NotBeNull();
55+
value.Value<string>("type").Should().Be(propertyType);
56+
value.Value<string>("format").Should().Be(propertyFormat);
57+
}
58+
59+
[DataTestMethod]
60+
[DataRow("/post-applicationjson-bytearray", "post", "200")]
61+
public void Given_OpenApiDocument_Then_It_Should_Return_OperationResponse(string path, string operationType, string responseCode)
62+
{
63+
var responses = this._doc["paths"][path][operationType]["responses"];
64+
65+
responses[responseCode].Should().NotBeNull();
66+
}
67+
68+
[DataTestMethod]
69+
[DataRow("/post-applicationjson-bytearray", "post", "200", "application/json")]
70+
public void Given_OpenApiDocument_Then_It_Should_Return_OperationResponseContentType(string path, string operationType, string responseCode, string contentType)
71+
{
72+
var content = this._doc["paths"][path][operationType]["responses"][responseCode]["content"];
73+
74+
content[contentType].Should().NotBeNull();
75+
}
76+
77+
[DataTestMethod]
78+
[DataRow("/post-applicationjson-bytearray", "post", "200", "application/json", "byteArrayObjectModel")]
79+
public void Given_OpenApiDocument_Then_It_Should_Return_OperationResponseContentTypeSchema(string path, string operationType, string responseCode, string contentType, string reference)
80+
{
81+
var content = this._doc["paths"][path][operationType]["responses"][responseCode]["content"];
82+
83+
var @ref = content[contentType]["schema"]["$ref"];
84+
85+
@ref.Value<string>().Should().Be($"#/components/schemas/{reference}");
86+
}
87+
88+
[DataTestMethod]
89+
[DataRow("byteArrayObjectModel", "object")]
90+
public void Given_OpenApiDocument_Then_It_Should_Return_ComponentSchema(string @ref, string refType)
91+
{
92+
var schemas = this._doc["components"]["schemas"];
93+
94+
var schema = schemas[@ref];
95+
96+
schema.Should().NotBeNull();
97+
schema.Value<string>("type").Should().Be(refType);
98+
}
99+
100+
[DataTestMethod]
101+
[DataRow("byteArrayObjectModel", "byteArrayValue", "string", "base64")]
102+
public void Given_OpenApiDocument_Then_It_Should_Return_ComponentSchemaProperty(string @ref, string propertyName, string propertyType, string propertyFormat)
103+
{
104+
var properties = this._doc["components"]["schemas"][@ref]["properties"];
105+
106+
var value = properties[propertyName];
107+
108+
value.Should().NotBeNull();
109+
value.Value<string>("type").Should().Be(propertyType);
110+
value.Value<string>("format").Should().Be(propertyFormat);
111+
}
112+
}
113+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace Microsoft.Azure.WebJobs.Extensions.OpenApi.TestApp.Models
6+
{
7+
public class ByteArrayObjectModel
8+
{
9+
public byte[] ByteArrayValue { get; set; }
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System.Net;
2+
using System.Threading.Tasks;
3+
4+
using Microsoft.AspNetCore.Http;
5+
using Microsoft.AspNetCore.Mvc;
6+
using Microsoft.Azure.WebJobs.Extensions.Http;
7+
using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Attributes;
8+
using Microsoft.Azure.WebJobs.Extensions.OpenApi.TestApp.Models;
9+
using Microsoft.Extensions.Logging;
10+
using Microsoft.OpenApi.Models;
11+
12+
namespace Microsoft.Azure.WebJobs.Extensions.OpenApi.TestApp
13+
{
14+
public static class Post_ApplicationJson_ByteArray_HttpTrigger
15+
{
16+
[FunctionName(nameof(Post_ApplicationJson_ByteArray_HttpTrigger))]
17+
[OpenApiOperation(operationId: nameof(Post_ApplicationJson_ByteArray_HttpTrigger.Post_ApplicationJson_ByteArray), tags: new[] { "bytearray" })]
18+
[OpenApiRequestBody(contentType: "application/octet-stream", bodyType: typeof(byte[]), Required = true, Description = "The OK response")]
19+
[OpenApiResponseWithBody(statusCode: HttpStatusCode.OK, contentType: "application/json", bodyType: typeof(ByteArrayObjectModel), Description = "The OK response")]
20+
public static async Task<IActionResult> Post_ApplicationJson_ByteArray(
21+
[HttpTrigger(AuthorizationLevel.Anonymous, "POST", Route = "post-applicationjson-bytearray")] HttpRequest req,
22+
ILogger log)
23+
{
24+
var result = new OkResult();
25+
26+
return await Task.FromResult(result).ConfigureAwait(false);
27+
}
28+
}
29+
}

0 commit comments

Comments
 (0)