From e1ad9c8a7d15af93ee03a39344111c2f2e2492cc Mon Sep 17 00:00:00 2001 From: Bryn Rhodes Date: Sat, 28 Sep 2024 18:23:00 -0600 Subject: [PATCH] Fixes the error 'duplicate namespace' attempting to translate in an IG context --- .../ls/server/manager/IgContextManager.java | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/ls/server/src/main/java/org/opencds/cqf/cql/ls/server/manager/IgContextManager.java b/ls/server/src/main/java/org/opencds/cqf/cql/ls/server/manager/IgContextManager.java index 025c66a..0452fbc 100644 --- a/ls/server/src/main/java/org/opencds/cqf/cql/ls/server/manager/IgContextManager.java +++ b/ls/server/src/main/java/org/opencds/cqf/cql/ls/server/manager/IgContextManager.java @@ -2,8 +2,10 @@ import java.io.InputStream; import java.net.URI; +import java.util.HashSet; import java.util.Map; import java.util.Optional; +import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import org.cqframework.cql.cql2elm.LibraryManager; import org.cqframework.fhir.npm.ILibraryReader; @@ -69,8 +71,20 @@ public synchronized void setupLibraryManager(URI uri, LibraryManager libraryMana .getModelInfoLoader() .registerModelInfoProvider(new NpmModelInfoProvider( npmProcessor.getPackageManager().getNpmList(), reader, adapter)); - for (NamespaceInfo ni : npmProcessor.getNamespaces()) { - namespaceManager.ensureNamespaceRegistered(ni); + + // TODO: This is a workaround for: a) multiple packages with the same package id will be in the dependency + // list, and b) there are packages with different package ids but the same base canonical (e.g. + // fhir.r4.examples has the same base canonical as fhir.r4) + // NOTE: Using ensureNamespaceRegistered works around a but not b + // NOTE: This logic is also used in org.opencds.cqf.fhir.cql.Engines.buildEnvironment() + Set keys = new HashSet(); + Set uris = new HashSet(); + for (var n : npmProcessor.getNamespaces()) { + if (!keys.contains(n.getName()) && !uris.contains(n.getUri())) { + libraryManager.getNamespaceManager().addNamespace(n); + keys.add(n.getName()); + uris.add(n.getUri()); + } } } }