Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

not possible to specify an "index" action for a resource with an empty sub-path #48

Open
aetherknight opened this issue Feb 23, 2017 · 0 comments

Comments

@aetherknight
Copy link

Given:

import endpoints
from protorpc import message_types, remote


api_collection = endpoints.api(
    name='someapi',
    version='v1',
    allowed_client_ids=[endpoints.API_EXPLORER_CLIENT_ID],
    scopes=[endpoints.EMAIL_SCOPE]
)

@api_collection.api_class(resource_name='some', path='some')
class SomeApiService(remote.Service):

    @endpoints.method(
        message_types.VoidMessage,
        message_types.VoidMessage,
        path='',
        http_method='GET',
        name='index',
    )
    def some_index(self, request):
        return message_types.VoidMessage()


APPLICATION = endpoints.api_server([api_collection])

I would expect the action's path to be /_ah/api/someapi/v1/some. However, the path is /_ah/api/someapi/v1/some/some_index:

some_index

This issue exists in both endpoints framework 1 and 2.0.2.

Root Cause

Within the decorator function created by endpoints.api_config.method(), it performs a truthy check of the provided path, and defaults to the method's __name__ if it is falsey.

    invoke_remote.method_info = _MethodInfo(
        name=name or api_method.__name__, path=path or api_method.__name__,
        http_method=http_method or DEFAULT_HTTP_METHOD,
        scopes=scopes, audiences=audiences,
        allowed_client_ids=allowed_client_ids, auth_level=auth_level,
        api_key_required=api_key_required,
        request_body_class=request_body_class,
        request_params_class=request_params_class)

However, _MethodInfo.get_path() provides explicit handling of an empty string (which never happens due to @endpoints.method not allowing the empty string):

  def get_path(self, api_info):
  ...
    path = self.__path or ''
    if path and path[0] == '/':
      # Absolute path, ignoring any prefixes.  Just strip off the leading /.
      path = path[1:]
    else:
      # Relative path.
      if api_info.path:
        path = '%s%s%s' % (api_info.path, '/' if path else '', path)

    # Verify that the path seems valid.
    for part in path.split('/'):
      if part and '{' in part and '}' in part:
        if re.match('^{[^{}]+}$', part) is None:
          raise api_exceptions.ApiConfigurationError(
              'Invalid path segment: %s (part of %s)' % (part, path))
    return path

This can be worked around, but it requires not using the path argument to @api_collection.api_class() for specifying the path, in order to give each method decorator a non-empty string.

@aetherknight aetherknight changed the title not possible to specify an "index" action for a resource not possible to specify an "index" action for a resource with an empty sub-path Feb 23, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant