using System.Collections.Concurrent; using Json.Schema; namespace NeonSprawl.Server; /// /// Thread-safe registration for (JsonSchema.Net registry is not concurrent). /// Used by content catalog loaders when parallel integration tests spin up multiple hosts. /// internal static class CatalogSchemaRegistry { private static readonly Lock RegisterLock = new(); private static readonly ConcurrentDictionary SchemasByPath = new(StringComparer.Ordinal); /// Parses once per process and registers it under a global lock. public static JsonSchema GetOrRegisterFromFile(string schemaFilePath) { var fullPath = Path.GetFullPath(schemaFilePath); if (SchemasByPath.TryGetValue(fullPath, out var cached)) return cached; lock (RegisterLock) { if (SchemasByPath.TryGetValue(fullPath, out cached)) return cached; var schema = JsonSchema.FromText(File.ReadAllText(fullPath)); SchemaRegistry.Global.Register(schema); SchemasByPath[fullPath] = schema; return schema; } } }