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

feat: [TSI-2083] enable format_options argument for java-client #426

Merged
merged 11 commits into from
Oct 30, 2023

Conversation

hahmed-dev
Copy link
Contributor

@hahmed-dev hahmed-dev commented Oct 12, 2023

Assumptions

Some assumptions made while working on the PR:

  • format_options argument will be key-value based map with values mostly being of primitive types
  • Initially explored the possibility of using params with style deepObject and explode: true option as suggested here as a fix but subsquently wasn't able to find appropriate mustache directive to determine if the param has any of these attributes (deepObject, explode: true) which led to adding a fix by checking if input is of the type Map and if yes, manually convert it to key value based Pair objects.

Problem Statement

Phrase API accepts format_options argument on locales download endpoint.

For java-client the format_option argument was not being properly parsed and embedded in the query params resulting in a request url like below

https://api.phrase.com/v2/projects/e4c0450ccc8dcc087fac8351ecc89eb6/locales/01877decc84124a75fdd0a4fc147d8de/download?file_format=properties&tags=menu&format_options=%7B%22omit_separator_space%22%3Atrue%7D&encoding=UTF-8

Solution

This pull requests adds the ability to parse any query params of type map and embed them to the URL's query string as proper key value pairs resulting in request URLs like:

localhost:3000/api/v2/projects/:project_id/locales/:id/download?file_format=i18next_4&format_options%5Bnesting%5D=false

@hahmed-dev hahmed-dev changed the title Tsi 2083 java format feat: [TSI-2083] enable format_options argument for java-client Oct 12, 2023
@hahmed-dev hahmed-dev marked this pull request as ready for review October 12, 2023 16:12
@@ -100,7 +100,12 @@ public class {{classname}} {
{{javaUtilPrefix}}List<Pair> localVarCollectionQueryParams = new {{javaUtilPrefix}}ArrayList<Pair>();
{{#queryParams}}
if ({{paramName}} != null) {
{{#isMap}}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe you should use #isDeepObject instead. You can see it's available to the API template if you run generate with --global-property debugOperations=true (also isExplode is available)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, will take a look, the document it printed out for me upon running with this argument,

  • niether included any documentations for get end points
  • nor isDeepObject or isExplode properties

But let me check again, would be more specific

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or, you can pass {{style}} as a parameter to mappedParameterToPairs, and then handle it appropriately (leaving other styles not covered until we need them).

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note that there are different debug... flags possible to pass as global-property. https://openapi-generator.tech/docs/debugging/#templates

Copy link
Contributor Author

@hahmed-dev hahmed-dev Oct 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will check this. Thanks for sharing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Mladen, using debugOperations was able to find the name of the directive.

public List<Pair> mappedParameterToPairs(String name, Object parameter){
List<Pair> params = new ArrayList<Pair>();

if(parameter instanceof Map){
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there's additional problem that we'll have to address at some point: these objects can be nested. so, it should be possible to generate a parameter such as formatOptions[customMetadataColumns][order]=B and so on 😄 Perhaps we could address that in a separate ticket

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thats true, i'll check if its something that can be addressed already within this PR otherwise we can do it separately :)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

..and arrays :D

Copy link
Contributor Author

@hahmed-dev hahmed-dev Oct 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess in case if a value against format_option is an array, it should already be handled? If not I will check and see if that can be addressed as well. I think from just a look maybe keynames need to be constructed in a nested way.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems it covers the basic case, but I guess arrays can also be nested 😬 never mind, we can cross that bridge when we get there

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also go will probably have the same issue with customMetadataColumns https://github.com/phrase/phrase-go/blob/master/api_locales.go#L436

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was already looking into nested arguments, that part is addressed with the latest commit. Regarding values being arrays/nested array, can see if it can be handled separately.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@theSoenke by same issue do you mean, it will require same patch like this?? For map based query params?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hahmed-dev format_options should already work. But for more deeply nested params it will need a similar fix I think

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@theSoenke I think deep nesting is an open feature request in the open api spec. If thats the case we may have to find a work around whenever we encounter something like this.

Copy link
Collaborator

@jablan jablan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice!

@@ -168,7 +173,8 @@ public void localeDownloadTest() throws ApiException, IOException, InterruptedEx
Assert.assertEquals("Correct file contents", fileContents, body);

RecordedRequest recordedRequest = mockBackend.takeRequest();
Assert.assertEquals("Request path", "//projects/MY_PROJECT_ID/locales/MY_ID/download?format_options%5Bomit_separator_space%5D=true&format_options%5Bfallback_language%5D=en", recordedRequest.getPath());
// for some reason with deep nested query params, ordering of query params change
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rails implementation simply sorts the params by name at end. :)

@docstun
Copy link
Member

docstun commented Oct 30, 2023

@hahmed-dev is this still valid?

@hahmed-dev
Copy link
Contributor Author

@docstun yes, we initially were waiting to merge the open api version bump PR since its based out of that, but we discussed last week, there is no dependency and we can merge this independently as well. I will try and shortly open a new PR and if that gets merged close this one.

Base automatically changed from tsi-1874-update-generator-v7 to master October 30, 2023 14:55
@hahmed-dev hahmed-dev merged commit faa8cb3 into master Oct 30, 2023
10 checks passed
@hahmed-dev hahmed-dev deleted the tsi-2083-java-format branch October 30, 2023 17:01
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

Successfully merging this pull request may close these issues.

4 participants