-
Notifications
You must be signed in to change notification settings - Fork 399
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#55: Tapir.statusFrom endpoint output
- Loading branch information
Showing
18 changed files
with
297 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package tapir | ||
import scala.reflect.ClassTag | ||
|
||
/** | ||
* Describe conditions for status code mapping using `Tapir.statusFrom`. | ||
*/ | ||
trait When[-I] { | ||
def matches(i: I): Boolean | ||
} | ||
|
||
case class WhenClass[T](ct: ClassTag[T], s: Schema) extends When[Any] { | ||
override def matches(i: Any): Boolean = ct.runtimeClass.isInstance(i) | ||
} | ||
case class WhenValue[T](p: T => Boolean) extends When[T] { | ||
override def matches(i: T): Boolean = p(i) | ||
} |
22 changes: 22 additions & 0 deletions
22
docs/openapi-docs/src/main/scala/tapir/docs/openapi/CodecToMediaType.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package tapir.docs.openapi | ||
|
||
import tapir.docs.openapi.schema.ObjectSchemas | ||
import tapir.openapi.{MediaType => OMediaType, _} | ||
import tapir.{MediaType => SMediaType, Schema => SSchema, _} | ||
|
||
private[openapi] class CodecToMediaType(objectSchemas: ObjectSchemas) { | ||
def apply[T, M <: SMediaType](o: CodecForOptional[T, M, _], | ||
example: Option[T], | ||
overrideSchema: Option[SSchema]): Map[String, OMediaType] = { | ||
val schema = overrideSchema.getOrElse(o.meta.schema) | ||
Map( | ||
o.meta.mediaType.mediaTypeNoParams -> OMediaType(Some(objectSchemas(schema)), | ||
example.flatMap(exampleValue(o, _)), | ||
Map.empty, | ||
Map.empty)) | ||
} | ||
|
||
def apply[M <: SMediaType](schema: SSchema, mediaType: M, example: Option[String]): Map[String, OMediaType] = { | ||
Map(mediaType.mediaTypeNoParams -> OMediaType(Some(objectSchemas(schema)), example.map(ExampleValue), Map.empty, Map.empty)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
docs/openapi-docs/src/main/scala/tapir/docs/openapi/EndpointToOperationResponse.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package tapir.docs.openapi | ||
|
||
import tapir.docs.openapi.schema.ObjectSchemas | ||
import tapir.openapi.OpenAPI.ReferenceOr | ||
import tapir.openapi._ | ||
import tapir.{Schema => SSchema, _} | ||
|
||
private[openapi] class EndpointToOperationResponse(objectSchemas: ObjectSchemas, codecToMediaType: CodecToMediaType) { | ||
def apply(e: Endpoint[_, _, _, _]): Map[ResponsesKey, ReferenceOr[Response]] = { | ||
// There always needs to be at least a 200 empty response | ||
outputToResponses(e.output, ResponsesCodeKey(200), Some(Response("", Map.empty, Map.empty))) ++ | ||
outputToResponses(e.errorOutput, ResponsesDefaultKey, None) | ||
} | ||
|
||
private def statusCodesToBodySchemas(io: EndpointIO[_]): Map[StatusCode, Option[SSchema]] = { | ||
io.traverse { | ||
case EndpointIO.StatusFrom(_: EndpointIO.Body[_, _, _], default, defaultSchema, whens) => | ||
val fromWhens = whens.map { | ||
case (WhenClass(_, schema), statusCode) => statusCode -> Some(schema) | ||
case (_, statusCode) => statusCode -> None | ||
} | ||
(default -> defaultSchema) +: fromWhens | ||
case EndpointIO.StatusFrom(_, default, _, whens) => | ||
val statusCodes = default +: whens.map(_._2) | ||
statusCodes.map(_ -> None) | ||
}.toMap | ||
} | ||
|
||
private def outputToResponses(io: EndpointIO[_], | ||
defaultResponseKey: ResponsesKey, | ||
defaultResponse: Option[Response]): Map[ResponsesKey, ReferenceOr[Response]] = { | ||
val statusCodes = statusCodesToBodySchemas(io) | ||
val responses = if (statusCodes.isEmpty) { | ||
// no status code mapping defined in the output - using the default response key, if there's any response defined at all | ||
outputToResponse(io, None).map(defaultResponseKey -> Right(_)).toMap | ||
} else { | ||
statusCodes.flatMap { | ||
case (statusCode, bodySchema) => | ||
outputToResponse(io, bodySchema).map((ResponsesCodeKey(statusCode): ResponsesKey) -> Right(_)) | ||
} | ||
} | ||
|
||
if (responses.isEmpty) { | ||
// no output at all - using default if defined | ||
defaultResponse.map(defaultResponseKey -> Right(_)).toMap | ||
} else responses | ||
} | ||
|
||
private def outputToResponse(io: EndpointIO[_], overrideBodySchema: Option[SSchema]): Option[Response] = { | ||
val ios = io.asVectorOfBasic() | ||
|
||
val headers = ios.collect { | ||
case EndpointIO.Header(name, codec, info) => | ||
name -> Right( | ||
Header( | ||
info.description, | ||
Some(!codec.meta.isOptional), | ||
None, | ||
None, | ||
None, | ||
None, | ||
None, | ||
Some(objectSchemas(codec.meta.schema)), | ||
info.example.flatMap(exampleValue(codec, _)), | ||
Map.empty, | ||
Map.empty | ||
)) | ||
} | ||
|
||
val bodies = ios.collect { | ||
case EndpointIO.Body(m, i) => (i.description, codecToMediaType(m, i.example, overrideBodySchema)) | ||
case EndpointIO.StreamBodyWrapper(StreamingEndpointIO.Body(s, mt, i)) => | ||
(i.description, codecToMediaType(overrideBodySchema.getOrElse(s), mt, i.example)) | ||
} | ||
val body = bodies.headOption | ||
|
||
val description = body.flatMap(_._1).getOrElse("") | ||
val content = body.map(_._2).getOrElse(Map.empty) | ||
|
||
if (body.isDefined || headers.nonEmpty) { | ||
Some(Response(description, headers.toMap, content)) | ||
} else { | ||
None | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.