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 for Swagger UI - view the generated docs #18

Open
syntaxlexx opened this issue Dec 16, 2019 · 11 comments · Fixed by #24
Open

Support for Swagger UI - view the generated docs #18

syntaxlexx opened this issue Dec 16, 2019 · 11 comments · Fixed by #24

Comments

@syntaxlexx
Copy link

No description provided.

@mtrajano
Copy link
Owner

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
Copy link
Author

@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
Copy link
Owner

@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 #17. Feel free to take a stab at it if you can get to it before me, thank you!

@mtrajano
Copy link
Owner

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
Copy link
Author

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
Copy link
Author

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
Copy link
Owner

mtrajano commented Feb 4, 2020

@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
Copy link
Author

@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
Copy link
Owner

mtrajano commented Feb 5, 2020

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
Copy link
Owner

mtrajano commented Feb 6, 2020

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: 9c9e10a 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
Copy link
Author

Wallah! It works now! dev-master branch

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