我正在使用openapi-generator 生成下面顯示的 Scala 代碼。
import examples.openverse.model.{InlineObject, OAuth2RegistrationSuccessful}
import examples.openverse.core.JsonSupport._
import sttp.client3._
import sttp.model.Method
object AuthTokensApi {
def apply(baseUrl: String = "https://api.openverse.engineering/v1") = new AuthTokensApi(baseUrl)
}
def registerApiOauth2(format: String, data: InlineObject
): Request[Either[ResponseException[String, Exception], OAuth2RegistrationSuccessful], Any] =
basicRequest
.method(Method.POST, uri"$baseUrl/auth_tokens/register/?format=${ format }")
.contentType("application/json")
.body(data)
.response(asJson[OAuth2RegistrationSuccessful])
WhereInlineObject
和OAuth2RegistrationSuccessful
是簡單的案例類,并且JsonSupport
如下:
object JsonSupport extends SttpJson4sApi {
def enumSerializers: Seq[Serializer[_]] = Seq[Serializer[_]]() :
new EnumNameSerializer(AudioReportRequestEnums.Reason) :
new EnumNameSerializer(ImageReportRequestEnums.Reason)
private class EnumNameSerializer[E <: Enumeration: ClassTag](enum: E) extends Serializer[E#Value] {
import JsonDSL._
val EnumerationClass: Class[E#Value] = classOf[E#Value]
def deserialize(implicit format: Formats): PartialFunction[(TypeInfo, JValue), E#Value] = {
case (t @ TypeInfo(EnumerationClass, _), json) if isValid(json) =>
json match {
case JString(value) => enum.withName(value)
case value => throw new MappingException(s"Can't convert $value to $EnumerationClass")
}
}
private[this] def isValid(json: JValue) = json match {
case JString(value) if enum.values.exists(_.toString == value) => true
case _ => false
}
def serialize(implicit format: Formats): PartialFunction[Any, JValue] = {
case i: E#Value => i.toString
}
}
implicit val format: Formats = DefaultFormats enumSerializers DateSerializers.all
implicit val serialization: org.json4s.Serialization = org.json4s.jackson.Serialization
}
為了重現,我只需registerApiOauth2
使用相應的引數呼叫該方法。
問題是編譯器由于format
帶有以下錯誤的引數而崩潰:
No implicit view available from examples.openverse.model.InlineObject => sttp.client3.BasicRequestBody.
.body(data)
No org.json4s.Formats found. Try to bring an instance of org.json4s.Formats in scope or use the org.json4s.DefaultFormats.
.response(asJson[OAuth2RegistrationSuccessful])
當我更改format
為任何其他引數(例如frmt
. 但是,這不能完成,因為生成的查詢引數必須這樣呼叫。這是我第一次遇到這樣的問題,并希望有一個解決方法。我的預感是問題源于JsonSupport
物件。
使用 MCVE 鏈接到 Scastie:https ://scastie.scala-lang.org/oDmYLP8MQOCMqUYEwhJnDg
uj5u.com熱心網友回復:
我設法重現。我JsonSupport._
在類中添加了匯入AuthTokensApi
。
使用format
https://scastie.scala-lang.org/DmytroMitin/mLtRiWE7SQKySehLJykLAQ無法編譯。
使用frmt
https://scastie.scala-lang.org/DmytroMitin/mLtRiWE7SQKySehLJykLAQ/2編譯。
這種行為是可以理解的。format
方法引數registerApiOauth2
def registerApiOauth2(format: String, data: InlineObject)...
按名稱隱藏format
內部定義的隱式JsonSupport
implicit val format: Formats = DefaultFormats enumSerializers DateSerializers.all
當一個隱式在這里被解決時
def registerApiOauth2(format: String, data: InlineObject)... = {
...
.body(data)(json4sBodySerializer(... /* HERE! */ ..., .....))
...
}
所以嘗試重命名前者或后者。如果您無法重命名它們中的任何一個,則手動決議隱式并將隱式參考為JsonSupport.format
,而不僅僅是format
basicRequest
.method(Method.POST, uri"$baseUrl/auth_tokens/register/?format=${format}")
.contentType("application/json")
.body(data)(json4sBodySerializer(JsonSupport.format, serialization))
.response(asJson[OAuth2RegistrationSuccessful](
implicitly[Manifest[OAuth2RegistrationSuccessful]],
JsonSupport.format,
serialization
))
您可以閱讀有關按名稱隱藏隱式的更多資訊:
隱式決議時出現 NullPointerException
擴展具有需要隱式成員的特征的物件
具有相同行為的更簡單示例:代碼
implicit val i: Int = 1
def m()(implicit x: Int) = ???
def m1()(i: String) = {
m()
}
不編譯
implicit val i1: Int = 1
def m()(implicit x: Int) = ???
def m1()(i: String) = {
m()
}
和
implicit val i: Int = 1
def m()(implicit x: Int) = ???
def m1()(i1: String) = {
m()
}
編譯。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/508555.html