Skip to content

[Kotlin] [OkHttp4] detailed requests#10201

Merged
wing328 merged 5 commits intoOpenAPITools:masterfrom
wegendt-bosch:feature/10199-add-with-http-info
Nov 16, 2021
Merged

[Kotlin] [OkHttp4] detailed requests#10201
wing328 merged 5 commits intoOpenAPITools:masterfrom
wegendt-bosch:feature/10199-add-with-http-info

Conversation

@wegendt-bosch
Copy link
Copy Markdown
Contributor

@wegendt-bosch wegendt-bosch commented Aug 19, 2021

Extract from generated function per operation one that returns headers and other info. See #10199.
Example output:
before:

    fun addPet(body: Pet) : Unit {
        val localVariableConfig = addPetRequestConfig(body = body)

        val localVarResponse = request<Pet, Unit>(
            localVariableConfig
        )

        return when (localVarResponse.responseType) { /*...*/}}

new:

    fun addPet(body: Pet) : Unit {
        val localVarResponse = addPetWithHttpInfo(body = body)

        return when (localVarResponse.responseType) { /*...*/}}

    fun addPetWithHttpInfo(body: Pet) : ApiInfrastructureResponse<Unit?> {
        val localVariableConfig = addPetRequestConfig(body = body)

        return request<Pet, Unit>(
            localVariableConfig
        )
    }

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

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.
  • File the PR against the correct branch: master (5.3.0), 6.0.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.

@wegendt-bosch
Copy link
Copy Markdown
Contributor Author

I'm not sure how to interpret the failing AppVeyor build, can anyone advise me on whats wrong there? :)

@4brunu
Copy link
Copy Markdown
Contributor

4brunu commented Aug 19, 2021

Hey, first of all, thanks for the PR you created.

The http status code and the headers are already part of the responses.
Screenshot 2021-08-19 at 16 33 24

Are you having issues accessing the info in the responses?

@sheepdreamofandroids
Copy link
Copy Markdown
Contributor

I have the same appveyor problem in another kotlin related PR: #10176
I think the problem existed before since there seems to be no connection to kotlin templates

@wegendt-bosch
Copy link
Copy Markdown
Contributor Author

Yes, as far as I can tell you can't access that info from API functions, this PR generates additional API functions that do expose that information, as was previously the case with the OkHttp2 generator

@4brunu
Copy link
Copy Markdown
Contributor

4brunu commented Aug 20, 2021

Do you need them in case of error, or in case of success?

@wegendt-bosch
Copy link
Copy Markdown
Contributor Author

wegendt-bosch commented Aug 20, 2021

In case of success, in our case there is some info in the response's headers. However, getting general info about status codes, redirect, etc. is useful for debugging, too (as info in logs)

Copy link
Copy Markdown
Contributor

@4brunu 4brunu left a comment

Choose a reason for hiding this comment

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

I see the need for this.
I left some comments, that actually I'm not sure about what's the best approach.

