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

Conversation

xuzhg
Copy link
Contributor

@xuzhg xuzhg commented Mar 14, 2019

  1. Change OpenApiByte
  2. Change OpenApiBinary
  3. Chang the OpenApiByte & OpenApiBinary serialization/desalinization.

Below are the discussions:

@darrelmiller
Admittedly, the schema is unnecessary, but there are examples in the spec that show this. In this scenario the format: binary is used to indicate that there is no special serialization that is performed on the bytes. They are raw. If you try and present them as a string, they might not create printable text.

This is not a problem in 90% of cases because for the definition of an API you don’t need to include any data. The spec says the parameter contains binary data and tooling knows not to do any processing on it. That’s obviously potentially tricky if it is in a URL parameter! However, for us, the only place it causes a problem is in examples. In examples it is important that the user chooses example data that can be serialized into printable strings. Or not include a sample for binary data as it is somewhat redundant.

This brings me back to my suggestion that for examples of binary data, if they are present, we should just assume that they can successfully be rendered as UTF8 strings and that they will roundtrip. If it doesn’t work, then the user should choose an example of binary data that can be stored in UTF-8 because that’s a limitation of the OpenAPI spec.

This will ensure that round tripping works just fine. However, if someone manually creates an OpenAPI document via the DOM and assigns an arbitrary set of bytes that cannot be represented as UTF-8 characters then it will not round trip. Ironically, this is good because it will discourage people from using format “binary”. If we “fix” this bug to make it serialize properly then we will hide the problem and people will expect other OpenAPI implementations to process the issue properly. But we don’t control how other implementations will interpret binary.

return new OpenApiBinary(Encoding.UTF8.GetBytes(value));
}
catch(Exception)
{ }
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

: 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)

@@ -119,7 +120,8 @@ public void SerializeAdvancedExampleAsV3JsonWorks()
{
""href"": ""http://example.com/1"",
""rel"": ""sampleRel1"",
""binary"": ""AQID""
""bytes"": ""AQID"",
""binary"": "")*+""
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.

)*+ [](start = 26, length = 3)

For the binary case, can we test with a bit more complicated string (something that's not in ASCII)?

Instead of putting in the byte[] specifically above for the binary case, you can use the UTF8.GetBytes function to get the right byte array. #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.

I original use the byte[] { 1, 2, 3 }, but, the output string is not displayable. How can I show that? #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.

Yeah, that's because 1, 2, 3 correspond to some control characters in ASCII (thus non-printable).

I was suggesting something along this line:

  • Here in line 124, do something like ""Ñ😻😑♮Í☛oƞ😲☇éNjŁ😟¥a´Ī♃ƠƩ""
  • Then, in line 35, do UTF8.GetBytes("Ñ😻😑♮Í☛oƞ😲☇éNjŁ😟¥a´Ī♃ƠƩ")

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

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.

:) I changed. #Closed

Copy link
Contributor

@PerthCharern PerthCharern left a comment

Choose a reason for hiding this comment

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

🕐

@PerthCharern
Copy link
Contributor

I will sign off, but given that this issue was quite contentious (for lack of a better word), could we get all 3 of us to agree before checking this in? That is, @darrelmiller, could we get your comments and/or sign-off here as well?

@PerthCharern PerthCharern mentioned this pull request Mar 14, 2019
@darrelmiller
Copy link
Member

Thanks @xuzhg for fixing this and thanks @PerthCharern for catching the issue after I had signed off on it!

@PerthCharern
Copy link
Contributor

Thanks @darrelmiller!

I'll merge this in. Thanks @xuzhg for detecting and addressing the issue.

@PerthCharern PerthCharern merged commit c08eb18 into microsoft:master Mar 15, 2019
@darrelmiller darrelmiller added this to the 1.1.3 milestone Apr 10, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants