Repository: marshmallow-code/apispec

Issue:
Ability to access APISpec.components in plugin init_spec method
Is there a specific reason why components is initialized after plugins init_spec methods are called?

If not, then I am willing to open a PR to solve this issue.

Discussion:

Patch:
diff --git a/AUTHORS.rst b/AUTHORS.rst
index 1ad3e9a..84c119e 100644
--- a/AUTHORS.rst
+++ b/AUTHORS.rst
@@ -62,3 +62,4 @@ Contributors (chronological)
 - Bastien Gerard `@bagerard <https://github.com/bagerard>`_
 - Ashutosh Chaudhary `@codeasashu <https://github.com/codeasashu>`_
 - Fedor Fominykh `@fedorfo <https://github.com/fedorfo>`_
+- Colin Bounouar `@Colin-b <https://github.com/colin-b>`_
diff --git a/src/apispec/core.py b/src/apispec/core.py
index 77b876c..e878321 100644
--- a/src/apispec/core.py
+++ b/src/apispec/core.py
@@ -190,19 +190,19 @@ class APISpec:
         self.version = version
         self.openapi_version = OpenAPIVersion(openapi_version)
         self.options = options
+        self.plugins = plugins
 
         # Metadata
         self._tags = []
         self._paths = OrderedDict()
 
+        # Components
+        self.components = Components(self.plugins, self.openapi_version)
+
         # Plugins
-        self.plugins = plugins
         for plugin in self.plugins:
             plugin.init_spec(self)
 
-        # Components
-        self.components = Components(self.plugins, self.openapi_version)
-
     def to_dict(self):
         ret = {
             "paths": self._paths,


Test patch:
diff --git a/tests/test_core.py b/tests/test_core.py
index 3978632..ce9348f 100644
--- a/tests/test_core.py
+++ b/tests/test_core.py
@@ -276,6 +276,21 @@ class TestComponents:
         spec.components.schema("Pet", properties=self.properties, enum=enum)
         assert spec.to_dict() == yaml.safe_load(spec.to_yaml())
 
+    def test_components_can_be_accessed_by_plugin_in_init_spec(self):
+        class TestPlugin(BasePlugin):
+            def init_spec(self, spec):
+                spec.components.schema(
+                    "TestSchema",
+                    {"properties": {"key": {"type": "string"}}, "type": "object"},
+                )
+
+        spec = APISpec(
+            "Test API", version="0.0.1", openapi_version="2.0", plugins=[TestPlugin()]
+        )
+        assert get_schemas(spec) == {
+            "TestSchema": {"properties": {"key": {"type": "string"}}, "type": "object"}
+        }
+
 
 class TestPath:
     paths = {