Thursday, May 18, 2017

Adobe Reader caused Firefox to crash

We have an object tag on the page and it points to a pdf.

<object data="https://www.somedomain.com./somedocument.pdf">
</object>

Weird scenario is some of us experience Adobe reader unable to open the document and crashed Firefox altogether. After many hours of search, I found out the problem was due to html encoded url. It is supposed to be https://www.somedomain.com/somedocument.pdf#page=1&viewrect=1,2,3,4 but instead it becomes https://www.somedomain.com/somedocument.pdf#page=1&amp;viewrect=1,2,3,4 and Adobe crashed after parsing that the encoded part. Removing the encoding fixed the issue.

Friday, May 5, 2017

XMLSerializer Anyone

I rewrote a project to utilize XMLSerializer instead of manually append xml tags. It works great until I encountered problem with backward compatibility on Boolean type. Our client is sending boolean value with first letter capitalized. "True" and "False" and apparently XMLSerializer were unable to deserialize the value as boolean. Because by convention, boolean in XML has to be all lower case. Searching online I found out that I need to do a small trick.

Instead of:

<XmlElement("isvalid")>
Public Property IsValid As Boolean

I have to rewrite it as:

<XmlElement("isvalid")>
Public Property IsValidString As String
   Get
      Return IsValid.ToString().ToLower()
   End Get
   Set
      Boolean.TryParse(value, IsValid)
   End Set
End Property

<XmlIgnore>
Public Property IsValid As Boolean

The modified version can handle both formats.

Path.Combine and Path.GetFullPath

I bumped into a case in which I need to resolve a relative path to a file, but the problem is it doesn't resolve as expected when Path.GetFullPath is used.

For example:
path1 = "C:\file\"
path2 = "..\test.txt"
Path.Combine produces "C:\file\..\test.txt"
Path.GetFullPath((New Uri(Path.Combine(path1,path2))).LocalPath) produces "C:\test.txt" which is correct.

The problem starts when I realized another slash was accidentally appended to the end of path1.

path1 = "C:\file\"
path2 = "..\test.txt"
Path.Combine produces "C:\file\\..\test.txt"
Path.GetFullPath((New Uri(Path.Combine(path1,path2))).LocalPath) produces "C:\file\test.txt" which is correct but unexpected.