[scala] Fix Array[Byte] type for Openapi type string format byte (#10426)

* [scala] fix byteArray mapping in AbstractScala

* fix testng framework instead of junit for sttp codegen

* regenerate scala examples

* regenerate docs and examples

* fix test
This commit is contained in:
Aleksandr Nekrasov 2021-09-21 11:15:49 +07:00 committed by GitHub
parent 18301d0011
commit ef0186c9cf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
22 changed files with 363 additions and 33 deletions

View File

@ -61,6 +61,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
<li>Any</li>
<li>Array</li>
<li>Boolean</li>
<li>Byte</li>
<li>Double</li>
<li>Float</li>
<li>Int</li>

View File

@ -56,6 +56,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
<li>Any</li>
<li>Array</li>
<li>Boolean</li>
<li>Byte</li>
<li>Double</li>
<li>Float</li>
<li>Int</li>

View File

@ -56,6 +56,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
<li>Any</li>
<li>Array</li>
<li>Boolean</li>
<li>Byte</li>
<li>Double</li>
<li>Float</li>
<li>Int</li>

View File

@ -56,6 +56,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
<li>Any</li>
<li>Array</li>
<li>Boolean</li>
<li>Byte</li>
<li>Double</li>
<li>Float</li>
<li>Int</li>

View File

@ -56,6 +56,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
<li>Any</li>
<li>Array</li>
<li>Boolean</li>
<li>Byte</li>
<li>Double</li>
<li>Float</li>
<li>Int</li>

View File

@ -64,6 +64,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
<li>Any</li>
<li>Array</li>
<li>Boolean</li>
<li>Byte</li>
<li>Double</li>
<li>Float</li>
<li>Int</li>

View File

@ -62,6 +62,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
<li>Any</li>
<li>Array</li>
<li>Boolean</li>
<li>Byte</li>
<li>Double</li>
<li>Float</li>
<li>Int</li>

View File

@ -57,6 +57,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
<li>Any</li>
<li>Array</li>
<li>Boolean</li>
<li>Byte</li>
<li>Double</li>
<li>Float</li>
<li>Int</li>

View File

@ -56,6 +56,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
<li>Any</li>
<li>Array</li>
<li>Boolean</li>
<li>Byte</li>
<li>Double</li>
<li>Float</li>
<li>Int</li>

View File

@ -1580,6 +1580,7 @@ public class DefaultCodegen implements CodegenConfig {
typeMapping.put("char", "String");
typeMapping.put("object", "Object");
typeMapping.put("integer", "Integer");
// FIXME: java specific type should be in Java Based Abstract Impl's
typeMapping.put("ByteArray", "byte[]");
typeMapping.put("binary", "File");
typeMapping.put("file", "File");

View File

@ -73,7 +73,8 @@ public abstract class AbstractScalaCodegen extends DefaultCodegen {
"List",
"Seq",
"Map",
"Array"));
"Array",
"Byte"));
reservedWords.addAll(Arrays.asList(
"abstract",
@ -118,6 +119,10 @@ public abstract class AbstractScalaCodegen extends DefaultCodegen {
"yield"
));
// Scala specific openApi types mapping
typeMapping.put("ByteArray", "Array[Byte]");
importMapping = new HashMap<String, String>();
importMapping.put("ListBuffer", "scala.collection.mutable.ListBuffer");
// although Seq is a predef, before Scala 2.13, it _could_ refer to a mutable Seq in some cases.
@ -344,17 +349,11 @@ public abstract class AbstractScalaCodegen extends DefaultCodegen {
if (ModelUtils.isSet(p)) {
openAPIType = "set";
}
String type;
// don't apply renaming on types from the typeMapping
if (typeMapping.containsKey(openAPIType)) {
type = typeMapping.get(openAPIType);
if (languageSpecificPrimitives.contains(type)) {
return toModelName(type);
}
} else {
type = openAPIType;
return typeMapping.get(openAPIType);
}
return toModelName(type);
return toModelName(openAPIType);
}
@Override

View File

@ -125,6 +125,7 @@ public class ScalaSttpClientCodegen extends AbstractScalaCodegen implements Code
importMapping.remove("Set");
importMapping.remove("Map");
// TODO: there is no specific sttp mapping. All Scala Type mappings should be in AbstractScala
typeMapping = new HashMap<>();
typeMapping.put("array", "Seq");
typeMapping.put("set", "Set");
@ -144,7 +145,6 @@ public class ScalaSttpClientCodegen extends AbstractScalaCodegen implements Code
typeMapping.put("number", "Double");
typeMapping.put("decimal", "BigDecimal");
typeMapping.put("ByteArray", "Array[Byte]");
typeMapping.put("ArrayByte", "Array[Byte]");
instantiationTypes.put("array", "ListBuffer");
instantiationTypes.put("map", "Map");

View File

@ -1,5 +1,7 @@
package org.openapitools.codegen.scala;
import io.swagger.v3.oas.models.media.ObjectSchema;
import io.swagger.v3.oas.models.media.Schema;
import org.openapitools.codegen.CodegenConstants;
import org.openapitools.codegen.languages.AbstractScalaCodegen;
import org.testng.Assert;
@ -95,4 +97,21 @@ public class AbstractScalaCodegenTest {
Assert.assertFalse(fakeScalaCodegen.importMapping().containsKey("BigInt"),
"BigInt is a Scala type and must not be imported");
}
@Test
void checkScalaTypeDeclaration() {
final AbstractScalaCodegen codegen = new P_AbstractScalaCodegen();
Schema<?> byteArraySchema = new ObjectSchema();
byteArraySchema.setType("string");
byteArraySchema.setFormat("byte");
byteArraySchema.setDescription("Schema with byte string");
Assert.assertEquals(codegen.getTypeDeclaration(byteArraySchema), "Array[Byte]",
"OpenApi File type represented as byte string should be represented as Array[Byte] scala type");
}
}

