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

How to document queryParam e.g. filter[id] #637

Closed
tecbeast42 opened this issue Nov 6, 2019 · 12 comments
Closed

How to document queryParam e.g. filter[id] #637

tecbeast42 opened this issue Nov 6, 2019 · 12 comments

Comments

@tecbeast42
Copy link

tecbeast42 commented Nov 6, 2019

I use a filter pages which requires me to define filter params like.

filter[id]

This seems to break doc generation. Both languages bash and javascript field to compile with v3.17.1 of laravel-apidoc-generator.

This happends because the cleanQueryParameters are of the route is broken during the markdown generation step.

array:1 [
  "filter" => array:1 [
    "id" => "sint"
  ]
]

this should be

array:1 [
  "filter[id]" => "factoryValue"
]

I tracked it down to Mpociot\ApiDoc\Tools\Generator::generateConcreteSampleForArrayKeys. If i comment the use of this function out markdown generates fine.

Is there a way to enable handle ?

@shalvah
Copy link
Contributor

shalvah commented Nov 7, 2019

Try out the new version 4. See the migration guide.

@ncatanchin
Copy link
Contributor

@shalvah I have upgraded to v4, great work!

I was not able to get query parameters as arrays working without making changes to the example-requests templates.

The below is what I did to get the bash example working - essentially, check if $value is an array, and output only the first value. Also added the square brackets - [] after the parameter name.

curl -X {{$route['methods'][0]}} \
    {{$route['methods'][0] == 'GET' ? '-G ' : ''}}"{{ rtrim($baseUrl, '/')}}/{{ ltrim($route['boundUri'], '/') }}@if(count($route['cleanQueryParameters']))?@foreach($route['cleanQueryParameters'] as $parameter => $value)
    @if (is_array($value))
        {{ urlencode($parameter) }}[]={{ urlencode($value[0]) }}@if(! $loop->last)&@endif
    @else
        {{ urlencode($parameter) }}={{ urlencode($value) }}@if(!$loop->last)&@endif
    @endif
@endforeach

The same approach seemed to work for the PHP template:

@foreach($route['cleanQueryParameters'] as $parameter => $value)
    @if (is_array($value))
            '{{$parameter}}[]' => '{{$value[0]}}',
    @else
            '{{$parameter}}' => '{{$value}}',
    @endif
@endforeach

@tecbeast42
Copy link
Author

I tried v4.0.2 with not success. The error is still the same. Should i open a PR? What is the use of generateConcreteSampleForArrayKeys.

lucianobargmann added a commit to lucianobargmann/laravel-apidoc-generator that referenced this issue Dec 6, 2019
Picks up on solution discussed at mpociot#637 
There is an error in the proposed solution, so I fixed it and hope we can integrate this bugfix back into your repo.
@chescos
Copy link

chescos commented Dec 22, 2019

I'm experiencing the same issue (with v4).

@shalvah
Copy link
Contributor

shalvah commented Jan 5, 2020

Array query parameters are notoriously difficult to get right, primarily because there's no standard for dealing with them. Changing one thing usually breaks someone else's setup, so tread carefully.

I don't totally understand your problem or solution. Please explain them again, more clearly. Does your solution handle a situation where there are multiple items in the query param array? (e.g. filter[id], filter[name])?

@lucianobargmann
Copy link
Contributor

lucianobargmann commented Jan 5, 2020 via email

@shalvah
Copy link
Contributor

shalvah commented Jan 5, 2020

i still don't understand the problem....my understanding is that the generation process converts this into

  "filter" => [
    "id" => "...",
    "name" => "...",
  ]
]```

So what exactly is wrong?🤔

@chescos
Copy link

chescos commented Jan 5, 2020

@shalvah An exception is thrown when you try to generate the documentation. That's because the "example code" generation for bash (and a few other languages) breaks when it receives the array instead of a string.

@shalvah
Copy link
Contributor

shalvah commented Jan 5, 2020

I see. So, in essence, you want two changes:

    "id" => "...",
    "name" => "...",
  ]

should become: "filter[id]" => "...", "filter[name]" => "...",

And then the code for handling a "list" query param value ("filter[id]" => ["...", "..."]) in the template.

I understand that the second throws an exception, but why do you wish to change the first?

If we're doing this, we're going all in. No config switches.

@chescos
Copy link

chescos commented Jan 5, 2020

@shalvah I wasn't the guy who supplied a code fix, if I see it correctly that was @ncatanchin. I just experienced the issue, so I commented to say that it still exists in v4. Your solution looks right to me though, the code examples should just display it as a regular string instead of an array (e.g "filter[key]" => "value").

@tecbeast42
Copy link
Author

@shalvah Could you explain why this was added Mpociot\ApiDoc\Tools\Generator::generateConcreteSampleForArrayKeys?

In General if you insert params that are named filter[id] e.g.

     * @queryParam filter[id]               only session with id

i can not generate the docs anymore. I would have to override the templates to generate the docs.

The rease for that is in generateConcreteSampleForArrayKeys which will parse filter[id] and transform it into an array.

@shalvah
Copy link
Contributor

shalvah commented Mar 7, 2020

Fixed in #700

@shalvah shalvah closed this as completed Mar 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants