diff --git a/AUTHORS.rst b/AUTHORS.rst
index 5eb59fa..6af896f 100644
--- a/AUTHORS.rst
+++ b/AUTHORS.rst
@@ -26,3 +26,4 @@ Contributors (chronological)
 - Dmitry Erlikh `@derlikh-smart <https://github.com/derlikh-smart>`_
 - 0x78f1935 `@0x78f1935 <https://github.com/0x78f1935>`_
 - One Codex, Inc. `@onecodex <https://github.com/onecodex>`_
+- Dorian Hoxha `@ddorian <https://github.com/ddorian>`_
diff --git a/flask_smorest/spec/plugins.py b/flask_smorest/spec/plugins.py
index e235992..cbafe16 100644
--- a/flask_smorest/spec/plugins.py
+++ b/flask_smorest/spec/plugins.py
@@ -110,7 +110,11 @@ class FlaskPlugin(BasePlugin):
     def rule_to_params(self, rule):
         """Get parameters from flask Rule"""
         params = []
-        for argument in [a for a in rule.arguments if a not in rule.defaults]:
+        for argument in [
+            a
+            for is_dynamic, a in rule._trace
+            if is_dynamic is True and a not in rule.defaults
+        ]:
             param = {
                 "in": "path",
                 "name": argument,

diff --git a/tests/test_spec.py b/tests/test_spec.py
index 33eace7..ea0ceb4 100644
--- a/tests/test_spec.py
+++ b/tests/test_spec.py
@@ -35,6 +35,27 @@ class TestAPISpec:
             assert "produces" not in spec
             assert "consumes" not in spec
 
+    @pytest.mark.parametrize("openapi_version", ["2.0", "3.0.2"])
+    def test_apispec_correct_path_parameters_ordering(self, app, openapi_version):
+        """Test path parameters are sorted from left to right.
+
+        If this test is flaky it's considered a failure.
+        """
+        app.config["OPENAPI_VERSION"] = openapi_version
+        api = Api(app)
+
+        blp = Blueprint("pets", "pets", url_prefix="/pets")
+
+        @blp.route("/project/<project_id>/upload/<part_id>/complete")
+        def do_nothing():
+            return
+
+        api.register_blueprint(blp)
+
+        sorted_params = list(api.spec.to_dict()["paths"].values())[0]["parameters"]
+        assert sorted_params[0]["name"] == "project_id"
+        assert sorted_params[1]["name"] == "part_id"
+
     @pytest.mark.parametrize("openapi_version", ["2.0", "3.0.2"])
     def test_apispec_lazy_registers_error_responses(self, app, openapi_version):
         """Test error responses are registered"""

Inconsistent order of parameters when using rule.arguments from werkzeug inside FlaskPlugin
I'm using this with flask-smorest. In https://flask-smorest.readthedocs.io/en/latest/openapi.html#enforce-order-in-openapi-specification-file, you say to use "ordered = True" so they're ordered and I can do snapshot-testing on the openapi json output.

~~But in this line:https://github.com/marshmallow-code/apispec/blob/dev/src/apispec/ext/marshmallow/common.py#L54, `schema._declared_fields` is not an ordered dict, but a simple dict. The `schema.fields` is ordered though.~~

~~Do you have any idea how to fix this even with classes instead of just objects?~~

See https://github.com/marshmallow-code/flask-smorest/issues/541 for correct issue

Regards,
Dorian

tests/test_spec.py::TestAPISpec::test_apispec_correct_path_parameters_ordering[AppConfig-2.0] tests/test_spec.py::TestAPISpec::test_apispec_correct_path_parameters_ordering[AppConfig-3.0.2]
tests/test_spec.py::TestAPISpec::test_apispec_sets_produces_consumes[AppConfig-2.0] tests/test_spec.py::TestAPISpec::test_apispec_sets_produces_consumes[AppConfig-3.0.2] tests/test_spec.py::TestAPISpec::test_apispec_lazy_registers_error_responses[AppConfig-2.0] tests/test_spec.py::TestAPISpec::test_apispec_lazy_registers_error_responses[AppConfig-3.0.2] tests/test_spec.py::TestAPISpec::test_apispec_lazy_registers_etag_headers[AppConfig-2.0] tests/test_spec.py::TestAPISpec::test_apispec_lazy_registers_etag_headers[AppConfig-3.0.2] tests/test_spec.py::TestAPISpec::test_apispec_lazy_registers_pagination_header[AppConfig] tests/test_spec.py::TestAPISpec::test_apispec_delimited_list_documentation[AppConfig-2.0] tests/test_spec.py::TestAPISpec::test_apispec_delimited_list_documentation[AppConfig-3.0.2] tests/test_spec.py::TestAPISpecServeDocs::test_apispec_serve_spec_prefix[AppConfig-None] tests/test_spec.py::TestAPISpecServeDocs::test_apispec_serve_spec_prefix[AppConfig-docs_url_prefix] tests/test_spec.py::TestAPISpecServeDocs::test_apispec_serve_spec_prefix[AppConfig-/docs_url_prefix] tests/test_spec.py::TestAPISpecServeDocs::test_apispec_serve_spec_prefix[AppConfig-docs_url_prefix/] tests/test_spec.py::TestAPISpecServeDocs::test_apispec_serve_spec_prefix[AppConfig-/docs_url_prefix/] tests/test_spec.py::TestAPISpecServeDocs::test_apispec_serve_spec_json_path[AppConfig-None-None] tests/test_spec.py::TestAPISpecServeDocs::test_apispec_serve_spec_json_path[AppConfig-None-docs_url_prefix] tests/test_spec.py::TestAPISpecServeDocs::test_apispec_serve_spec_json_path[AppConfig-spec.json-None] tests/test_spec.py::TestAPISpecServeDocs::test_apispec_serve_spec_json_path[AppConfig-spec.json-docs_url_prefix] tests/test_spec.py::TestAPISpecServeDocs::test_apispec_serve_spec_redoc[AppConfig-None-None-None] tests/test_spec.py::TestAPISpecServeDocs::test_apispec_serve_spec_redoc[AppConfig-None-None-docs_url_prefix] tests/test_spec.py::TestAPISpecServeDocs::test_apispec_serve_spec_redoc[AppConfig-None-redoc-None] tests/test_spec.py::TestAPISpecServeDocs::test_apispec_serve_spec_redoc[AppConfig-None-redoc-docs_url_prefix] tests/test_spec.py::TestAPISpecServeDocs::test_apispec_serve_spec_redoc[AppConfig-https://my-redoc/-None-None] tests/test_spec.py::TestAPISpecServeDocs::test_apispec_serve_spec_redoc[AppConfig-https://my-redoc/-None-docs_url_prefix] tests/test_spec.py::TestAPISpecServeDocs::test_apispec_serve_spec_redoc[AppConfig-https://my-redoc/-redoc-None] tests/test_spec.py::TestAPISpecServeDocs::test_apispec_serve_spec_redoc[AppConfig-https://my-redoc/-redoc-docs_url_prefix] tests/test_spec.py::TestAPISpecServeDocs::test_apispec_serve_spec_swagger_ui[AppConfig-None-None-None] tests/test_spec.py::TestAPISpecServeDocs::test_apispec_serve_spec_swagger_ui[AppConfig-None-None-docs_url_prefix] tests/test_spec.py::TestAPISpecServeDocs::test_apispec_serve_spec_swagger_ui[AppConfig-None-swagger-ui-None] tests/test_spec.py::TestAPISpecServeDocs::test_apispec_serve_spec_swagger_ui[AppConfig-None-swagger-ui-docs_url_prefix] tests/test_spec.py::TestAPISpecServeDocs::test_apispec_serve_spec_swagger_ui[AppConfig-https://my-swagger/-None-None] tests/test_spec.py::TestAPISpecServeDocs::test_apispec_serve_spec_swagger_ui[AppConfig-https://my-swagger/-None-docs_url_prefix] tests/test_spec.py::TestAPISpecServeDocs::test_apispec_serve_spec_swagger_ui[AppConfig-https://my-swagger/-swagger-ui-None] tests/test_spec.py::TestAPISpecServeDocs::test_apispec_serve_spec_swagger_ui[AppConfig-https://my-swagger/-swagger-ui-docs_url_prefix] tests/test_spec.py::TestAPISpecServeDocs::test_apispec_serve_spec_swagger_ui_config[AppConfig] tests/test_spec.py::TestAPISpecServeDocs::test_apispec_serve_spec_rapidoc[AppConfig-None-None-None] tests/test_spec.py::TestAPISpecServeDocs::test_apispec_serve_spec_rapidoc[AppConfig-None-None-docs_url_prefix] tests/test_spec.py::TestAPISpecServeDocs::test_apispec_serve_spec_rapidoc[AppConfig-None-rapidoc-None] tests/test_spec.py::TestAPISpecServeDocs::test_apispec_serve_spec_rapidoc[AppConfig-None-rapidoc-docs_url_prefix] tests/test_spec.py::TestAPISpecServeDocs::test_apispec_serve_spec_rapidoc[AppConfig-https://my-rapidoc/-None-None] tests/test_spec.py::TestAPISpecServeDocs::test_apispec_serve_spec_rapidoc[AppConfig-https://my-rapidoc/-None-docs_url_prefix] tests/test_spec.py::TestAPISpecServeDocs::test_apispec_serve_spec_rapidoc[AppConfig-https://my-rapidoc/-rapidoc-None] tests/test_spec.py::TestAPISpecServeDocs::test_apispec_serve_spec_rapidoc[AppConfig-https://my-rapidoc/-rapidoc-docs_url_prefix] tests/test_spec.py::TestAPISpecServeDocs::test_apispec_serve_spec_rapidoc_config[AppConfig] tests/test_spec.py::TestAPISpecServeDocs::test_apispec_serve_spec_empty_path[AppConfig-json--] tests/test_spec.py::TestAPISpecServeDocs::test_apispec_serve_spec_empty_path[AppConfig-json--/] tests/test_spec.py::TestAPISpecServeDocs::test_apispec_serve_spec_empty_path[AppConfig-json-/-] tests/test_spec.py::TestAPISpecServeDocs::test_apispec_serve_spec_empty_path[AppConfig-json-/-/] tests/test_spec.py::TestAPISpecServeDocs::test_apispec_serve_spec_empty_path[AppConfig-redoc--] tests/test_spec.py::TestAPISpecServeDocs::test_apispec_serve_spec_empty_path[AppConfig-redoc--/] tests/test_spec.py::TestAPISpecServeDocs::test_apispec_serve_spec_empty_path[AppConfig-redoc-/-] tests/test_spec.py::TestAPISpecServeDocs::test_apispec_serve_spec_empty_path[AppConfig-redoc-/-/] tests/test_spec.py::TestAPISpecServeDocs::test_apispec_serve_spec_empty_path[AppConfig-swagger-ui--] tests/test_spec.py::TestAPISpecServeDocs::test_apispec_serve_spec_empty_path[AppConfig-swagger-ui--/] tests/test_spec.py::TestAPISpecServeDocs::test_apispec_serve_spec_empty_path[AppConfig-swagger-ui-/-] tests/test_spec.py::TestAPISpecServeDocs::test_apispec_serve_spec_empty_path[AppConfig-swagger-ui-/-/] tests/test_spec.py::TestAPISpecServeDocs::test_apispec_serve_spec_empty_path[AppConfig-rapidoc--] tests/test_spec.py::TestAPISpecServeDocs::test_apispec_serve_spec_empty_path[AppConfig-rapidoc--/] tests/test_spec.py::TestAPISpecServeDocs::test_apispec_serve_spec_empty_path[AppConfig-rapidoc-/-] tests/test_spec.py::TestAPISpecServeDocs::test_apispec_serve_spec_empty_path[AppConfig-rapidoc-/-/] tests/test_spec.py::TestAPISpecServeDocs::test_apispec_serve_spec_preserve_order[AppConfig] tests/test_spec.py::TestAPISpecServeDocs::test_multiple_apis_serve_separate_specs[AppConfig] tests/test_spec.py::TestAPISpecCLICommands::test_apispec_command_print[AppConfig-'openapi tests/test_spec.py::TestAPISpecCLICommands::test_apispec_command_print_output_yaml_no_yaml_module[AppConfig] tests/test_spec.py::TestAPISpecCLICommands::test_apispec_command_write[AppConfig-'openapi tests/test_spec.py::TestAPISpecCLICommands::test_apispec_command_write_output_yaml_no_yaml_module[AppConfig] tests/test_spec.py::TestAPISpecCLICommands::test_apispec_command_print_with_multiple_apis[AppConfig] tests/test_spec.py::TestAPISpecCLICommands::test_apispec_command_write_with_multiple_apis[AppConfig] tests/test_spec.py::TestAPISpecCLICommands::test_apispec_command_list_config_prefixes[AppConfig]