Skip to content

Set view function's docstring to descriptions of coreapi.Link on SchemaGenerator #4444

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

Closed
5 of 6 tasks
c-bata opened this issue Aug 26, 2016 · 12 comments
Closed
5 of 6 tasks

Comments

@c-bata
Copy link
Contributor

c-bata commented Aug 26, 2016

Checklist

  • I have verified that that issue exists against the master branch of Django REST framework.
  • I have searched for similar issues in both open and closed tickets and cannot find a duplicate.
  • This is not a usage question. (Those should be directed to the discussion group instead.)
  • This cannot be dealt with as a third party library. (We prefer new functionality to be in the form of third party libraries where possible.)
  • I have reduced the issue to the simplest possible case.
  • I have included a failing test as a pull request. (If you are unable to do so we can still accept the issue.)

Proposal

Hi, It's a proposal of SchemaGenerator.
Now, SchamaGenerator set only url, action, encoding and fields at following source code:

https://github.com/tomchristie/django-rest-framework/blob/master/rest_framework/schemas.py#L220-L242

I think it's very useful to set a docstring to a description of coreapi.Link like:

    def get_link(self, path, method, callback, view):
        """
        Return a `coreapi.Link` instance for the given endpoint.
        """
        :

        return coreapi.Link(
            url=urlparse.urljoin(self.url, path),
            action=method.lower(),
            encoding=encoding,
            fields=fields,
            description=callback.__doc__  # <= here
        )
@decentral1se
Copy link
Contributor

This is being covered in #4241.

@tomchristie
Copy link
Member

The issue with this is that with a view you'd end up with the same descriptions for each of GET, POST, ... although the actions are different.

Or with a viewset, you'd end up with the same descriptions for all the actions on the viewset.

Not obvious what a good way to resolve this would be.

You could use the get() method docstrings or action method docstrings, tho in class based views/viewsets you'll very often have generic implementations, and the docstrings may not be any use.

@tomchristie
Copy link
Member

tomchristie commented Sep 2, 2016

@lwm Agree - although let's leave this specific issue open for now, for further discussion.

@c-bata
Copy link
Contributor Author

c-bata commented Sep 2, 2016

@tomchristie You're right. Thank you for your consideration.

@Skorpyon
Copy link

Skorpyon commented Sep 8, 2016

It may use action handler docstring from actions map:

def create(self, request, **kwargs):
    """
    Create new instance of Icecream.
    """

def delete(self, request, **kwargs):
    """
    Remove Icecream.
    __Warning:__ if Icecream have attached candies, you should remove candies first.
    """

@ghost
Copy link

ghost commented Sep 8, 2016

@Skorpyon what about generic views?
@tomchristie will this be ready by 3.5.0 due date? is there anyway to help?

@tomchristie tomchristie added this to the 3.5.0 Release milestone Sep 8, 2016
@tomchristie
Copy link
Member

@danield137

Having your company sign up to a paid plan is the most impactful way to help collaborate. I'm working full time on the project right now. And yes this issue oughta come up prior to 3.5 but it's not clear yet what the resolution will be.

@abondar
Copy link

abondar commented Sep 15, 2016

@danield137 For documenting generic views I use this implementation for SchemaGenerator

        class SchemaGenerator(schemas.SchemaGenerator):
            def get_link(self, path, method, callback, view):
                fields = self.get_path_fields(path, method, callback, view)
                fields += self.get_serializer_fields(path, method, callback, view)
                fields += self.get_pagination_fields(path, method, callback, view)
                fields += self.get_filter_fields(path, method, callback, view)

                if fields and any([field.location in ('form', 'body') for field in fields]):
                    encoding = self.get_encoding(path, method, callback, view)
                else:
                    encoding = None

                if self.url and path.startswith('/'):
                    path = path[1:]

                return coreapi.Link(
                    url=urlparse.urljoin(self.url, path),
                    action=method.lower(),
                    encoding=encoding,
                    fields=fields,
                    description=view.__doc__
                )

@ghost
Copy link

ghost commented Sep 15, 2016

@Zeliboba5 I am aware of this as a possible workaround, but what I mostly care about is arguments and return types and descriptions.
For now, I wrote it manually instead of generating it from code.

@smcoll
Copy link

smcoll commented Sep 22, 2016

Version 1.x of django-rest-swagger supported YAML docs like this:

class MyViewSet(viewsets.ModelViewSet):
    """
    <shared description>
    ---
    create:
        parameters:
            - name: foo
              required: true
    update:
        parameters:
            - name: foo
              required: false
    myaction:
        parameters:
            - name: bar
              required: true
    """

But essentially, you could either add your docs in each method itself (like @Skorpyon mentioned) or you could bundle the docs in the viewset's docstring.

@tomchristie
Copy link
Member

Now resolved in the upcoming version-3-5 branch.

@c-bata
Copy link
Contributor Author

c-bata commented Oct 5, 2016

Thank you!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants