Codecs for Tapir from OpenApi Specs?

Today I’m pulling a 24K line OpenAPI spec into Tapir using Generate endpoint definitions from an OpenAPI YAML — Tapir 1.x documentation .

I’m down from 600 errors at start to just 29 errors in the generated code. I think all are variations on

[error] – [E172] Type Error: /Users/dwalend/projects/apispecs/nifi/target/scala-3.3.6/src_managed/main/sbt-openapi-codegen/Connections.scala:52:49
[error] 52 | .in(queryOption[LongParameter].description(“The revision is used to verify the client is working with the latest version of the flow.”))
[error] | ^
[error] |Cannot find a codec between types: List[String] and Option[com.macrohealth.platform.api.nifi.NifiFromOpenApi.LongParameter], formatted as: sttp.tapir.CodecFormat.TextPlain.
[error] |Did you define a codec for: Option[com.macrohealth.platform.api.nifi.NifiFromOpenApi.LongParameter]?
[error] |Did you import the codecs for: sttp.tapir.CodecFormat.TextPlain?
[error] |.
[error] |I found:
[error] |
[error] | sttp.tapir.Codec.listHeadOption[String,
[error] | com.macrohealth.platform.api.nifi.NifiFromOpenApi.LongParameter, CF](
[error] | sttp.tapir.Codec.derivedStringBasedUnionEnumeration
[error] | com.macrohealth.platform.api.nifi.NifiFromOpenApi.LongParameter

[error] | )
[error] |
[error] |But macro expansion was stopped.

What’s the right fix for these? (I’ve fixed most problems by adding new libraries to the classpath. Is it just adding another .jar file into the classpath?)

Thanks,

David

1 Like

I think I’d need some more information. A simple endpoint.in(queryOption[Long]) works just fine.

However, you seem to have: query[Option[LongParameter]]. Couple of questions:

  • where is the parameter name? It seems to be missing
  • what is LongParameter?
  • what is the OpenAPI spec from which this fragment is generated?

I suspect there’s a missing codec for LongParameter, which would have the potential of being used in the context of a query parameter. But to debug this, some minimal example would be needed.

1 Like

That was exactly it. I’d guessed that LongParameter was so generic it was from Tapir’s library. It’s in the OpenAPI, and the codec is not generated from

LongParameter:

type: object

properties:

long:

type: integer

format: int64

Adding

package object nifi {
given Codec[String, LongParameter, TextPlain] = {
def decodeLongParameter(s: String): DecodeResult[LongParameter] = {
try {
val v = s.toLong
DecodeResult.Value(LongParameter(Option(v)))
} catch {
case x: NumberFormatException => DecodeResult.Error(s, x)
}
}

to the mix cleared the error. (Today’s puzzle is OOM in the compiler.)

Thanks!

Ah :slight_smile: You might then report a bug describing the OpenAPI spec fragment (for LongParameter) and the context in which it was used (query param), as I think the necessary code should be generated - but for some reason isn’t

A bug for Tapir? Let me make some time for that minimal case now that I understand it.

Yes, this definitely looks like a bug. Having this fixed might be useful for others as well :slight_smile: