Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ object InAndOutComponents {
private[endpoints] def aliases(packageReuse: PackageReuseContext, types: Seq[String], seperateFilesForModels: Boolean): String =
types.map(PackageReuseContext.enumAliasType(_, packageReuse, seperateFilesForModels)).mkString("\n")

private def mkWrapHelper(required: Boolean)(s: String): String =
if (required) s else s"$s.map(Option(_))(_.orNull)"
private def mkWrapTypeHelper(required: Boolean)(s: String): String =
if (required) s else s"Option[$s]"

private[endpoints] def contentTypeMapper(
contentType: String,
schema: OpenapiSchemaType,
Expand All @@ -64,6 +69,8 @@ object InAndOutComponents {
tapirCodegenDirectives: Set[String],
validators: ValidationDefns
)(implicit location: Location): MappedContentType = {
def wrapHelper: String => String = mkWrapHelper(required)
def wrapTypeHelper: String => String = mkWrapTypeHelper(required)
def vRef(t: OpenapiSchemaType, r: Boolean) = {
val maybeValidatorRef = t match {
case r: OpenapiSchemaRef => Some(r.stripped)
Expand All @@ -88,13 +95,14 @@ object InAndOutComponents {
(tapirCodegenDirectives.contains(forceEager) && position != Err) ||
(tapirCodegenDirectives.contains(forceReqEager) && position == Request) ||
(tapirCodegenDirectives.contains(forceRespEager) && position == Response))
def mkMapContentType(i: String, t: String) = MappedContentType(wrapHelper(i), wrapTypeHelper(t))
contentType match {
case any if streaming =>
failoverBinaryCase(endpointName, position, any, schema, false, streamingImplementation)
failoverBinaryCase(endpointName, position, any, schema, false, streamingImplementation, required)
case "text/plain" =>
MappedContentType("stringBody", "String")
mkMapContentType("stringBody", "String")
case "text/html" =>
MappedContentType("htmlBodyUtf8", "String")
mkMapContentType("htmlBodyUtf8", "String")
case ct if ContentTypes.isXml(ct) && xmlSerdeLib != XmlSerdeLib.NoSupport =>
val (outT: String, maybeInline: Option[String], maybeAlias: Option[String], maybeTpe: Seq[String]) = schema match {
case st: OpenapiSchemaSimpleType =>
Expand All @@ -115,8 +123,7 @@ object InAndOutComponents {
MappedContentType(bodyType + v(required), req, maybeInline, maybeTpe)
case ct if ContentTypes.isJson(ct) && tapirCodegenDirectives.contains(jsonBodyAsString) =>
val body = if (ContentTypes.isSuffixed(ct)) suffixedBody("sttp.tapir.Codec.string", ct) else "stringJsonBody"
if (required) MappedContentType(body, "String", None)
else MappedContentType(s"$body.map(Option(_))(_.orNull)", "Option[String]", None)
mkMapContentType(body, "String")
case ct if ContentTypes.isJson(ct) =>
val (outT, maybeInline) = schema match {
case st: OpenapiSchemaSimpleType =>
Expand All @@ -140,15 +147,15 @@ object InAndOutComponents {
case "multipart/form-data" =>
schema match {
case _: OpenapiSchemaBinary =>
MappedContentType("multipartBody", "Seq[Part[Array[Byte]]]")
mkMapContentType("multipartBody", "Seq[Part[Array[Byte]]]")
case schemaRef: OpenapiSchemaRef =>
val (t, _) = mapSchemaSimpleTypeToType(schemaRef, multipartForm = true)
MappedContentType(s"multipartBody[$t]" + v(required), t)
MappedContentType(wrapHelper(s"multipartBody[$t]") + v(required), wrapTypeHelper(t))
case schemaRef: OpenapiSchemaObject if schemaRef.properties.forall(_._2.`type`.isInstanceOf[OpenapiSchemaSimpleType]) =>
val (inlineClassName, inlineClassDefn) = inlineDefn(endpointName, position, schemaRef)
MappedContentType(
s"multipartBody[$inlineClassName]" + v(required),
inlineClassName,
wrapHelper(s"multipartBody[$inlineClassName]") + v(required),
wrapTypeHelper(inlineClassName),
inlineClassDefn,
inlineClassDefn.map(_ => inlineClassName).toSeq
)
Expand All @@ -161,7 +168,8 @@ object InAndOutComponents {
other,
schema,
eager,
streamingImplementation
streamingImplementation,
required
)
}
}
Expand All @@ -172,8 +180,11 @@ object InAndOutComponents {
contentType: String,
schema: OpenapiSchemaType,
isEager: Boolean,
streamingImplementation: StreamingImplementation
streamingImplementation: StreamingImplementation,
required: Boolean
)(implicit location: Location): MappedContentType = {
def wrapHelper: String => String = mkWrapHelper(required)
def wrapTypeHelper: String => String = mkWrapTypeHelper(required)
def codec(baseType: String, contentType: String) = {
val cf = codecFormat(contentType)
val schema = if (baseType == "Array[Byte]") "Schema.schemaForByteArray" else "Schema.schemaForString"
Expand All @@ -195,7 +206,7 @@ object InAndOutComponents {
case "application/zip" => "CodecFormat.Zip()"
case o => codecFormat(o).instance
}
if (isEager) MappedContentType(eagerBody, if (contentType.startsWith("text/")) "String" else "Array[Byte]")
if (isEager) MappedContentType(wrapHelper(eagerBody), wrapTypeHelper(if (contentType.startsWith("text/")) "String" else "Array[Byte]"))
else {
val capability = capabilityImpl(streamingImplementation)
val tpe = capabilityType(streamingImplementation)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,8 @@ object OutComponent {
headerDefns(targetScala3, jsonSerdeLib, doc, generateValidators, ambiguous, isErrorPosition)(endpointName, many)

val noAmbiguity = noHeaders && !ambiguous
val bodyIsStreaming = (!isErrorPosition && tapirCodegenDirectives.contains(forceRespStreaming)) ||
(!isErrorPosition && tapirCodegenDirectives.contains(forceStreaming))

val (oneOfs, types, inlineDefns) = many.map { m =>
val (decl, maybeBodyType, inlineDefn1) = bodyFmt(m, isErrorPosition, optional = contentCanBeEmpty)
Expand Down Expand Up @@ -358,8 +360,10 @@ object OutComponent {
val (_, nonOptionalType, _) = bodyFmt(m, isErrorPosition)
val maybeMap = if (m.content.size > 1 || tpeIsBin) ".map(Some(_))(_.orNull)" else ""
val someType = nonOptionalType.map(": " + _.replaceAll("^Option\\[(.+)]$", "$1")).getOrElse("")
val tpesAreBin = someType.contains("BinaryStream") || someType.contains("fs2.Stream")
def maybeUnchecked = if (tpesAreBin) " @scala.unchecked" else ""
(
s"oneOfVariantValueMatcher($maybeCode$decl$maybeStrict$maybeMap$h){ case ${matchBodyAndHeaders(s"Some(_$someType)")} => true }",
s"oneOfVariantValueMatcher($maybeCode$decl$maybeStrict$maybeMap$h){ case ${matchBodyAndHeaders(s"Some(_$someType$maybeUnchecked)")} => true }",
maybeBodyType,
inlineDefn1
)
Expand Down Expand Up @@ -393,8 +397,6 @@ object OutComponent {
.map { case (k, vs) => k -> vs.map(_._2) }
.toMap
val traitName = s"${endpointName.capitalize}Body${if (isErrorPosition) "Err" else "Out"}"
val bodyIsStreaming = (!isErrorPosition && tapirCodegenDirectives.contains(forceRespStreaming)) ||
(!isErrorPosition && tapirCodegenDirectives.contains(forceStreaming))
val bodyIsEager = !bodyIsStreaming && (isErrorPosition ||
(!isErrorPosition && tapirCodegenDirectives.contains(forceRespEager)) ||
(!isErrorPosition && tapirCodegenDirectives.contains(forceEager)))
Expand Down
Loading
Loading