View File

@ -1,7 +1,7 @@
package org.openapitools.codegen.scala;
import org.junit.Assert;
import org.junit.Test;
import org.testng.Assert;
import org.testng.annotations.Test;
import org.openapitools.codegen.languages.ScalaSttpClientCodegen;
import java.util.HashMap;
@ -14,7 +14,7 @@ public class SttpBooleanPropertyTest {
Map<String, Object> additionalProperties = new HashMap<>();
booleanProperty.updateAdditionalProperties(additionalProperties);
Assert.assertEquals(false, additionalProperties.get("k1"));
Assert.assertEquals(additionalProperties.get("k1"), false);
}
@Test
@ -24,6 +24,6 @@ public class SttpBooleanPropertyTest {
additionalProperties.put("k1", true);
booleanProperty.updateAdditionalProperties(additionalProperties);
Assert.assertEquals(true, additionalProperties.get("k1"));
Assert.assertEquals(additionalProperties.get("k1"), true);
}
}

View File

@ -1,8 +1,8 @@
package org.openapitools.codegen.scala;
import org.openapitools.codegen.languages.ScalaSttpClientCodegen;
import org.junit.Assert;
import org.junit.Test;
import org.testng.Assert;
import org.testng.annotations.Test;
import io.swagger.v3.oas.models.media.Schema;

View File

