[Scala][sttp] various improvements (#5475)

* various improvements to scala sttp

* update ScalaSttpClientCodegen.java

* add windows batch file

* test scala sttp in jdk8

* fix tempalte directory
This commit is contained in:
William Cheng 2020-02-29 00:34:46 +08:00 committed by GitHub
parent 84250973be
commit 4603061c17
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
44 changed files with 78 additions and 111 deletions

View File

@ -64,7 +64,7 @@ OpenAPI Generator allows generation of API client libraries (SDK generation), se
| | Languages/Frameworks | | | Languages/Frameworks |
|-|-| |-|-|
**API clients** | **ActionScript**, **Ada**, **Apex**, **Bash**, **C**, **C#** (.net 2.0, 3.5 or later, .NET Standard 1.3 - 2.0, .NET Core 2.0), **C++** (cpp-restsdk, Qt5, Tizen), **Clojure**, **Dart (1.x, 2.x)**, **Elixir**, **Elm**, **Eiffel**, **Erlang**, **Go**, **Groovy**, **Haskell** (http-client, Servant), **Java** (Jersey1.x, Jersey2.x, OkHttp, Retrofit1.x, Retrofit2.x, Feign, RestTemplate, RESTEasy, Vertx, Google API Client Library for Java, Rest-assured, Spring 5 Web Client, MicroProfile Rest Client), **k6**, **Kotlin**, **Lua**, **Nim**, **Node.js/JavaScript** (ES5, ES6, AngularJS with Google Closure Compiler annotations, Flow types), **Objective-C**, **OCaml**, **Perl**, **PHP**, **PowerShell**, **Python**, **R**, **Ruby**, **Rust** (rust, rust-server), **Scala** (akka, http4s, scalaz, swagger-async-httpclient), **Swift** (2.x, 3.x, 4.x, 5.x), **Typescript** (AngularJS, Angular (2.x - 8.x), Aurelia, Axios, Fetch, Inversify, jQuery, Node, Rxjs) **API clients** | **ActionScript**, **Ada**, **Apex**, **Bash**, **C**, **C#** (.net 2.0, 3.5 or later, .NET Standard 1.3 - 2.0, .NET Core 2.0), **C++** (cpp-restsdk, Qt5, Tizen), **Clojure**, **Dart (1.x, 2.x)**, **Elixir**, **Elm**, **Eiffel**, **Erlang**, **Go**, **Groovy**, **Haskell** (http-client, Servant), **Java** (Jersey1.x, Jersey2.x, OkHttp, Retrofit1.x, Retrofit2.x, Feign, RestTemplate, RESTEasy, Vertx, Google API Client Library for Java, Rest-assured, Spring 5 Web Client, MicroProfile Rest Client), **k6**, **Kotlin**, **Lua**, **Nim**, **Node.js/JavaScript** (ES5, ES6, AngularJS with Google Closure Compiler annotations, Flow types), **Objective-C**, **OCaml**, **Perl**, **PHP**, **PowerShell**, **Python**, **R**, **Ruby**, **Rust** (rust, rust-server), **Scala** (akka, http4s, scalaz, sttp, swagger-async-httpclient), **Swift** (2.x, 3.x, 4.x, 5.x), **Typescript** (AngularJS, Angular (2.x - 8.x), Aurelia, Axios, Fetch, Inversify, jQuery, Node, Rxjs)
**Server stubs** | **Ada**, **C#** (ASP.NET Core, NancyFx), **C++** (Pistache, Restbed, Qt5 QHTTPEngine), **Erlang**, **F#** (Giraffe), **Go** (net/http, Gin), **Haskell** (Servant), **Java** (MSF4J, Spring, Undertow, JAX-RS: CDI, CXF, Inflector, Jersey, RestEasy, Play Framework, [PKMST](https://github.com/ProKarma-Inc/pkmst-getting-started-examples), [Vert.x](https://vertx.io/)), **Kotlin** (Spring Boot, Ktor, Vertx), **PHP** (Laravel, Lumen, Slim, Silex, [Symfony](https://symfony.com/), [Zend Expressive](https://github.com/zendframework/zend-expressive)), **Python** (Flask), **NodeJS**, **Ruby** (Sinatra, Rails5), **Rust** (rust-server), **Scala** ([Finch](https://github.com/finagle/finch), [Lagom](https://github.com/lagom/lagom), [Play](https://www.playframework.com/), Scalatra) **Server stubs** | **Ada**, **C#** (ASP.NET Core, NancyFx), **C++** (Pistache, Restbed, Qt5 QHTTPEngine), **Erlang**, **F#** (Giraffe), **Go** (net/http, Gin), **Haskell** (Servant), **Java** (MSF4J, Spring, Undertow, JAX-RS: CDI, CXF, Inflector, Jersey, RestEasy, Play Framework, [PKMST](https://github.com/ProKarma-Inc/pkmst-getting-started-examples), [Vert.x](https://vertx.io/)), **Kotlin** (Spring Boot, Ktor, Vertx), **PHP** (Laravel, Lumen, Slim, Silex, [Symfony](https://symfony.com/), [Zend Expressive](https://github.com/zendframework/zend-expressive)), **Python** (Flask), **NodeJS**, **Ruby** (Sinatra, Rails5), **Rust** (rust-server), **Scala** ([Finch](https://github.com/finagle/finch), [Lagom](https://github.com/lagom/lagom), [Play](https://www.playframework.com/), Scalatra)
**API documentation generators** | **HTML**, **Confluence Wiki**, **Asciidoc** **API documentation generators** | **HTML**, **Confluence Wiki**, **Asciidoc**
**Configuration files** | [**Apache2**](https://httpd.apache.org/) **Configuration files** | [**Apache2**](https://httpd.apache.org/)
@ -814,6 +814,7 @@ Here is a list of template creators:
* Rust (rust-server): @metaswitch * Rust (rust-server): @metaswitch
* Scala (scalaz & http4s): @tbrown1979 * Scala (scalaz & http4s): @tbrown1979
* Scala (Akka): @cchafer * Scala (Akka): @cchafer
* Scala (sttp): @chameleon82
* Swift: @tkqubo * Swift: @tkqubo
* Swift 3: @hexelon * Swift 3: @hexelon
* Swift 4: @ehyche * Swift 4: @ehyche

2
bin/openapi3/scala-sttp-petstore.sh Normal file → Executable file
View File

@ -27,6 +27,6 @@ fi
# if you've executed sbt assembly previously it will use that instead. # if you've executed sbt assembly previously it will use that instead.
export JAVA_OPTS="${JAVA_OPTS} -Xmx1024M -DloggerPath=conf/log4j.properties" export JAVA_OPTS="${JAVA_OPTS} -Xmx1024M -DloggerPath=conf/log4j.properties"
ags="generate --artifact-id "scala-sttp-petstore-client" -t modules/openapi-generator/src/main/resources/scala-sttp-client -i modules/openapi-generator/src/test/resources/3_0/petstore.yaml -g scala-sttp -o samples/client/petstore/scala-sttp $@" ags="generate --artifact-id 'scala-sttp-petstore' -t modules/openapi-generator/src/main/resources/scala-sttp -i modules/openapi-generator/src/test/resources/3_0/petstore.yaml -g scala-sttp -o samples/openapi3/client/petstore/scala-sttp $@"
java $JAVA_OPTS -jar $executable $ags java $JAVA_OPTS -jar $executable $ags

View File

@ -0,0 +1,10 @@
set executable=.\modules\openapi-generator-cli\target\openapi-generator-cli.jar
If Not Exist %executable% (
mvn clean package
)
REM set JAVA_OPTS=%JAVA_OPTS% -Xmx1024M -DloggerPath=conf/log4j.properties
set ags=generate --artifact-id "scala-sttp-petstore" -t modules\openapi-generator\src\main\resources\scala-sttp -i modules\openapi-generator\src\test\resources\3_0\petstore.yaml -g scala-sttp -o samples\openapi3\client\petstore\scala-sttp
java %JAVA_OPTS% -jar %executable% %ags%

View File

@ -54,7 +54,7 @@ The following generators are available:
* [scala-akka](generators/scala-akka.md) * [scala-akka](generators/scala-akka.md)
* [scala-gatling](generators/scala-gatling.md) * [scala-gatling](generators/scala-gatling.md)
* [scala-httpclient-deprecated (deprecated)](generators/scala-httpclient-deprecated.md) * [scala-httpclient-deprecated (deprecated)](generators/scala-httpclient-deprecated.md)
* [scala-sttp](generators/scala-sttp.md) * [scala-sttp (beta)](generators/scala-sttp.md)
* [scalaz](generators/scalaz.md) * [scalaz](generators/scalaz.md)
* [swift2-deprecated (deprecated)](generators/swift2-deprecated.md) * [swift2-deprecated (deprecated)](generators/swift2-deprecated.md)
* [swift3-deprecated (deprecated)](generators/swift3-deprecated.md) * [swift3-deprecated (deprecated)](generators/swift3-deprecated.md)

View File

@ -1,3 +1,19 @@
/*
* Copyright 2018 OpenAPI-Generator Contributors (https://openapi-generator.tech)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openapitools.codegen.languages; package org.openapitools.codegen.languages;
import io.swagger.v3.oas.models.Operation; import io.swagger.v3.oas.models.Operation;
@ -5,6 +21,8 @@ import io.swagger.v3.oas.models.servers.Server;
import org.openapitools.codegen.CodegenConfig; import org.openapitools.codegen.CodegenConfig;
import org.openapitools.codegen.CodegenOperation; import org.openapitools.codegen.CodegenOperation;
import org.openapitools.codegen.SupportingFile; import org.openapitools.codegen.SupportingFile;
import org.openapitools.codegen.meta.GeneratorMetadata;
import org.openapitools.codegen.meta.Stability;
import java.io.File; import java.io.File;
import java.util.List; import java.util.List;
@ -14,8 +32,13 @@ public class ScalaSttpClientCodegen extends ScalaAkkaClientCodegen implements Co
public ScalaSttpClientCodegen() { public ScalaSttpClientCodegen() {
super(); super();
} generatorMetadata = GeneratorMetadata.newBuilder(generatorMetadata)
.stability(Stability.BETA)
.build();
embeddedTemplateDir = templateDir = "scala-sttp";
outputFolder = "generated-code/scala-sttp";
}
@Override @Override
public void processOpts() { public void processOpts() {
@ -52,13 +75,13 @@ public class ScalaSttpClientCodegen extends ScalaAkkaClientCodegen implements Co
@Override @Override
public String getHelp() { public String getHelp() {
return "Generates a Scala client library base on Sttp."; return "Generates a Scala client library (beta) based on Sttp.";
} }
@Override @Override
public String encodePath(String input) { public String encodePath(String input) {
String result = super.encodePath(input); String result = super.encodePath(input);
return result.replace("{","${"); return result.replace("{", "${");
} }
@Override @Override

View File

@ -1291,6 +1291,7 @@
<module>samples/openapi3/client/petstore/go</module> <module>samples/openapi3/client/petstore/go</module>
<!-- test java-related projects --> <!-- test java-related projects -->
<!--<module>samples/client/petstore/scala-akka</module>--> <!--<module>samples/client/petstore/scala-akka</module>-->
<module>samples/openapi3/client/petstore/scala-sttp</module>
<module>samples/client/petstore/scala-httpclient</module> <module>samples/client/petstore/scala-httpclient</module>
<module>samples/client/petstore/scalaz</module> <module>samples/client/petstore/scalaz</module>
<module>samples/client/petstore/clojure</module> <module>samples/client/petstore/clojure</module>

View File

@ -1 +0,0 @@
sbt.version=1.2.8

View File

@ -1,99 +0,0 @@
import org.junit.runner.RunWith
import org.openapitools.client.api._
import org.openapitools.client.core.{ApiInvoker, ApiKeyValue, SttpSerializer}
import org.openapitools.client.model._
import org.scalatest.Inspectors._
import org.scalatest._
import org.scalatest.junit.JUnitRunner
import sttp.client.{HttpURLConnectionBackend, Identity, NothingT, SttpBackend}
@RunWith(classOf[JUnitRunner])
class PetApiTest extends AsyncFlatSpec with Matchers {
implicit val sttpSerializer: SttpSerializer = new SttpSerializer
implicit val backend: SttpBackend[Identity, Nothing, NothingT] = HttpURLConnectionBackend()
val api = new PetApi("https://petstore3.swagger.io/api/v3")
implicit val apiKey: ApiKeyValue = ApiKeyValue("api-key")
import ApiInvoker._
behavior of "PetApi"
it should "add and fetch a pet" in {
val petId = 1000
val createdPet = Pet(
Some(petId),
Some(Category(Some(1), Some("sold"))),
"dragon",
(for (i <- 1 to 10) yield "http://foo.com/photo/" + i).toList,
Some((for (i <- 1 to 5) yield org.openapitools.client.model.Tag(Some(i), Some("tag-" + i))).toList),
Some(PetEnums.Status.Sold)
)
val addPetRequest = api.addPet(createdPet)
val getPetRequest = api.getPetById(petId)
addPetRequest.result
val pet = getPetRequest.result
pet should have(
'id(createdPet.id),
'status(createdPet.status),
'category(createdPet.category),
'name(createdPet.name)
)
pet.tags should not be empty
pet.tags.get should contain theSameElementsInOrderAs createdPet.tags.get
pet.photoUrls should contain theSameElementsInOrderAs createdPet.photoUrls
}
it should "update a pet" in {
val petId = (Math.random() * 1000000000).toLong
val createdPetObj = Pet(
Some(petId),
Some(Category(Some(1), Some("sold"))),
"programmer",
(for (i <- 1 to 10) yield "http://foo.com/photo/" + i).toList,
Some((for (i <- 1 to 5) yield org.openapitools.client.model.Tag(Some(i), Some("tag-" + i))).toList),
Some(PetEnums.Status.Available)
)
val createdPet = api.addPet(createdPetObj).result
val pet = api.getPetById(createdPet.id.get).result
val updatedPetObj = pet.copy(status = Some(PetEnums.Status.Sold), name = "developer")
val updatedPet = api.updatePet(updatedPetObj).result
val updatedRequested = api.getPetById(createdPet.id.get).result
pet.name should be("programmer")
pet.status should be(Some(PetEnums.Status.Available))
updatedPet.name should be("developer")
updatedPet.status should be(Some(PetEnums.Status.Sold))
updatedRequested.name should be("developer")
updatedRequested.status should be(Some(PetEnums.Status.Sold))
}
it should "find pets by status" in {
val pets = api.findPetsByStatus(List("available")).result
pets should not be empty
forAll(pets.toList) { pet =>
pet.status should contain(PetEnums.Status.Available)
}
}
it should "find pets by tag" in {
val pets = api.findPetsByTags(List("tag1", "tag2")).result
pets should not be empty
forAll(pets.toList) { pet =>
val tagNames = pet.tags.toList.flatten.map(_.name).collect { case Some(name) => name }
tagNames should contain atLeastOneOf("tag1", "tag2")
}
}
}

View File

@ -1,4 +1,4 @@
# scala-sttp-petstore-client # &#39;scala-sttp-petstore&#39;
OpenAPI Petstore OpenAPI Petstore
- API version: 1.0.0 - API version: 1.0.0
@ -37,7 +37,7 @@ Add this dependency to your project's POM:
```xml ```xml
<dependency> <dependency>
<groupId>org.openapitools</groupId> <groupId>org.openapitools</groupId>
<artifactId>scala-sttp-petstore-client</artifactId> <artifactId>'scala-sttp-petstore'</artifactId>
<version>1.0.0</version> <version>1.0.0</version>
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>
@ -48,13 +48,13 @@ Add this dependency to your project's POM:
Add this dependency to your project's build file: Add this dependency to your project's build file:
```groovy ```groovy
compile "org.openapitools:scala-sttp-petstore-client:1.0.0" compile "org.openapitools:'scala-sttp-petstore':1.0.0"
``` ```
### SBT users ### SBT users
```scala ```scala
libraryDependencies += "org.openapitools" % "scala-sttp-petstore-client" % "1.0.0" libraryDependencies += "org.openapitools" % "'scala-sttp-petstore'" % "1.0.0"
``` ```
## Getting Started ## Getting Started

View File

@ -1,5 +1,5 @@
version := "1.0.0" version := "1.0.0"
name := "scala-sttp-petstore-client" name := "&#39;scala-sttp-petstore&#39;"
organization := "org.openapitools" organization := "org.openapitools"
scalaVersion := "2.13.0" scalaVersion := "2.13.0"

View File

@ -0,0 +1,32 @@
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>org.openapitools</groupId>
<artifactId>scalaz-sttp-petstore-client</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<name>scala-sttp-petstore-client</name>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.5.0</version>
<executions>
<execution>
<id>sbt-test</id>
<phase>integration-test</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>sbt</executable>
<arguments>
<argument>test</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>