Skip to content

Commit

Permalink
account for version parsring error
Browse files Browse the repository at this point in the history
  • Loading branch information
anamnavi committed Oct 11, 2024
1 parent 5c4c7cd commit c9746cf
Showing 1 changed file with 47 additions and 1 deletion.
48 changes: 47 additions & 1 deletion src/code/V2ResponseUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
// Licensed under the MIT License.

using Microsoft.PowerShell.PSResourceGet.UtilClasses;
using NuGet.Versioning;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml;

namespace Microsoft.PowerShell.PSResourceGet.Cmdlets
Expand Down Expand Up @@ -70,6 +72,9 @@ public override IEnumerable<PSResourceResult> ConvertToPSResourceResult(FindResu
#region V2 Specific Methods

public XmlNode[] ConvertResponseToXML(string httpResponse) {
NuGetVersion emptyVersion = new NuGetVersion("0.0.0.0");
NuGetVersion firstVersion = emptyVersion;
NuGetVersion lastVersion = emptyVersion;

//Create the XmlDocument.
XmlDocument doc = new XmlDocument();
Expand All @@ -80,7 +85,48 @@ public XmlNode[] ConvertResponseToXML(string httpResponse) {
XmlNode[] nodes = new XmlNode[entryNode.Count];
for (int i = 0; i < entryNode.Count; i++)
{
nodes[i] = entryNode[i];
XmlNode node = entryNode[i];
nodes[i] = node;
var entryChildNodes = node.ChildNodes;
foreach (XmlElement childNode in entryChildNodes)
{
var entryKey = childNode.LocalName;
if (entryKey.Equals("properties"))
{
var propertyChildNodes = childNode.ChildNodes;
foreach (XmlElement propertyChild in propertyChildNodes)
{
var propertyKey = propertyChild.LocalName;
var propertyValue = propertyChild.InnerText;
if (propertyKey.Equals("NormalizedVersion"))
{
if (!NuGetVersion.TryParse(propertyValue, out NuGetVersion parsedNormalizedVersion))
{
parsedNormalizedVersion = emptyVersion;
}

if (i == 0)
{
firstVersion = parsedNormalizedVersion;
}
else
{
// later version element
lastVersion = parsedNormalizedVersion;
}

break; // don't care about rest of the childNode's properties
}
}
}
}
}

// only order the array in desc order if array has more than 1 element and is currently in ascending order
// check for emptyVersion is in case a version that couldn't be parsed was found, just keep ordering as is.
if (nodes.Length > 1 && firstVersion != emptyVersion && lastVersion != emptyVersion && firstVersion < lastVersion)
{
nodes = nodes.Reverse().ToArray();
}

return nodes;
Expand Down

0 comments on commit c9746cf

Please sign in to comment.