jmcarp / flask-apispec

MIT License
655 stars 157 forks source link

Extracting documentation from resource #135

Open YnkDK opened 5 years ago

YnkDK commented 5 years ago

Is it possible to extract documentation from the docstring, e.g.

class CatResource(MethodResource):

    @marshal_with(PetSchema)
    def get(self, pet_id):
        """Gets the cat with the given id

        Lorem ipsum dolor sit amet, ea aeque commodo lucilius nam. Est tation 
        libris periculis eu, tale oporteat efficiantur eu quo. Choro integre has
        cu. Et mea mutat habemus vulputate, iudico prompta facilisis pro te,
        porro fugit labore ut cum. Has audiam alienum apeirian in, mel ut vero
        porro option, aliquip mediocrem accusamus mea an. In his iisque laboramus 
        efficiendi.

        Inermis volutpat ne his, an populo debitis complectitur vix. Eros
        bonorum feugait no has. Eu duis autem per, eu his paulo neglegentur
        necessitatibus. Zril voluptaria mel at, saperet dissentiet in duo.
        """
        return Pet('calici', 'cat')

This would be equivalent to what I am doing at the moment:

class CatResource(MethodResource):

    @marshal_with(PetSchema)
    @doc(
        summary='Gets the cat with the given id',
        description='Lorem ipsum dolor sit amet, ea aeque commodo lucilius nam. Est tation '
                    'libris periculis eu, tale oporteat efficiantur eu quo. Choro integre has '
                    'cu. Et mea mutat habemus vulputate, iudico prompta facilisis pro te, '
                    'porro fugit labore ut cum. Has audiam alienum apeirian in, mel ut vero '
                    'porro option, aliquip mediocrem accusamus mea an. In his iisque laboramus ' 
                    'efficiendi.'
                    '\n'
                    '\n'
                    'Inermis volutpat ne his, an populo debitis complectitur vix. Eros '
                    'bonorum feugait no has. Eu duis autem per, eu his paulo neglegentur '
                    'necessitatibus. Zril voluptaria mel at, saperet dissentiet in duo.')
    def get(self, pet_id):
        return Pet('calici', 'cat')

The docstring itself is easy to extract, e.g. CatResource.get.__doc__. Is this already possible? When the description becomes very descriptive (e.g. more than 72 characters which results in a new line according to PEP 8) it becomes harder to read and format, than if it has been in the docstring of the method.

rth commented 5 years ago

Using multi-line strings with textwrap.dedent might help somewhat,


from textwrap import dedent

class CatResource(MethodResource):

   @marshal_with(PetSchema)
   @doc(summary='Gets the cat with the given id',
        description=dedent(
        """
        Lorem ipsum dolor sit amet, ea aeque commodo lucilius nam. Est tation 
        libris periculis eu, tale oporteat efficiantur eu quo. Choro integre has
        cu. Et mea mutat habemus vulputate, iudico prompta facilisis pro te,
        porro fugit labore ut cum. Has audiam alienum apeirian in, mel ut vero
        porro option, aliquip mediocrem accusamus mea an. In his iisque laboramus 
        efficiendi.

        Inermis volutpat ne his, an populo debitis complectitur vix. Eros
        bonorum feugait no has. Eu duis autem per, eu his paulo neglegentur
        necessitatibus. Zril voluptaria mel at, saperet dissentiet in duo.
        """)
   def get(self, pet_id):
       return Pet('calici', 'cat')