Skip to content

Commit 3ad1f71

Browse files
committed
write the binary to base64string and read it as byte[]
1 parent 08976be commit 3ad1f71

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
lines changed

src/Microsoft.OpenApi.Readers/ParseNodes/OpenApiAnyConverter.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,13 +195,17 @@ public static IOpenApiAny GetSpecificOpenApiAny(IOpenApiAny openApiAny, OpenApiS
195195

196196
if (type == "string" && format == "byte")
197197
{
198-
if ( byte.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out var byteValue))
198+
if (byte.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out var byteValue))
199199
{
200200
return new OpenApiByte(byteValue);
201201
}
202202
}
203203

204-
// TODO: Parse byte array to OpenApiBinary type.
204+
// binary
205+
if (type == "string" && format == "binary")
206+
{
207+
return new OpenApiBinary(Convert.FromBase64String(value));
208+
}
205209

206210
if (type == "string" && format == "date")
207211
{

src/Microsoft.OpenApi/Writers/OpenApiWriterAnyExtensions.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT license.
33

4+
using System;
45
using System.Collections.Generic;
56
using Microsoft.OpenApi.Interfaces;
67
using Microsoft.OpenApi.Exceptions;
@@ -170,7 +171,14 @@ private static void WritePrimitive(this IOpenApiWriter writer, IOpenApiPrimitive
170171

171172
case PrimitiveType.Binary:
172173
var binaryValue = (OpenApiBinary)primitive;
173-
writer.WriteValue(binaryValue.Value);
174+
if (binaryValue == null)
175+
{
176+
writer.WriteNull();
177+
}
178+
else
179+
{
180+
writer.WriteValue(Convert.ToBase64String(binaryValue.Value));
181+
}
174182
break;
175183

176184
case PrimitiveType.Boolean:

test/Microsoft.OpenApi.Tests/Models/OpenApiExampleTests.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ public class OpenApiExampleTests
3030
new OpenApiObject
3131
{
3232
["href"] = new OpenApiString("http://example.com/1"),
33-
["rel"] = new OpenApiString("sampleRel1")
33+
["rel"] = new OpenApiString("sampleRel1"),
34+
["binary"] = new OpenApiBinary(new byte[] { 1, 2, 3 })
3435
}
3536
}
3637
},
@@ -117,7 +118,8 @@ public void SerializeAdvancedExampleAsV3JsonWorks()
117118
""links"": [
118119
{
119120
""href"": ""http://example.com/1"",
120-
""rel"": ""sampleRel1""
121+
""rel"": ""sampleRel1"",
122+
""binary"": ""AQID""
121123
}
122124
]
123125
},

0 commit comments

Comments
 (0)