Skip to content

Change OpenApiByte & OpenApiBinary #391

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/Microsoft.OpenApi.Readers/ParseNodes/OpenApiAnyConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Globalization;
using System.Linq;
using System.Text;
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Exceptions;
using Microsoft.OpenApi.Models;
Expand Down Expand Up @@ -195,16 +196,23 @@ public static IOpenApiAny GetSpecificOpenApiAny(IOpenApiAny openApiAny, OpenApiS

if (type == "string" && format == "byte")
{
if (byte.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out var byteValue))
try
{
return new OpenApiByte(byteValue);
return new OpenApiByte(Convert.FromBase64String(value));
}
catch(FormatException)
{ }
}

// binary
if (type == "string" && format == "binary")
{
return new OpenApiBinary(Convert.FromBase64String(value));
try
{
return new OpenApiBinary(Encoding.UTF8.GetBytes(value));
}
catch(EncoderFallbackException)
{ }
Copy link
Contributor

@PerthCharern PerthCharern Mar 14, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try/catch unnecessary. #Closed

Copy link
Contributor Author

@xuzhg xuzhg Mar 14, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leave the uplevel to process the exception? #Closed

Copy link
Contributor

@PerthCharern PerthCharern Mar 14, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which exception case are we expecting here?


In reply to: 265768186 [](ancestors = 265768186)

Copy link
Contributor Author

@xuzhg xuzhg Mar 14, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GetBytes(..) may throw the following exception:

Exceptions:
// T:System.ArgumentNullException:
// s is null.
//
// T:System.Text.EncoderFallbackException:
// A fallback occurred (see Character Encoding in the .NET Framework for complete
// explanation)-and-System.Text.Encoding.EncoderFallback is set to System.Text.EncoderExceptionFallback. #Closed

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it. Let's do this:

  • The ArgNullEx one won't be thrown since we already handle it above.
  • Can we specifically catch the second type of exception?

In reply to: 265771477 [](ancestors = 265771477)

Copy link
Contributor Author

@xuzhg xuzhg Mar 14, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed. #Closed

}

