From 2fe8cc0dca244985d44ffd0988685f1ca6313483 Mon Sep 17 00:00:00 2001 From: Kleidukos Date: Tue, 1 Sep 2026 17:38:46 +0200 Subject: [PATCH] fix(assets): safeguard against null InstrumentCategory during ETF/Fund scans --- FinlyticAssets/Services/AssetsDbService.cs | 21 +++++++++++++------ .../TradeRepublicAssetResponse.cs | 8 ++++++- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/FinlyticAssets/Services/AssetsDbService.cs b/FinlyticAssets/Services/AssetsDbService.cs index eeb11de..b23125d 100644 --- a/FinlyticAssets/Services/AssetsDbService.cs +++ b/FinlyticAssets/Services/AssetsDbService.cs @@ -221,8 +221,10 @@ public class AssetsDbService : IAssetsDbService HasSubtypeChanges(existingEntity, dto)) { existingEntity.Name = dto.Name; - existingEntity.Type = dto.Type; - existingEntity.InstrumentCategory = dto.InstrumentCategory; + existingEntity.Type = dto.Type ?? existingEntity.Type ?? "stock"; + existingEntity.InstrumentCategory = !string.IsNullOrWhiteSpace(dto.InstrumentCategory) + ? dto.InstrumentCategory + : (existingEntity.InstrumentCategory ?? dto.Type ?? "stock"); existingEntity.HasCfd = dto.HasCfd; existingEntity.LastUpdatedAt = now; existingEntity.Tags = mappedTags; @@ -242,6 +244,13 @@ public class AssetsDbService : IAssetsDbService private static AssetEntity MapDtoToEntity(TradeRepublicAsset dto) { + static string ResolveCategory(string? category, string? fallbackType, string defaultCategory) + { + if (!string.IsNullOrWhiteSpace(category)) return category; + if (!string.IsNullOrWhiteSpace(fallbackType)) return fallbackType; + return defaultCategory; + } + return dto switch { TradeRepublicStock stock => new StockEntity @@ -249,7 +258,7 @@ public class AssetsDbService : IAssetsDbService Isin = stock.Isin, Name = stock.Name, Type = stock.Type, - InstrumentCategory = stock.InstrumentCategory, + InstrumentCategory = ResolveCategory(stock.InstrumentCategory, stock.Type, "stock"), HasCfd = stock.HasCfd, DerivativeProductCategories = stock.DerivativeProductCategories?.ToList() ?? new List() }, @@ -258,7 +267,7 @@ public class AssetsDbService : IAssetsDbService Isin = etf.Isin, Name = etf.Name, Type = etf.Type, - InstrumentCategory = etf.InstrumentCategory, + InstrumentCategory = ResolveCategory(etf.InstrumentCategory, etf.Type, "fund"), HasCfd = etf.HasCfd, DerivativeProductCategories = etf.DerivativeProductCategories?.ToList() ?? new List() }, @@ -267,7 +276,7 @@ public class AssetsDbService : IAssetsDbService Isin = syn.Isin, Name = syn.Name, Type = syn.Type, - InstrumentCategory = syn.InstrumentCategory, + InstrumentCategory = ResolveCategory(syn.InstrumentCategory, syn.Type, "synthetic"), HasCfd = syn.HasCfd, DerivativeProductCategories = syn.DerivativeProductCategories?.ToList() ?? new List() }, @@ -276,7 +285,7 @@ public class AssetsDbService : IAssetsDbService Isin = dto.Isin, Name = dto.Name, Type = dto.Type, - InstrumentCategory = dto.InstrumentCategory, + InstrumentCategory = ResolveCategory(dto.InstrumentCategory, dto.Type, "stock"), HasCfd = dto.HasCfd } }; diff --git a/FinlyticCore/Dtos/TradeRepublic/TradeRepublicAssetResponse.cs b/FinlyticCore/Dtos/TradeRepublic/TradeRepublicAssetResponse.cs index 4c6bcab..08ff1c3 100644 --- a/FinlyticCore/Dtos/TradeRepublic/TradeRepublicAssetResponse.cs +++ b/FinlyticCore/Dtos/TradeRepublic/TradeRepublicAssetResponse.cs @@ -27,7 +27,13 @@ public record TradeRepublicAsset [JsonPropertyName("isin")] public string Isin { get; init; } = ""; [JsonPropertyName("name")] public string Name { get; init; } = ""; [JsonPropertyName("type")] public string Type { get; init; } = ""; - [JsonPropertyName("instrumentCategory")] public string InstrumentCategory { get; init; } = ""; + private readonly string _instrumentCategory = ""; + [JsonPropertyName("instrumentCategory")] + public string InstrumentCategory + { + get => _instrumentCategory; + init => _instrumentCategory = value ?? ""; + } [JsonPropertyName("hasCfd")] public bool HasCfd { get; init; } [JsonPropertyName("imageId")] public string? ImageId { get; init; }