@@ -178,6 +178,8 @@ public static Pipeline fromDB(DBAccess dbAccess, String pipelineId) {
178178
179179 public static Pipeline generatePipelineFromJSONView (JSONView pipelineView , DBAccess dbAccess ) {
180180 try {
181+ pipelineView = new JSONView (mergeGeneratorsIntoSources (pipelineView .asMap ()));
182+
181183 String id = getJSONViewString (pipelineView , "id" );
182184 JSONView sourcesView = pipelineView .get ("sources" );
183185 HashMap <String , Generator > generators = new HashMap <>();
@@ -369,11 +371,54 @@ private static String getJSONViewString(JSONView view, String name) {
369371 }
370372
371373
372- private static String [] keysOnlyInA (Map <String , ?> mapA , Map <String , ?> mapB ) {
373- List <String > uniqueKeys = new ArrayList <>();
374- for (String key : mapA .keySet ()) {
375- if (!mapB .containsKey (key )) { uniqueKeys .add (key ); }
374+ @ SuppressWarnings ("unchecked" )
375+ private static Map <String , Object > mergeGeneratorsIntoSources (Map <String , Object > input ) {
376+ Map <String , Object > result = new LinkedHashMap <>(input );
377+
378+ // Deep-copy sources into a new list so we don't mutate the original
379+ List <Map <String , Object >> originalSources = (List <Map <String , Object >>) input .get ("sources" );
380+ List <Map <String , Object >> newSources = new ArrayList <>();
381+
382+ // Build a map of sourceId -> createsGenerators list for quick lookup
383+ Map <String , List <Map <String , Object >>> sourceGeneratorMap = new LinkedHashMap <>();
384+
385+ for (Map <String , Object > source : originalSources ) {
386+ Map <String , Object > newSource = new LinkedHashMap <>(source );
387+ String sourceId = (String ) source .get ("id" );
388+
389+ // Ensure createsGenerators exists; copy existing ones if present
390+ List <Map <String , Object >> existingGenerators =
391+ (List <Map <String , Object >>) source .getOrDefault ("createsGenerators" , new ArrayList <>());
392+ List <Map <String , Object >> generatorList = new ArrayList <>(existingGenerators );
393+
394+ newSource .put ("createsGenerators" , generatorList );
395+ sourceGeneratorMap .put (sourceId , generatorList );
396+ newSources .add (newSource );
397+ }
398+
399+ // Iterate over standalone generators and merge them into the matching source
400+ List <Map <String , Object >> standaloneGenerators =
401+ (List <Map <String , Object >>) input .getOrDefault ("generators" , new ArrayList <>());
402+
403+ for (Map <String , Object > generator : standaloneGenerators ) {
404+ String sourceId = (String ) generator .get ("source" );
405+
406+ List <Map <String , Object >> targetList = sourceGeneratorMap .get (sourceId );
407+ if (targetList == null ) {
408+ throw new IllegalArgumentException (
409+ "Generator references unknown source id: " + sourceId );
410+ }
411+
412+ // Copy the generator without the "source" key, as it's now implied by nesting
413+ Map <String , Object > strippedGenerator = new LinkedHashMap <>(generator );
414+ strippedGenerator .remove ("source" );
415+ targetList .add (strippedGenerator );
376416 }
377- return uniqueKeys .toArray (new String [0 ]);
417+
418+ // Build the result: same top-level structure but with updated sources and no "generators"
419+ result .put ("sources" , newSources );
420+ result .remove ("generators" );
421+
422+ return result ;
378423 }
379424}
0 commit comments