@ -1,7 +1,7 @@
package org.openapitools.codegen.scala;
import org.junit.Assert;
import org.junit.Test;
import org.testng.Assert;
import org.testng.annotations.Test;
import org.openapitools.codegen.languages.ScalaSttpClientCodegen;
import java.util.HashMap;
@ -13,8 +13,8 @@ public class SttpJsonLibraryPropertyTest {
ScalaSttpClientCodegen.JsonLibraryProperty property = new ScalaSttpClientCodegen.JsonLibraryProperty();
Map<String, Object> additionalProperties = new HashMap<>();
property.updateAdditionalProperties(additionalProperties);
Assert.assertEquals(true, additionalProperties.get("json4s"));
Assert.assertEquals(false, additionalProperties.get("circe"));
Assert.assertEquals(additionalProperties.get("json4s"), true);
Assert.assertEquals(additionalProperties.get("circe"), false);
}
@Test
@ -23,8 +23,8 @@ public class SttpJsonLibraryPropertyTest {
Map<String, Object> additionalProperties = new HashMap<>();
additionalProperties.put("jsonLibrary", "json4s");
property.updateAdditionalProperties(additionalProperties);
Assert.assertEquals(true, additionalProperties.get("json4s"));
Assert.assertEquals(false, additionalProperties.get("circe"));
Assert.assertEquals(additionalProperties.get("json4s"), true);
Assert.assertEquals(additionalProperties.get("circe"), false);
}
@Test
@ -33,7 +33,7 @@ public class SttpJsonLibraryPropertyTest {
Map<String, Object> additionalProperties = new HashMap<>();
additionalProperties.put("jsonLibrary", "circe");
property.updateAdditionalProperties(additionalProperties);
Assert.assertEquals(false, additionalProperties.get("json4s"));
Assert.assertEquals(true, additionalProperties.get("circe"));
Assert.assertEquals(additionalProperties.get("json4s"), false);
Assert.assertEquals(additionalProperties.get("circe"), true);
}
}

View File

@ -1,7 +1,7 @@
package org.openapitools.codegen.scala;
import org.junit.Assert;
import org.junit.Test;
import org.testng.Assert;
import org.testng.annotations.Test;
import org.openapitools.codegen.CodegenConstants;
import org.openapitools.codegen.languages.ScalaSttpClientCodegen;

View File

