May Sale! All accounts are 40% off this week.

Sinres's avatar

API documentation with Swagger

Hello Guy's!

I have decided that I need to use a documentation generator in my project like Swagger. I used package l5-swagger and I think the design is getting messy.

In order not to make a virtual directory and create models with defined properties there some like here Visit https://blog.quickadminpanel.com/laravel-api-documentation-with-openapiswagger/

I thought to do it in the eloquent model like this:

/**
 * @OA\Schema(
 *     title="User",
 *     description="User model",
 *      @OA\Property(format="int64", title="ID", property="id", description="User ID.", example=1),
 *      @OA\Property(format="int64", title="Name", property="name", description="User Name.", example="Adam"),
 *      @OA\Property(format="email", title="Email" property="email", description="User Email.", example="[email protected]"),
 *      @OA\Property(format="datetime", property="email_verified_at", description="Email verified at.", example="2021-05-18 07:50:45"),
 *      @OA\Property(format="datetime", property="created_at", description="Created at.", example="2021-05-18 07:50:45"),
 *      @OA\Property(format="datetime", property="updated_at", description="Updated at.", example="2021-05-18 07:50:45"),
 * )
 */
class User extends Authenticatable
{


}

Any sugestion ? I want the project to have a well-documented API but keeping the code in order.

How it is used by you while keeping the design transparent?

0 likes
2 replies
martinbean's avatar

@sinres Annotations are the devil and just make your code “busy”. You can write your OpenAPI specs as YAML and keep them separate from your actual codebase.

There are GUI tools for making writing specs easier, such as Stoplight Studio.

It also means you can write your API specs before the actual code, and then create mock servers based on your specs to integrate with whilst you’re building out the actual API. I did this in my previous role and it meant I could give front-end developers a mock of the API the team was working on whilst we actually built out the endpoints, and speeds up development time (as front-end developers don’t then have to wait for the API before they can even start their work). You can’t do that with annotations, which need to be placed above your actual methods, meaning you can’t annotate and spec your API until you’ve actually created the controllers and actions.

1 like

Please or to participate in this conversation.