mtrajano / laravel-swagger

Auto generates the swagger documentation of a laravel project based on best practices and simple assumptions
167 stars 71 forks source link

Support for Swagger UI - view the generated docs #18

Open syntaxlexx opened 4 years ago

mtrajano commented 4 years ago

This is a great suggestion, I’ll look into how difficult this would be. I’m guessing there’s something out there that does this already but it would definitely make sense being part of the lib

syntaxlexx commented 4 years ago

@mtrajano yes there's this package darkaonline/l5-swagger that has the Swagger UI, but it feels very redundant having to require two swagger-api generators in a project; one for generating, and one for viewing the generated docs. It would really help and come in handy if this awesome package would include it.

mtrajano commented 4 years ago

@lexxyungcarter thanks for the pointer I was unsure about this as I thought you needed js to build the files but it looks like Swagger provides bundled js files as a composer package that can be added in a package view file. I will look into this as soon as I’m done working on the securityDefinitions issue https://github.com/mtrajano/laravel-swagger/issues/17. Feel free to take a stab at it if you can get to it before me, thank you!

mtrajano commented 4 years ago

I think ideally this should be a separate command, say laravel-swagger:view or something like that as not everyone might want these files to be added. Would also add a path in the config so the user can choose where the pages should live, maybe default to /docs as that seems to be the convention.

syntaxlexx commented 4 years ago

Yes. That seems right to me. Or even better, publish the view assrts to reources/views/vendor just as other packages such as laravel horizon, mailables, e.t.c.

syntaxlexx commented 4 years ago

Can't wait for the next release of Laravel Swagger!

@mtrajano Is it just me or is there an an issue with 'reading' the type-hinted Requests in the controller? Of late, I can't seem to get mine showing the parameters in the docs.

mtrajano commented 4 years ago

@lexxyungcarter me neither, a lot of exciting stuff coming! And that’s weird if you typehint a FormRequest instance in your controller action it should automatically generate the request params for you, it does that based on the rules array you define. Are you getting some sort of error when running or is it just not picking it up? If you share a specific file it might help when looking into it, additionally you can also take a look at the test files for some examples, thanks!

syntaxlexx commented 4 years ago

@mtrajano No errors pop up, it's just not picking up. Here's an example:


    /**
     * create a new contact message
     *
     * @param ContactMessageRequest $request
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function store(ContactMessageRequest $request)
    {
        $response = $this->repo->create(true);

        if($response)
            return response()->json([
                'message' => 'Thank you for your message. We will keep in touch.',
                'alert' => 'success',
            ], 200);

        return response()->json([
            'message' => 'There was an error sending your message. Please try again or refresh the page.',
            'alert' => 'error',
        ], 422);

    }

Request

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class ContactMessageRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'concern_id' => 'nullable|numeric',
            'name' =>  'required|string|max:255',
            'email' =>  'required|email|max:255',
            'phone' =>  'required|max:20',
            'body' => 'required|max:500',
            'country_id' => 'nullable|numeric|exists:countries,id',
        ];
    }
}

I have a command that calls the laravel swagger command, which is:

        $this->call('laravel-swagger:generate', [
            '--filter' => '/api',
            '--output' => 'storage/api-docs/api-docs.json',
        ]);

However, the generated docs becomes:

         "\/api\/contact-messages": {
            "get": {
                "summary": "return all",
                "description": "",
                "deprecated": false,
                "responses": {
                    "200": {
                        "description": "OK"
                    }
                }
            },
            "post": {
                "summary": "create a new contact message",
                "description": "",
                "deprecated": false,
                "responses": {
                    "200": {
                        "description": "OK"
                    }
                }
            }
        },

As for the other routes, the route parameters get passed as expected, but not the type-hinted Requests. Any clue on what could be wrong? Thank you.

mtrajano commented 4 years ago

Hey, appologies for the late response. It looks like there was a pull request fixing something similar to what you're saying. Would mind upgrading to 0.6.2 and giving it a try again? I can take a look in a few as well and verify, thanks

mtrajano commented 4 years ago

Hey I'm sorry I just got a chance to look at this, a fix has been pushed to master. If you're curious this was the fix: https://github.com/mtrajano/laravel-swagger/commit/9c9e10abce15c96e061d1836b3c4ba150de4da55 it broke during the last pr when someone tried fixing something unrelated. Give it a try and let me know if it works, I'm going to push some other minor things and then release 0.6.3 with this fix

syntaxlexx commented 4 years ago

Wallah! It works now! dev-master branch