@ -1,7 +1,7 @@
package org.openapitools.codegen.scala;
import org.junit.Assert;
import org.junit.Test;
import org.testng.Assert;
import org.testng.annotations.Test;
import org.openapitools.codegen.languages.ScalaSttpClientCodegen;
import java.util.HashMap;
@ -10,21 +10,21 @@ import java.util.Map;
public class SttpStringPropertyTest {
@Test
public void shouldUseDefaultValueIfAdditionalPropertiesAreEmpty(){
public void shouldUseDefaultValueIfAdditionalPropertiesAreEmpty() {
ScalaSttpClientCodegen.StringProperty property = new ScalaSttpClientCodegen.StringProperty("k1", "desc", "default");
Map<String, Object> additionalProperties = new HashMap<>();
property.updateAdditionalProperties(additionalProperties);
Assert.assertEquals("default", additionalProperties.get("k1"));
Assert.assertEquals(additionalProperties.get("k1"), "default");
}
@Test
public void shouldUseGivenValueIfProvided(){
public void shouldUseGivenValueIfProvided() {
ScalaSttpClientCodegen.StringProperty property = new ScalaSttpClientCodegen.StringProperty("k1", "desc", "default");
Map<String, Object> additionalProperties = new HashMap<>();
additionalProperties.put("k1", "custom");
property.updateAdditionalProperties(additionalProperties);
Assert.assertEquals("custom", additionalProperties.get("k1"));
Assert.assertEquals(additionalProperties.get("k1"), "custom");
}
}

View File

@ -0,0 +1,237 @@
/**
* OpenAPI Petstore
* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.
*
* The version of the OpenAPI document: 1.0.0
*
*
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
* https://openapi-generator.tech
* Do not edit the class manually.
*/
package org.openapitools.example.invoker
import com.sun.jersey.api.client.Client
import com.sun.jersey.api.client.ClientResponse
import com.sun.jersey.api.client.config.ClientConfig
import com.sun.jersey.api.client.config.DefaultClientConfig
import com.sun.jersey.api.client.filter.LoggingFilter
import com.sun.jersey.core.util.MultivaluedMapImpl
import com.sun.jersey.multipart.FormDataMultiPart
import com.sun.jersey.multipart.file.FileDataBodyPart
import java.io.File
import java.net.URLEncoder
import java.util.UUID
import javax.ws.rs.core.MediaType
import scala.collection.JavaConverters._
import scala.collection.mutable
import com.fasterxml.jackson.module.scala.DefaultScalaModule
import com.fasterxml.jackson.datatype.joda.JodaModule
import com.fasterxml.jackson.core.JsonGenerator.Feature
import com.fasterxml.jackson.databind._
import com.fasterxml.jackson.annotation._
import com.fasterxml.jackson.databind.annotation.JsonSerialize
object ScalaJsonUtil {
def getJsonMapper: ObjectMapper = {
val mapper = new ObjectMapper()
mapper.registerModule(new DefaultScalaModule())
mapper.registerModule(new JodaModule())
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL)
mapper.setSerializationInclusion(JsonInclude.Include.NON_DEFAULT)
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false)
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY)
mapper
}
}
class ApiInvoker(val mapper: ObjectMapper = ScalaJsonUtil.getJsonMapper,
httpHeaders: mutable.HashMap[String, String] = mutable.HashMap(),
hostMap: mutable.HashMap[String, Client] = mutable.HashMap(),
asyncHttpClient: Boolean = false,
authScheme: String = "",
authPreemptive: Boolean = false
) {
var defaultHeaders: mutable.HashMap[String, String] = httpHeaders
def escape(value: String): String = {
URLEncoder.encode(value, "utf-8").replaceAll("\\+", "%20")
}
def escape(values: List[String]): String = {
values.map(escape).mkString(",")
}
def escape(value: Long): String = value.toString
def escape(value: Double): String = value.toString
def escape(value: Float): String = value.toString
def escape(value: UUID): String = value.toString
def deserialize(json: String, containerType: String, cls: Class[_]) = {
if (cls == classOf[String]) {
json match {
case s: String =>
if (s.startsWith("\"") && s.endsWith("\"") && s.length > 1) {
s.substring(1, s.length - 1)
} else {
s
}
case _ => null
}
} else {
containerType.toLowerCase match {
case "array" =>
val typeInfo = mapper.getTypeFactory.constructCollectionType(classOf[java.util.List[_]], cls)
val response = mapper.readValue(json, typeInfo).asInstanceOf[java.util.List[_]]
response.asScala.toList
case "list" =>
val typeInfo = mapper.getTypeFactory.constructCollectionType(classOf[java.util.List[_]], cls)
val response = mapper.readValue(json, typeInfo).asInstanceOf[java.util.List[_]]
response.asScala.toList
case _ =>
json match {
case e: String if "\"\"" == e => null
case _ => mapper.readValue(json, cls)
}
}
}
}
def serialize(obj: AnyRef): String = {
if (obj != null) {
obj match {
case e: List[_] => mapper.writeValueAsString(obj.asInstanceOf[List[_]].asJava)
case _ => mapper.writeValueAsString(obj)
}
} else {
null
}
}
def invokeApi(
host: String,
path: String,
method: String,
queryParams: Map[String, String],
formParams: Map[String, String],
body: AnyRef,
headerParams: Map[String, String],
contentType: String
): String = {
val client = getClient(host)
val querystring = queryParams.filter(k => k._2 != null).map(k => escape(k._1) + "=" + escape(k._2)).mkString("?", "&", "")
val builder = client.resource(host + path + querystring).accept(contentType)
headerParams.map(p => builder.header(p._1, p._2))
defaultHeaders.foreach(p => {
if (!headerParams.contains(p._1) && p._2 != null) {
builder.header(p._1, p._2)
}
})
var formData: MultivaluedMapImpl = null
if (contentType == "application/x-www-form-urlencoded") {
formData = new MultivaluedMapImpl()
formParams.foreach(p => formData.add(p._1, p._2))
}
val response: ClientResponse = method match {
case "GET" => builder.get(classOf[ClientResponse])
case "POST" =>
if (formData != null && formData.size() > 0) {
builder.post(classOf[ClientResponse], formData)
} else if (body != null && body.isInstanceOf[File]) {
val file = body.asInstanceOf[File]
val form = new FormDataMultiPart()
form.field("filename", file.getName)
form.bodyPart(new FileDataBodyPart("file", file, MediaType.MULTIPART_FORM_DATA_TYPE))
builder.post(classOf[ClientResponse], form)
} else {
if (body == null) {
builder.post(classOf[ClientResponse], serialize(body))
} else {
builder.`type`(contentType).post(classOf[ClientResponse], serialize(body))
}
}
case "PUT" =>
if (formData != null) {
builder.post(classOf[ClientResponse], formData)
} else if (body == null) {
builder.put(classOf[ClientResponse], null)
} else {
builder.`type`(contentType).put(classOf[ClientResponse], serialize(body))
}
case "DELETE" => builder.delete(classOf[ClientResponse])
case "PATCH" =>
if(formData != null) {
builder.header("X-HTTP-Method-Override", "PATCH").post(classOf[ClientResponse], formData)
} else if(body == null) {
builder.header("X-HTTP-Method-Override", "PATCH").post(classOf[ClientResponse], null)
} else {
builder.header("X-HTTP-Method-Override", "PATCH").`type`(contentType).post(classOf[ClientResponse], serialize(body))
}
case _ => null
}
response.getStatusInfo.getStatusCode match {
case 204 => ""
case code: Int if Range(200, 299).contains(code) =>
if (response.hasEntity) {
response.getEntity(classOf[String])
} else {
""
}
case _ =>
val entity = if (response.hasEntity) {
response.getEntity(classOf[String])
} else {
"no data"
}
throw new ApiException(response.getStatusInfo.getStatusCode, entity)
}
}
def getClient(host: String): Client = {
if (hostMap.contains(host)) {
hostMap(host)
} else {
val client = newClient(host)
// client.addFilter(new LoggingFilter())
hostMap += host -> client
client
}
}
def newClient(host: String): Client = if (asyncHttpClient) {
import com.ning.http.client.Realm
import org.sonatype.spice.jersey.client.ahc.AhcHttpClient
import org.sonatype.spice.jersey.client.ahc.config.DefaultAhcConfig
val config: DefaultAhcConfig = new DefaultAhcConfig()
if (!authScheme.isEmpty) {
val authSchemeEnum = Realm.AuthScheme.valueOf(authScheme)
config
.getAsyncHttpClientConfigBuilder
.setRealm(new Realm.RealmBuilder().setScheme(authSchemeEnum)
.setUsePreemptiveAuth(authPreemptive).build)
}
AhcHttpClient.create(config)
} else {
Client.create()
}
}
object ApiInvoker extends ApiInvoker(
mapper = ScalaJsonUtil.getJsonMapper,
httpHeaders = mutable.HashMap(),
hostMap = mutable.HashMap(),
asyncHttpClient = false,
authScheme = "",
authPreemptive = false
)
class ApiException(val code: Int, msg: String) extends RuntimeException(msg)

View File

@ -0,0 +1,20 @@
package org.openapitools.example.invoker
import org.openapitools.example.api._
import com.wordnik.swagger.client._
import java.io.Closeable
class AsyncClient(config: SwaggerConfig) extends Closeable {
lazy val locator: ServiceLocator = config.locator
lazy val name: String = config.name
private[this] val client = transportClient
protected def transportClient: TransportClient = new RestClient(config)
def close() {
client.close()
}
}

View File

@ -0,0 +1,22 @@
/**
* OpenAPI Petstore
* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.
*
* The version of the OpenAPI document: 1.0.0
*
*
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
* https://openapi-generator.tech
* Do not edit the class manually.
*/
package org.openapitools.client.model
case class InlineObject (
// Updated name of the pet
name: Option[String] = None,
// Updated status of the pet
status: Option[String] = None
)

View File

@ -0,0 +1,23 @@
/**
* OpenAPI Petstore
* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.
*
* The version of the OpenAPI document: 1.0.0
*
*
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
* https://openapi-generator.tech
* Do not edit the class manually.
*/
package org.openapitools.client.model
import java.io.File
case class InlineObject1 (
// Additional data to pass to server
additionalMetadata: Option[String] = None,
// file to upload
file: Option[File] = None
)