diff --git a/.github/workflows/samples-kotlin-echo-api.yaml b/.github/workflows/samples-kotlin-echo-api.yaml new file mode 100644 index 00000000000..ec7c27c549e --- /dev/null +++ b/.github/workflows/samples-kotlin-echo-api.yaml @@ -0,0 +1,50 @@ +name: Kotlin Client (Echo API) JDK17 + +on: + push: + paths: + - samples/client/echo_api/kotlin** + pull_request: + paths: + - samples/client/echo_api/kotlin** +env: + GRADLE_VERSION: 7.4 +jobs: + build: + name: Build Kotlin Client JDK17 + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + sample: + # clients + - samples/client/echo_api/kotlin-jvm-spring-3-restclient + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-java@v4 + with: + distribution: 'temurin' + java-version: 17 + - name: Cache maven dependencies + uses: actions/cache@v4 + env: + cache-name: maven-repository + with: + path: | + ~/.gradle + key: ${{ runner.os }}-${{ github.job }}-${{ env.cache-name }}-${{ hashFiles('**/pom.xml') }} + - name: Install Gradle wrapper + uses: eskatos/gradle-command-action@v3 + with: + gradle-version: ${{ env.GRADLE_VERSION }} + build-root-directory: ${{ matrix.sample }} + arguments: wrapper + - name: Setup node.js + uses: actions/setup-node@v4 + - name: Run echo server + run: | + git clone https://github.com/wing328/http-echo-server -b openapi-generator-test-server + (cd http-echo-server && npm install && npm start &) + - name: Build + working-directory: ${{ matrix.sample }} + run: ./gradlew build diff --git a/.gitignore b/.gitignore index 9f893291374..96d856cb7bb 100644 --- a/.gitignore +++ b/.gitignore @@ -212,6 +212,7 @@ samples/server/petstore/kotlin-springboot/build samples/client/petstore/kotlin*/src/main/kotlin/test/ samples/client/petstore/kotlin*/build/ samples/server/others/kotlin-server/jaxrs-spec/build/ +samples/client/echo_api/kotlin-jvm-spring-3-restclient/build/ # haskell .stack-work diff --git a/samples/client/echo_api/kotlin-jvm-spring-3-restclient/src/test/kotlin/org/openapitools/client/CustomTest.kt b/samples/client/echo_api/kotlin-jvm-spring-3-restclient/src/test/kotlin/org/openapitools/client/CustomTest.kt new file mode 100644 index 00000000000..6480884d9d6 --- /dev/null +++ b/samples/client/echo_api/kotlin-jvm-spring-3-restclient/src/test/kotlin/org/openapitools/client/CustomTest.kt @@ -0,0 +1,74 @@ +package org.openapitools.client + +import io.kotlintest.shouldBe +import io.kotlintest.specs.ShouldSpec +import org.openapitools.client.apis.BodyApi +import org.openapitools.client.apis.QueryApi +import org.openapitools.client.models.Category +import org.openapitools.client.models.Pet +import org.openapitools.client.models.Tag + +class PetApiTest : ShouldSpec() { + init { + val petId:Long = 10006 + val queryApi = QueryApi("http://localhost:3000") + val bodyApi = BodyApi("http://localhost:3000") + + should("return a pet") { + val pet : Pet = Pet( + id = petId, + name = "kotlin client test", + photoUrls = listOf("http://test_kotlin_unit_test.com"), + category = Category(petId, "test kotlin category"), + tags = listOf(Tag(petId, "test kotlin tag")) + ) + + /* comment out the following as for some reasons http request body contains + a line "cc" before the JSON payload, e.g. + + Echo mode switched on for this request +--> POST /echo/body/Pet HTTP/1.1 +--> Connection: Upgrade, HTTP2-Settings +--> Host: localhost:3000 +--> HTTP2-Settings: AAEAAEAAAAIAAAABAAMAAABkAAQBAAAAAAUAAEAA +--> Transfer-encoding: chunked +--> Upgrade: h2c +--> User-Agent: Java-http-client/17.0.10 +--> Accept: application/json +--> Content-Type: application/json +--> +--> +Found the content-type: application/json +Found the blank line before the response body +<-- +[socket#8] event: data +--> cc +--> {"name":"kotlin client test","photoUrls":["http://test_kotlin_unit_test.com"],"id":10006,"category":{"id":10006,"name":"test kotlin category"},"tags":[{"id":10006,"name":"test kotlin tag"}],"status":null} +--> +<-- cc +<-- {"name":"kotlin client test","photoUrls":["http://test_kotlin_unit_test.com"],"id":10006,"category":{"id":10006,"name":"test kotlin category"},"tags":[{"id":10006,"name":"test kotlin tag"}],"status":null} +<-- +[socket#8] event: data + + val result : Pet = bodyApi.testEchoBodyPet(pet) + + result.id shouldBe (petId) + result.name shouldBe ("kotlin client test") + result.photoUrls[0] shouldBe ("http://test_kotlin_unit_test.com") + result.category!!.id shouldBe (petId) + result.category!!.name shouldBe ("test kotlin category") + result.tags!![0].id shouldBe (petId) + result.tags!![0].name shouldBe ("test kotlin tag") + + */ + } + + should("test echo server response parser") { + val response: String = queryApi.testQueryStyleFormExplodeTrueArrayString() + val result = EchoServerResponseParser(response) + result.path shouldBe ("/query/style_form/explode_true/array_string") + result.method shouldBe ("GET") + result.body shouldBe ("") + } + } +} \ No newline at end of file diff --git a/samples/client/echo_api/kotlin-jvm-spring-3-restclient/src/test/kotlin/org/openapitools/client/EchoServerResponseParser.kt b/samples/client/echo_api/kotlin-jvm-spring-3-restclient/src/test/kotlin/org/openapitools/client/EchoServerResponseParser.kt new file mode 100644 index 00000000000..fe0981d0e01 --- /dev/null +++ b/samples/client/echo_api/kotlin-jvm-spring-3-restclient/src/test/kotlin/org/openapitools/client/EchoServerResponseParser.kt @@ -0,0 +1,44 @@ +package org.openapitools.client + +class EchoServerResponseParser(response: String) { + lateinit var method: String + lateinit var path: String + lateinit var protocol: String + val headers = hashMapOf() + val body: String + + init { + require(response.isNotEmpty()) { "Echo server response cannot be null" } + + val lines = response.lineSequence().iterator() + var firstLine = true + var bodyStart = false + val bodyBuilder = StringBuilder() + for (line in lines) { + if (firstLine) { + val items = line.split(" ") + method = items[0] + path = items[1] + protocol = items[2] + firstLine = false + continue + } + if (bodyStart) { + bodyBuilder.append(line) + bodyBuilder.append("\n") + } + if (line.isEmpty()) { + bodyStart = true + continue + } + + val keyValue = line.split(": ") + if (keyValue.size == 2) { + headers[keyValue[0]] = keyValue[1] + } + } + body = bodyBuilder.toString().trimEnd() + } +} + +