if (type == "string" && format == "date")
Expand Down
10 changes: 9 additions & 1 deletion src/Microsoft.OpenApi/Any/OpenApiByte.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,20 @@ namespace Microsoft.OpenApi.Any
/// <summary>
/// Open API Byte
/// </summary>
public class OpenApiByte : OpenApiPrimitive<byte>
public class OpenApiByte : OpenApiPrimitive<byte[]>
{
/// <summary>
/// Initializes the <see cref="OpenApiByte"/> class.
/// </summary>
public OpenApiByte(byte value)
: this(new byte[] { value })
{
}

Copy link
Contributor

@PerthCharern PerthCharern Mar 14, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this #Closed

Copy link
Contributor Author

@xuzhg xuzhg Mar 14, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this is a breaking change. We should avoid breaking change.
#Closed

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but this is technically a bug fix since our old model is incorrect


In reply to: 265768029 [](ancestors = 265768029)

Copy link
Contributor Author

@xuzhg xuzhg Mar 14, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

public OpenApiByte(byte value) is not a technically bug. Keeping it also provides a method for customer to use. #Closed

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess that's fine.


In reply to: 265771924 [](ancestors = 265771924)

/// <summary>
/// Initializes the <see cref="OpenApiByte"/> class.
/// </summary>
public OpenApiByte(byte[] value)
: base(value)
{
}
Expand Down
22 changes: 20 additions & 2 deletions src/Microsoft.OpenApi/Any/OpenApiPrimitive.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

using System;
using System.Text;
using Microsoft.OpenApi.Exceptions;
using Microsoft.OpenApi.Properties;
using Microsoft.OpenApi.Writers;
Expand Down Expand Up @@ -73,12 +75,28 @@ public void Write(IOpenApiWriter writer, OpenApiSpecVersion specVersion)

case PrimitiveType.Byte:
var byteValue = (OpenApiByte)(IOpenApiPrimitive)this;
writer.WriteValue(byteValue.Value);
if (byteValue.Value == null)
{
writer.WriteNull();
}
else
{
writer.WriteValue(Convert.ToBase64String(byteValue.Value));
}

break;

case PrimitiveType.Binary:
var binaryValue = (OpenApiBinary)(IOpenApiPrimitive)this;
writer.WriteValue(binaryValue.Value);
if (binaryValue.Value == null)
{
writer.WriteNull();
}
else
{
writer.WriteValue(Encoding.UTF8.GetString(binaryValue.Value));
}

break;

case PrimitiveType.Boolean:
Expand Down
77 changes: 3 additions & 74 deletions src/Microsoft.OpenApi/Writers/OpenApiWriterAnyExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

using System;
using System.Collections.Generic;
using Microsoft.OpenApi.Interfaces;
using Microsoft.OpenApi.Exceptions;
using Microsoft.OpenApi.Properties;
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Interfaces;

namespace Microsoft.OpenApi.Writers
{
Expand Down Expand Up @@ -137,76 +134,8 @@ private static void WritePrimitive(this IOpenApiWriter writer, IOpenApiPrimitive
throw Error.ArgumentNull(nameof(primitive));
}

switch (primitive.PrimitiveType)
{
case PrimitiveType.Integer:
var intValue = (OpenApiInteger)primitive;
writer.WriteValue(intValue.Value);
break;

case PrimitiveType.Long:
var longValue = (OpenApiLong)primitive;
writer.WriteValue(longValue.Value);
break;

case PrimitiveType.Float:
var floatValue = (OpenApiFloat)primitive;
writer.WriteValue(floatValue.Value);
break;

case PrimitiveType.Double:
var doubleValue = (OpenApiDouble)primitive;
writer.WriteValue(doubleValue.Value);
break;

case PrimitiveType.String:
var stringValue = (OpenApiString)primitive;
writer.WriteValue(stringValue.Value);
break;

case PrimitiveType.Byte:
var byteValue = (OpenApiByte)primitive;
writer.WriteValue(byteValue.Value);
break;

case PrimitiveType.Binary:
var binaryValue = (OpenApiBinary)primitive;
if (binaryValue == null)
{
writer.WriteNull();
}
else
{
writer.WriteValue(Convert.ToBase64String(binaryValue.Value));
}
break;

case PrimitiveType.Boolean:
var boolValue = (OpenApiBoolean)primitive;
writer.WriteValue(boolValue.Value);
break;

case PrimitiveType.Date:
var dateValue = (OpenApiDate)primitive;
writer.WriteValue(dateValue.Value);
break;

case PrimitiveType.DateTime:
var dateTimeValue = (OpenApiDateTime)primitive;
writer.WriteValue(dateTimeValue.Value);
break;

case PrimitiveType.Password:
var passwordValue = (OpenApiPassword)primitive;
writer.WriteValue(passwordValue.Value);
break;

default:
throw new OpenApiWriterException(
string.Format(
SRResource.PrimitiveTypeNotSupported,
primitive.PrimitiveType));
}
// The Spec version is meaning for the Any type, so it's ok to use the latest one.
primitive.Write(writer, OpenApiSpecVersion.OpenApi3_0);
}
}
}
7 changes: 5 additions & 2 deletions test/Microsoft.OpenApi.Tests/Models/OpenApiExampleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Globalization;
using System.IO;
using System.Text;
using FluentAssertions;
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Models;
Expand Down Expand Up @@ -31,7 +32,8 @@ public class OpenApiExampleTests
{
["href"] = new OpenApiString("http://example.com/1"),
["rel"] = new OpenApiString("sampleRel1"),
["binary"] = new OpenApiBinary(new byte[] { 1, 2, 3 })
["bytes"] = new OpenApiByte(new byte[] { 1, 2, 3 }),
["binary"] = new OpenApiBinary(Encoding.UTF8.GetBytes("Ñ😻😑♮Í☛oƞ♑😲☇éNjžŁ♻😟¥a´Ī♃ƠąøƩ"))
}
}
},
Expand Down Expand Up @@ -119,7 +121,8 @@ public void SerializeAdvancedExampleAsV3JsonWorks()
{
""href"": ""http://example.com/1"",
""rel"": ""sampleRel1"",
""binary"": ""AQID""
""bytes"": ""AQID"",
""binary"": ""Ñ😻😑♮Í☛oƞ♑😲☇éNjžŁ♻😟¥a´Ī♃ƠąøƩ""
}
]
},
Expand Down