openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

[#12887] typescript-axios: object as query parameter resolved

Open shubham-lakhiwal opened this issue 3 years ago • 5 comments

This pr is a solution to an open issue https://github.com/OpenAPITools/openapi-generator/issues/12887. With nested object, the leaf key-value pairs are taken and parent key is ignored. For ex: { pagenation: { size: 10, page: 1, }, query: "searchText" }

will be sent as url?query=searchText&page=1&size=10

@mkusaka @davidgamero kindly review the changes if they are at the right place. Thanks!

PR checklist

  • [x] Read the contribution guidelines.
  • [x] Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • [x] Run the following to build the project and update samples:
    ./mvnw clean package 
    ./bin/generate-samples.sh
    ./bin/utils/export_docs_generators.sh
    
    Commit all changed files. This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master. These must match the expectations made by your contribution. You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*. For Windows users, please run the script in Git BASH.
  • [x] File the PR against the correct branch: master (6.1.0) (minor release - breaking changes with fallbacks), 7.0.x (breaking changes without fallbacks)
  • [x] If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

shubham-lakhiwal avatar Aug 01 '22 12:08 shubham-lakhiwal

cc @TiFu (2017/07) @taxpon (2017/07) @sebastianhaas (2017/07) @kenisteward (2017/07) @Vrolijkx (2017/09) @macjohnny (2018/01) @topce (2018/10) @akehir (2019/07) @petejohansonxo (2019/11) @amakhrov (2020/02) @davidgamero (2022/03) @mkusaka (2022/04)

wing328 avatar Aug 03 '22 16:08 wing328

IIRC the current behavior was intentional initially and now we cant change this without breaking backwards compatibility

maybe we can introduce a flag if this new behavior is important

macjohnny avatar Aug 03 '22 16:08 macjohnny

@macjohnny Thanks for pointing it out. I have updated the code to support backward compatibility.

setSearchParams( url, { stand: 123 }, { sit: 654 }, { sit: [{ flip: 123, stand: 456 }] }, { sit: [1, 2, 3, 4] } )

will produce below params

?stand=123&sit=654&flip=123&stand=456&sit=1&sit=2&sit=3&sit=4

Here is a link to my code sandbox to try out different cases.

shubham-lakhiwal avatar Aug 04 '22 12:08 shubham-lakhiwal

thanks for updating the PR can you give a small example what problem it is now trying to solve? i.e. what is the input schema, resulting code before and after?

macjohnny avatar Aug 04 '22 12:08 macjohnny

@macjohnny below is the brief description:

In our index.yml we have an API path as below: Screenshot 2022-08-04 at 16 54 48

Parameter pageable has below schema Screenshot 2022-08-04 at 16 56 06

so during the api call, setSearchParams function was hit with params setSearchParams( url, {pageable: {size:10, page: 2}}), which was eventually making an api call as /cool/api/?pageable=%5Bobject+Object%5D. Clearly, this does not have the desired parameters as our actual parameters are lost.

With this solution, the API is hit as /cool/api/?size=10&page=2, which has the desired parameters.

shubham-lakhiwal avatar Aug 04 '22 13:08 shubham-lakhiwal

@shubham-lakhiwal can you please re-generate the samples?

macjohnny avatar Aug 10 '22 08:08 macjohnny

@macjohnny thanks for reviewing it. Sorry for not converting into draft.

shubham-lakhiwal avatar Aug 10 '22 08:08 shubham-lakhiwal

@macjohnny I am sorry to bring this up late. A small bug was left behind in this pull request by the last commit. There is another pull request for the fix. Kindly review. https://github.com/OpenAPITools/openapi-generator/pull/13149

shubham-lakhiwal avatar Aug 10 '22 18:08 shubham-lakhiwal