Skip to content

Commit

Permalink
Update AreAPICodeFilesTheSame (#8641)
Browse files Browse the repository at this point in the history
* Handle AreAPICodeFilesTheSame for cases where the Parser Version is different
* Deserialize code file using correct format
* Sanitize token code file name
  • Loading branch information
chidozieononiwu authored Jul 16, 2024
1 parent f5d5955 commit a52842c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/dotnet/APIView/APIViewWeb/Languages/LanguageProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ public override async Task<CodeFile> GetCodeFileAsync(string originalName, Strea
var tempDirectory = Path.Combine(tempPath, "ApiView", randomSegment);
Directory.CreateDirectory(tempDirectory);
originalName = Path.GetFileName(originalName);
// Replace spaces and parentheses in the file name to remove invalid file name in cosmos DB.
// temporary work around. We need to make sure FileName is set for all requests.
originalName = originalName.Replace(" ", "_").Replace("(", "").Replace(")","");
var originalFilePath = Path.Combine(tempDirectory, originalName);

var jsonFilePath = (LanguageServiceHelpers.UseTreeStyleParser(this.Name)) ? Path.ChangeExtension(originalFilePath, ".json.tgz") : Path.ChangeExtension(originalFilePath, ".json");
Expand Down
5 changes: 5 additions & 0 deletions src/dotnet/APIView/APIViewWeb/Managers/CodeFileManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,11 @@ public async Task<APICodeFileModel> CreateReviewCodeFileModel(string apiRevision
/// <returns></returns>
public async Task<bool> AreAPICodeFilesTheSame(RenderedCodeFile codeFileA, RenderedCodeFile codeFileB)
{
if (codeFileA.CodeFile.VersionString != codeFileA.CodeFile.VersionString)
{
return false;
}

if (LanguageServiceHelpers.UseTreeStyleParser(codeFileA.CodeFile.Language))
{
var diffTree =CodeFileHelpers.ComputeAPIForestDiff(codeFileA.CodeFile.APIForest, codeFileB.CodeFile.APIForest);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,18 @@ public async Task<RenderedCodeFile> GetCodeFileAsync(string revisionId, string c

var info = await client.DownloadAsync();

codeFile = new RenderedCodeFile(await CodeFile.DeserializeAsync(info.Value.Content, doTreeStyleParserDeserialization: LanguageServiceHelpers.UseTreeStyleParser(language)));
CodeFile deserializedCodeFile = null;
// Try to deserialize the code file twice, as the first time might fail due to the file being not yet updated to new tree token format.
// This is a temporary work around. We should have a property in Cosmos revision to indicate whether a token is using new format or old format.
try
{
deserializedCodeFile = await CodeFile.DeserializeAsync(info.Value.Content, doTreeStyleParserDeserialization: LanguageServiceHelpers.UseTreeStyleParser(language));
}
catch
{
deserializedCodeFile = await CodeFile.DeserializeAsync(info.Value.Content, doTreeStyleParserDeserialization: false);
}
codeFile = new RenderedCodeFile(deserializedCodeFile);

if (updateCache)
{
Expand Down

0 comments on commit a52842c

Please sign in to comment.