Repository: linkml/linkml

Issue:
gen-linkml CLI tool does not honor (default) --mergeimports parameter
`gen-linkml` advertises a `--mergeimports` parameter and even states that merging is the default state, as compared to `--no-mergeimports`

>   --mergeimports / --no-mergeimports
                                  Merge imports into source file
                                  (default=mergeimports)

However, the output of running the MIxS schema through `gen-linkml` does not contain the imports

```shell
poetry run gen-linkml \
		--format yaml \
		--mergeimports \
		--no-materialize-attributes model/schema/mixs.yaml
```

---

```yaml
name: MIxS
description: Minimal Information about any Sequence Standard
id: http://w3id.org/mixs
imports:
  - linkml:types
  - checklists
  - core
  - agriculture
  - food_animal_and_animal_feed
  - food_farm_environment
  - food_food_production_facility
  - food_human_foods
  - symbiont_associated
  - host_associated
  - microbial_mat_biofilm
  - miscellaneous_natural_or_artificial_environment
  - plant_associated
  - sediment
  - soil
  - wastewater_sludge
  - water
  - human_associated
  - human_gut
  - human_oral
  - human_skin
  - human_vaginal
  - air
  - built_environment
  - hydrocarbon_resources_cores
  - hydrocarbon_resources_fluids_swabs
prefixes:
  linkml:
    prefix_prefix: linkml
    prefix_reference: https://w3id.org/linkml/
  mixs.vocab:
    prefix_prefix: mixs.vocab
    prefix_reference: https://w3id.org/mixs/vocab/
  MIXS:
    prefix_prefix: MIXS
    prefix_reference: https://w3id.org/mixs/terms/
  MIGS:
    prefix_prefix: MIGS
    prefix_reference: https://w3id.org/mixs/migs/
default_prefix: mixs.vocab
```

Discussion:

Patch:
diff --git a/linkml/generators/linkmlgen.py b/linkml/generators/linkmlgen.py
index df94c703..a03a58ce 100644
--- a/linkml/generators/linkmlgen.py
+++ b/linkml/generators/linkmlgen.py
@@ -35,8 +35,8 @@ class LinkmlGenerator(Generator):
 
     def __post_init__(self):
         # TODO: consider moving up a level
-        self.schemaview = SchemaView(self.schema)
         super().__post_init__()
+        self.schemaview = SchemaView(self.schema, merge_imports=self.mergeimports)
 
     def materialize_classes(self) -> None:
         """Materialize class slots from schema as attribues, in place"""


Test patch:
diff --git a/tests/test_generators/test_linkmlgen.py b/tests/test_generators/test_linkmlgen.py
index eb7fa862..a9dd9488 100644
--- a/tests/test_generators/test_linkmlgen.py
+++ b/tests/test_generators/test_linkmlgen.py
@@ -25,7 +25,7 @@ class LinkMLGenTestCase(unittest.TestCase):
         self.assertNotIn("activity", sv.all_classes(imports=False))
         self.assertListEqual(["is_living"], list(sv.get_class("Person").attributes.keys()))
 
-        gen = LinkmlGenerator(SCHEMA, format='yaml')
+        gen = LinkmlGenerator(SCHEMA, format='yaml', mergeimports=False)
         out = gen.serialize()
         # TODO: restore this when imports works for string inputs
         #schema2 = YAMLGenerator(out).schema
@@ -39,12 +39,22 @@ class LinkMLGenTestCase(unittest.TestCase):
         self.assertEqual(len(yobj["classes"]), len(sv.all_classes(imports=False)))
         # self.assertNotIn("attributes", yobj["classes"]["Person"])
         # test with material-attributes option
-        gen2 = LinkmlGenerator(SCHEMA, format='yaml')
+        gen2 = LinkmlGenerator(SCHEMA, format='yaml', mergeimports=False)
         gen2.materialize_attributes = True
         out2 = gen2.serialize()
         yobj2 = yaml.safe_load(out2)
         self.assertEqual(len(yobj2["classes"]), len(sv.all_classes(imports=False)))
         self.assertIn("attributes", yobj2["classes"]["Person"])
+        self.assertNotIn("activity", yobj2["classes"])
+        self.assertNotIn("agent", yobj2["classes"])
+
+        # turn on mergeimports option
+        gen3 = LinkmlGenerator(SCHEMA, format="yaml", mergeimports=True)
+        out3 = gen3.serialize()
+        yobj3 = yaml.safe_load(out3)
+        self.assertEqual(len(yobj3["classes"]), len(sv.all_classes(imports=True)))
+        self.assertIn("activity", yobj3["classes"])
+        self.assertIn("agent", yobj3["classes"])
 
         # test that structured patterns are being expanded
         # and populated into the pattern property on a class