fun addPetWithHttpInfo(body: Pet) : ApiInfrastructureResponse<Unit?> {
val localVariableConfig = addPetRequestConfig(body = body)

return request<Pet, Unit>(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'm not sure if here we should return the ApiInfrastructureResponse or if we should return only the success response, and throw the other errors, something like:

    @Suppress("UNCHECKED_CAST")
    @Throws(UnsupportedOperationException::class, ClientException::class, ServerException::class)
    fun findPetsByStatus(status: kotlin.collections.List<kotlin.String>) : kotlin.collections.List<Pet> {
        val localVarResponse = findPetsByStatusWithHttpInfo(status = status)

        return localVarResponse.data
    }

    @Suppress("UNCHECKED_CAST")
    @Throws(UnsupportedOperationException::class, ClientException::class, ServerException::class)
    fun findPetsByStatusWithHttpInfo(status: kotlin.collections.List<kotlin.String>) : Success<kotlin.collections.List<Pet>> {
        val localVariableConfig = findPetsByStatusRequestConfig(status = status)

        val localVarResponse = request<Unit, kotlin.collections.List<Pet>>(
            localVariableConfig
        )

        return when (localVarResponse.responseType) {
            ResponseType.Success -> localVarResponse
            ResponseType.Informational -> throw UnsupportedOperationException("Client does not support Informational responses.")
            ResponseType.Redirection -> throw UnsupportedOperationException("Client does not support Redirection responses.")
            ResponseType.ClientError -> {
                val localVarError = localVarResponse as ClientError<*>
                throw ClientException("Client error : ${localVarError.statusCode} ${localVarError.message.orEmpty()}", localVarError.statusCode, localVarResponse)
            }
            ResponseType.ServerError -> {
                val localVarError = localVarResponse as ServerError<*>
                throw ServerException("Server error : ${localVarError.statusCode} ${localVarError.message.orEmpty()}", localVarError.statusCode, localVarResponse)
            }
        }
    }

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I guess that works for my use case, but why not enable custom use cases by returning everything? In general I favour passing information unchanged if possible

Copy link
Copy Markdown
Contributor

@4brunu 4brunu Aug 22, 2021

Choose a reason for hiding this comment

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

@wing328 what do you think about this? Should we just return the raw response or do some king of error handling? What are the other generators doing in this case?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Maybe we should just keep the @wegendt-bosch implementation of returning the raw response because that's the more flexible approach?

* @throws ServerException If the API returns a server error response
*/
@Throws(UnsupportedOperationException::class, ClientException::class, ServerException::class)
fun addPetWithHttpInfo(body: Pet) : ApiInfrastructureResponse<Unit?> {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'm not also sure about the name withHttpInfo

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Detailed? I used WithHttpInfo since that's what it was in the past, but I'm fine with anything :)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please keep WithHttpInfo for the time being as that's what we've been using in other clients (e.g. C#, Ruby, etc)

@wing328
Copy link
Copy Markdown
Member

wing328 commented Sep 7, 2021

CI build failed with the following errors:

 /home/runner/work/openapi-generator/openapi-generator/samples/client/petstore/kotlin-jvm-okhttp4-coroutines/src/main/kotlin/org/openapitools/client/apis/UserApi.kt: (89, 9): 'return' is not allowed here
e: /home/runner/work/openapi-generator/openapi-generator/samples/client/petstore/kotlin-jvm-okhttp4-coroutines/src/main/kotlin/org/openapitools/client/apis/UserApi.kt: (152, 121): Suspend function 'withContext' should be called only from a coroutine or another suspend function
e: /home/runner/work/openapi-generator/openapi-generator/samples/client/petstore/kotlin-jvm-okhttp4-coroutines/src/main/kotlin/org/openapitools/client/apis/UserApi.kt: (155, 9): 'return' is not allowed here
e: /home/runner/work/openapi-generator/openapi-generator/samples/client/petstore/kotlin-jvm-okhttp4-coroutines/src/main/kotlin/org/openapitools/client/apis/UserApi.kt: (218, 120): Suspend function 'withContext' should be called only from a coroutine or another suspend function
e: /home/runner/work/openapi-generator/openapi-generator/samples/client/petstore/kotlin-jvm-okhttp4-coroutines/src/main/kotlin/org/openapitools/client/apis/UserApi.kt: (221, 9): 'return' is not allowed here
e: /home/runner/work/openapi-generator/openapi-generator/samples/client/petstore/kotlin-jvm-okhttp4-coroutines/src/main/kotlin/org/openapitools/client/apis/UserApi.kt: (284, 94): Suspend function 'withContext' should be called only from a coroutine or another suspend function
e: /home/runner/work/openapi-generator/openapi-generator/samples/client/petstore/kotlin-jvm-okhttp4-coroutines/src/main/kotlin/org/openapitools/client/apis/UserApi.kt: (287, 9): 'return' is not allowed here
e: /home/runner/work/openapi-generator/openapi-generator/samples/client/petstore/kotlin-jvm-okhttp4-coroutines/src/main/kotlin/org/openapitools/client/apis/UserApi.kt: (352, 97): Suspend function 'withContext' should be called only from a coroutine or another suspend function
e: /home/runner/work/openapi-generator/openapi-generator/samples/client/petstore/kotlin-jvm-okhttp4-coroutines/src/main/kotlin/org/openapitools/client/apis/UserApi.kt: (355, 9): 'return' is not allowed here
e: /home/runner/work/openapi-generator/openapi-generator/samples/client/petstore/kotlin-jvm-okhttp4-coroutines/src/main/kotlin/org/openapitools/client/apis/UserApi.kt: (422, 127): Suspend function 'withContext' should be called only from a coroutine or another suspend function
e: /home/runner/work/openapi-generator/openapi-generator/samples/client/petstore/kotlin-jvm-okhttp4-coroutines/src/main/kotlin/org/openapitools/client/apis/UserApi.kt: (425, 9): 'return' is not allowed here
e: /home/runner/work/openapi-generator/openapi-generator/samples/client/petstore/kotlin-jvm-okhttp4-coroutines/src/main/kotlin/org/openapitools/client/apis/UserApi.kt: (491, 71): Suspend function 'withContext' should be called only from a coroutine or another suspend function
e: /home/runner/work/openapi-generator/openapi-generator/samples/client/petstore/kotlin-jvm-okhttp4-coroutines/src/main/kotlin/org/openapitools/client/apis/UserApi.kt: (494, 9): 'return' is not allowed here
e: /home/runner/work/openapi-generator/openapi-generator/samples/client/petstore/kotlin-jvm-okhttp4-coroutines/src/main/kotlin/org/openapitools/client/apis/UserApi.kt: (558, 106): Suspend function 'withContext' should be called only from a coroutine or another suspend function
e: /home/runner/work/openapi-generator/openapi-generator/samples/client/petstore/kotlin-jvm-okhttp4-coroutines/src/main/kotlin/org/openapitools/client/apis/UserApi.kt: (561, 9): 'return' is not allowed here


> Task :compileKotlin FAILED
FAILURE: Build failed with an exception.

Can you please take a look when you've time?

ref: https://github.com/OpenAPITools/openapi-generator/pull/10201/checks?check_run_id=3530098034

@wegendt-bosch
Copy link
Copy Markdown
Contributor Author

should be fixed now, added suspend generation on same conditional as other function generators

@wing328
Copy link
Copy Markdown
Member

wing328 commented Sep 14, 2021

Now I see a different errors:

e: /home/runner/work/openapi-generator/openapi-generator/samples/client/petstore/kotlin-jvm-okhttp4-coroutines/src/main/kotlin/org/openapitools/client/apis/PetApi.kt: (90, 9): 'return' is not allowed here
> Task :compileKotlin
e: /home/runner/work/openapi-generator/openapi-generator/samples/client/petstore/kotlin-jvm-okhttp4-coroutines/src/main/kotlin/org/openapitools/client/apis/PetApi.kt: (158, 9): 'return' is not allowed here
e: /home/runner/work/openapi-generator/openapi-generator/samples/client/petstore/kotlin-jvm-okhttp4-coroutines/src/main/kotlin/org/openapitools/client/apis/PetApi.kt: (228, 9): 'return' is not allowed here
e: /home/runner/work/openapi-generator/openapi-generator/samples/client/petstore/kotlin-jvm-okhttp4-coroutines/src/main/kotlin/org/openapitools/client/apis/PetApi.kt: (303, 9): 'return' is not allowed here
e: /home/runner/work/openapi-generator/openapi-generator/samples/client/petstore/kotlin-jvm-okhttp4-coroutines/src/main/kotlin/org/openapitools/client/apis/PetApi.kt: (375, 9): 'return' is not allowed here
e: /home/runner/work/openapi-generator/openapi-generator/samples/client/petstore/kotlin-jvm-okhttp4-coroutines/src/main/kotlin/org/openapitools/client/apis/PetApi.kt: (441, 9): 'return' is not allowed here
e: /home/runner/work/openapi-generator/openapi-generator/samples/client/petstore/kotlin-jvm-okhttp4-coroutines/src/main/kotlin/org/openapitools/client/apis/PetApi.kt: (511, 9): 'return' is not allowed here
e: /home/runner/work/openapi-generator/openapi-generator/samples/client/petstore/kotlin-jvm-okhttp4-coroutines/src/main/kotlin/org/openapitools/client/apis/PetApi.kt: (585, 9): 'return' is not allowed here
e: /home/runner/work/openapi-generator/openapi-generator/samples/client/petstore/kotlin-jvm-okhttp4-coroutines/src/main/kotlin/org/openapitools/client/apis/StoreApi.kt: (89, 9): 'return' is not allowed here
e: /home/runner/work/openapi-generator/openapi-generator/samples/client/petstore/kotlin-jvm-okhttp4-coroutines/src/main/kotlin/org/openapitools/client/apis/StoreApi.kt: (155, 9): 'return' is not allowed here
e: /home/runner/work/openapi-generator/openapi-generator/samples/client/petstore/kotlin-jvm-okhttp4-coroutines/src/main/kotlin/org/openapitools/client/apis/StoreApi.kt: (222, 9): 'return' is not allowed here
e: /home/runner/work/openapi-generator/openapi-generator/samples/client/petstore/kotlin-jvm-okhttp4-coroutines/src/main/kotlin/org/openapitools/client/apis/StoreApi.kt: (290, 9): 'return' is not allowed here
e: /home/runner/work/openapi-generator/openapi-generator/samples/client/petstore/kotlin-jvm-okhttp4-coroutines/src/main/kotlin/org/openapitools/client/apis/UserApi.kt: (89, 9): 'return' is not allowed here
e: /home/runner/work/openapi-generator/openapi-generator/samples/client/petstore/kotlin-jvm-okhttp4-coroutines/src/main/kotlin/org/openapitools/client/apis/UserApi.kt: (155, 9): 'return' is not allowed here
e: /home/runner/work/openapi-generator/openapi-generator/samples/client/petstore/kotlin-jvm-okhttp4-coroutines/src/main/kotlin/org/openapitools/client/apis/UserApi.kt: (221, 9): 'return' is not allowed here
e: /home/runner/work/openapi-generator/openapi-generator/samples/client/petstore/kotlin-jvm-okhttp4-coroutines/src/main/kotlin/org/openapitools/client/apis/UserApi.kt: (287, 9): 'return' is not allowed here
e: /home/runner/work/openapi-generator/openapi-generator/samples/client/petstore/kotlin-jvm-okhttp4-coroutines/src/main/kotlin/org/openapitools/client/apis/UserApi.kt: (355, 9): 'return' is not allowed here
e: /home/runner/work/openapi-generator/openapi-generator/samples/client/petstore/kotlin-jvm-okhttp4-coroutines/src/main/kotlin/org/openapitools/client/apis/UserApi.kt: (425, 9): 'return' is not allowed here
e: /home/runner/work/openapi-generator/openapi-generator/samples/client/petstore/kotlin-jvm-okhttp4-coroutines/src/main/kotlin/org/openapitools/client/apis/UserApi.kt: (494, 9): 'return' is not allowed here
e: /home/runner/work/openapi-generator/openapi-generator/samples/client/petstore/kotlin-jvm-okhttp4-coroutines/src/main/kotlin/org/openapitools/client/apis/UserApi.kt: (561, 9): 'return' is not allowed here

Ref: https://github.com/OpenAPITools/openapi-generator/pull/10201/checks?check_run_id=3594505704

Please take a look when you've time.

{{/isDeprecated}}
val localVariableConfig = {{operationId}}RequestConfig({{#allParams}}{{{paramName}}} = {{{paramName}}}{{^-last}}, {{/-last}}{{/allParams}})

return request<{{#hasBodyParam}}{{#bodyParams}}{{{dataType}}}{{/bodyParams}}{{/hasBodyParam}}{{^hasBodyParam}}{{^hasFormParams}}Unit{{/hasFormParams}}{{#hasFormParams}}Map<String, Any?>{{/hasFormParams}}{{/hasBodyParam}}, {{{returnType}}}{{^returnType}}Unit{{/returnType}}>(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@wegendt-bosch I think the problem is here.
It should be return{{^doNotUseRxAndCoroutines}}{{#useCoroutines}}@withContext{{/useCoroutines}}{{/doNotUseRxAndCoroutines}} request<...

Copy link
Copy Markdown
Contributor

@4brunu 4brunu Oct 1, 2021

Choose a reason for hiding this comment

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

Hey, any news on this? 🙂

@wegendt-bosch
Copy link
Copy Markdown
Contributor Author

Sorry was on holiday, will get on it ASAP :)

@4brunu
Copy link
Copy Markdown
Contributor

4brunu commented Oct 4, 2021

Sorry, I didn't know about that, when you have time :)

@wegendt-bosch
Copy link
Copy Markdown
Contributor Author

It seems CI fails because of Haskell dependency HTTP errors? I'll push an empty commit later to retry

@wing328
Copy link
Copy Markdown
Member

wing328 commented Oct 4, 2021

@wegendt-bosch can you please merge the latest master into your branch to address the CI failure?

@wing328 wing328 modified the milestones: 5.3.0, 5.3.1 Oct 25, 2021
@4brunu
Copy link
Copy Markdown
Contributor

4brunu commented Nov 11, 2021

@wegendt-bosch Hi, did you got a chance to look in this? Thanks

@wegendt-bosch
Copy link
Copy Markdown
Contributor Author

Yeah I did that ages ago :)

@4brunu
Copy link
Copy Markdown
Contributor

4brunu commented Nov 15, 2021

Oh, sorry, I missed that.
Thanks, looks good to me 👍

{{/useCoroutines}}
{{/doNotUseRxAndCoroutines}}
import {{packageName}}.infrastructure.ApiClient
import {{packageName}}.infrastructure.ApiInfrastructureResponse
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'll rename ApiInfrastructureResponse to ApiResponse in another PR later to make it consistent with other clients (e.g. Java, C#, etc)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can you please create a typealias to make the transition easier?

@Deprecated("Renamed to ApiResponse", ReplaceWith("ApiResponse"))
typealias ApiInfrastructureResponse = ApiResponse

@wing328
Copy link
Copy Markdown
Member

wing328 commented Nov 16, 2021

kotlin tests (with updated samples) passed via #10869

@wing328 wing328 merged commit af2ca38 into OpenAPITools:master Nov 16, 2021
@wegendt-bosch
Copy link
Copy Markdown
Contributor Author

🎉

@wing328
Copy link
Copy Markdown
Member

wing328 commented Nov 16, 2021

@wegendt-bosch sorry for the delay. We missed it as there are too many PRs...

@wegendt-bosch
Copy link
Copy Markdown
Contributor Author

no problem :)

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.

4 participants