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

fix(kotlin): set content type on ktor-jvm library #15812

Conversation

bcmedeiros
Copy link
Contributor

@bcmedeiros bcmedeiros commented Jun 11, 2023

Draft for #15811

PR checklist

  • Read the contribution guidelines.
  • 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.
  • 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.
  • In case you are adding a new generator, run the following additional script :
    ./bin/utils/ensure-up-to-date
    
    Commit all changed files.
  • File the PR against the correct branch: master (6.3.0) (minor release - breaking changes with fallbacks), 7.0.x (breaking changes without fallbacks)
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

@bcmedeiros
Copy link
Contributor Author

bcmedeiros commented Jun 11, 2023

@wing328 This is the other issue I was referring to in #15793

generate-samples.sh is creating a lot of files, so I haven't "run" it, but I think there is something going on there. I tried to update just the templates affected by my change and everything seems all right.

@wing328
Copy link
Member

wing328 commented Jun 12, 2023

cc @jimschubert (2017/09) ❤️, @dr4ke616 (2018/08) @karismann (2019/03) @Zomzog (2019/04) @andrewemery (2019/10) @4brunu (2019/11) @yutaka0m (2020/03) @stefankoppier (2022/06)

@@ -196,6 +197,7 @@ import {{packageName}}.auth.*
this.method = requestConfig.method.httpMethod
headers.filter { header -> !UNSAFE_HEADERS.contains(header.key) }.forEach { header -> this.header(header.key, header.value) }
if (requestConfig.method in listOf(RequestMethod.PUT, RequestMethod.POST, RequestMethod.PATCH)) {
contentType(ContentType.Application.Json)
Copy link
Member

@wing328 wing328 Jun 12, 2023

Choose a reason for hiding this comment

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

thanks for the PR.

instead of hard coding to applicaiton/json, what about updating api.mustache to use content-type defined in the sepc instead similar to what we've done in https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/kotlin-client/libraries/jvm-okhttp/api.mustache#L222 ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@wing328 My first try was:

contentType(ContentType.parse("{{contentType}}"))

I also just tried:

contentType(ContentType.parse("{{{mediaType}}}"))

Both result in an empty value:

contentType(ContentType.parse(""))

Also, even if I get this correctly, I'm pretty sure we would need some conditionals to add the correct gradle dependencies in the template.

At this point I know very little about Mustache and the project layout overall.

Copy link
Member

Choose a reason for hiding this comment

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

can you please share your spec? or a minimal spec to reproduce the issue?

My first try was:

did you do it in api.mustache inside the operation mustache tag?

I would suggest plotting the fix in https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/kotlin-client/libraries/jvm-okhttp/api.mustache#L222 to the api.mustache for ktor-jvm library.

Copy link
Contributor Author

@bcmedeiros bcmedeiros Jun 12, 2023

Choose a reason for hiding this comment

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

did you do it in api.mustache inside the operation mustache tag?

I see I need to be in the scope of the operation now, but anyway, I cannot set the content type there due to the current design of the operation and the jsonRequest and request abstractions in ApiClient.

The current fix is done in a method called request, whose only caller is jsonRequest, so I highly doubt that "making this right" and set the dynamic Content Type will actually work.

I'll try a little bit longer, though.

Copy link
Member

Choose a reason for hiding this comment

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

You can use this fix with customized template (e.g. -t option CLI) if you need this fix urgently for your use case.

Copy link
Member

Choose a reason for hiding this comment

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

If you look at https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/kotlin-client/libraries/jvm-okhttp/api.mustache#L222, the content-type header is set there instead of ApiClient.

can we not do the same? or you saying jvm-okhttp has the issue for your use case?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@wing328 not without a major redesign. We need to do it inside the client.request { block, which the current design chose to abstract in the ApiClient.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@wing328 is it time for a big redesign? should I give it a try?

Copy link
Member

Choose a reason for hiding this comment

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

I don't think I fully understand the problem/limitation that requires the redesign.

Can you please PM me via Slack to further discuss this when you've time?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think I fully understand the problem/limitation that requires the redesign.

It's not easy to understand the problem without looking at the code. Basically, the content type is being set in a super class method that has no access to the config, IIRC.

Can you please PM me via Slack to further discuss this when you've time?

The issue ended up being fixed by #16843, so I'll close this PR.

That solution ended up getting the content type from the header, or a hardcoded value as a fallback, BTW, so we still haven't "fixed" this properly as you suggested.

@bcmedeiros
Copy link
Contributor Author

As explained in the comment above, a similar fix was merged as part of #16843

@bcmedeiros bcmedeiros closed this Mar 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants