apispec-webframeworks icon indicating copy to clipboard operation
apispec-webframeworks copied to clipboard

Flask plugin does not gracefully fail when view is None

Open ElementalWarrior opened this issue 4 months ago • 1 comments

I cannot use spec.path without providing view.

In flask.py:

def path_helper(
        self,
        path: Optional[str] = None,
        operations: Optional[dict] = None,
        parameters: Optional[List[dict]] = None,
        *,
        view: Optional[Union[Callable[..., Any], "RouteCallable"]] = None,
        app: Optional[Flask] = None,
        **kwargs: Any,
    ) -> Optional[str]:
        """Path helper that allows passing a Flask view function."""
        assert view is not None

The assert statement completely fails, this means that if someone was to define multiple plugins for apispec, then because this one fails any time view is not defined, there is no way forward.

Inside core.py inside the apispec, library, they handle any PluginMethodNotImplementedErrors, and move on. I would suggest raising that exception instead.

for plugin in self.plugins:
            try:
                ret = plugin.path_helper(
                    path=path, operations=operations, parameters=parameters, **kwargs
                )
            except PluginMethodNotImplementedError:
                continue

ElementalWarrior avatar Feb 15 '24 20:02 ElementalWarrior

IIRC, this exception is made for methods that are not implemented in a plugin. I wouldn't use that.

apispec is not really meant to handle multiple plugins having a path_helper method. It uses the last value that is not None.

Therefore, I'm not sure it would be a good idea to do that.

Still, I guess the method could return None rather than crash.

Would you like to submit a PR?

lafrech avatar Feb 15 '24 20:02 lafrech