Skip to content

Commit

Permalink
The OriginalName property was not set properly
Browse files Browse the repository at this point in the history
  • Loading branch information
Vincent Wilms committed Dec 18, 2024
1 parent 8d694a9 commit cf06586
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 23 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## v2.0.0-beta.34 - 2024-12-18

- The OriginalName property was not set properly

## v2.0.0-beta.33 - 2024-12-17

- Follow Nexus changes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@ public static class StructuredFileDataModelExtensions
/// </summary>
public const string FileSourceIdKey = "file-source-id";

/// <summary>
/// A constant with the key for a original name property.
/// </summary>
public const string OriginalNameKey = "original-name";

/// <summary>
/// Adds a file source id property.
/// </summary>
Expand All @@ -28,16 +23,5 @@ public static ResourceBuilder WithFileSourceId(this ResourceBuilder resourceBuil
return resourceBuilder.WithProperty(FileSourceIdKey, fileSourceId);
}

/// <summary>
/// Adds an original name property.
/// </summary>
/// <param name="resourceBuilder">The resource builder.</param>
/// <param name="originalName">The original name to add.</param>
/// <returns>A resource catalog builder.</returns>
public static ResourceBuilder WithOriginalName(this ResourceBuilder resourceBuilder, string originalName)
{
return resourceBuilder.WithProperty(OriginalNameKey, originalName);
}

#endregion
}
43 changes: 38 additions & 5 deletions src/Nexus.Sources.StructuredFile/StructuredFileDataSource.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Buffers;
using System.Diagnostics.CodeAnalysis;
using System.Text.Json;
using System.Text.RegularExpressions;
using Microsoft.Extensions.Logging;
using Nexus.DataModel;
Expand Down Expand Up @@ -501,7 +502,7 @@ protected virtual async Task ReadAsync(
);

var originalName = catalogItem.Resource.Properties?
.GetStringValue(StructuredFileDataModelExtensions.OriginalNameKey)!;
.GetStringValue(DataModelExtensions.OriginalNameKey) ?? throw new Exception("The OriginalName property is not set.");

return new ReadRequest(
OriginalResourceName: originalName,
Expand Down Expand Up @@ -744,13 +745,45 @@ async Task<ResourceCatalog> IDataSource.EnrichCatalogAsync(

if (catalog.Resources is not null)
{
// TODO: code duplication (DataModelExtensions.cs)
var isModified = false;
var newResources = new List<Resource>();

foreach (var resource in catalog.Resources)
{
// ensure file source id
var fileSourceId = resource.Properties?.GetStringValue(StructuredFileDataModelExtensions.FileSourceIdKey);
var resourceProperties = resource.Properties;
var newResource = resource;

var originalName = resourceProperties?
.GetStringValue(DataModelExtensions.OriginalNameKey);

if (originalName is null)
{
var newResourceProperties = resourceProperties is null
? []
: resourceProperties!.ToDictionary(entry => entry.Key, entry => entry.Value);

// Original name
if (originalName is null)
newResourceProperties[DataModelExtensions.OriginalNameKey] = JsonSerializer.SerializeToElement(resource.Id);

newResource = resource with
{
Properties = newResourceProperties
};

if (string.IsNullOrWhiteSpace(fileSourceId))
throw new Exception($"The resource {resource.Id} is missing the file source property.");
isModified = true;
}

newResources.Add(newResource);
}

if (isModified)
{
catalog = catalog with
{
Resources = newResources
};
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using Nexus.Extensibility;
using System.Globalization;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text.Json;
using Xunit;
Expand Down
2 changes: 1 addition & 1 deletion version.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"version": "2.0.0",
"suffix": "beta.33"
"suffix": "beta.34"
}

0 comments on commit cf06586

Please sign in to comment.