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

Support passing of "title" attribute from views to the generated schema Links #5275

Closed
wants to merge 2 commits into from

Conversation

wrsulliv
Copy link

@wrsulliv wrsulliv commented Jul 15, 2017

As described by core-api/python-openapi-codec#28, the "name" field of the generated Swagger JSON is currently hard-coded to "data". When using swagger-codegen, the generated models are labeled <Data*> which breaks the reusability of the generated SDKs.

The proposed solution allows a user to set a "title" property on a view (or viewset) which is then used to generate the "name" field in this format: <Title><Action>Params.

For example, the "post" action of the following viewset will be given a name of "CommentPostParams" instead of "data".

class CommentViewSet(viewsets.ModelViewSet):
    queryset = Comment.objects.all()
    serializer_class = CommentSerializer
    title = "comment"

This pull request has an associated pull request: core-api/python-openapi-codec#42, both of which act to drive further discussion on core-api/python-openapi-codec#28.

Assuming views and viewsets are named <ViewName>View and <ViewSetName>ViewSet, it would also be possible to extract the view name automatically with a small code change.

Related issue: marcgibbons/django-rest-swagger#595

Determine a title from a view instance.

If the 'title' attribute is not set on the view, then an empty title is returned.
"""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

btw, this could be simplified as a simple getattr call.

return getattr(view, 'title', '')

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, thank you, I'll try to remember this in the future.

@wrsulliv wrsulliv changed the title Pass an attribute called "title" from view to generated Link Support passing of "title" attribute on views to the generated schema Links Jul 18, 2017
@wrsulliv wrsulliv changed the title Support passing of "title" attribute on views to the generated schema Links Support passing of "title" attribute from views to the generated schema Links Jul 18, 2017
@tomchristie tomchristie added this to the 3.6.4 Release milestone Jul 19, 2017
@carltongibson
Copy link
Collaborator

I'm going to close this.

From 3.7 the correct way to handle this will be to subclass AutoSchema to set the title as needed:

Something like:

class TitledSchema(AutoSchema):
    
    def __init__(self, title, manual_fields=[]):
        self._title = title
        super().__init__(manual_fields=manual_fields)
        
    def get_link(self, path, method, base_url):
        link = super().get_link(path, method, base_url)
        link.title = self._title
        return link

You can try this now installing master. Feedback on any sticking points welcome.

@gleysonmelo
Copy link

gleysonmelo commented Feb 10, 2019

Hi!
I'm trying to generate an Angular API Client using swagger- codegen, but I had the same problem with the "data*" names.
I've been looking for a solution to this problem for a while, but I couldn't find it.

Tried the 'TitledSchema' sample above, but I receive a "'Link' object doesn't support property assignment" error.
I'm using djangorestframework==3.9.1.

Can you point some example code working in the latest version?

Thanks

UPDATE: Changed Django Rest Swagger for drf-yasg and it worked fine.

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

Successfully merging this pull request may close these issues.

5 participants