Skip to content

Commit

Permalink
Add error resposnes
Browse files Browse the repository at this point in the history
  • Loading branch information
Krzysztof Palcowski committed Mar 1, 2024
1 parent f9f94a0 commit 97f4f15
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package pl.iterators.baklava.core.model

import scala.util.Try

class EnrichedRouteRepresentation[Request, Response] private (
val routeRepresentation: RouteRepresentation[Request, Response],
val enrichDescriptions: Seq[EnrichedDescription]) {
Expand Down Expand Up @@ -50,6 +52,6 @@ object EnrichedDescription {
description.toLowerCase
.replace("inaccessible", "unauthorized")
.split(" ")
.flatMap(s => statusCodesMap.get(s))
.flatMap(s => Try(s.toInt).toOption.orElse(statusCodesMap.get(s)))
.headOption
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package pl.iterators.baklava.core.model

final case class RouteErrorResponse[T](result: T, status: Int) {
lazy val jsonData: RouteErrorResponseJsonData =
RouteErrorResponseJsonData(s"result:${RouteErrorResponse.typeFromResult(result)}", status)

def resultName: String = result.getClass.getSimpleName.replaceAll("$", "")
}
final case class RouteErrorResponseJsonData(`type`: String, status: Int)

object RouteErrorResponse {
def typeFromResult[T](result: T): String = {
val cls = result.getClass
val leading =
Option(cls.getEnclosingClass)
.map(_.getSimpleName.filterNot(_ == '$').replace("Result", "").replaceAll("([a-z])([A-Z])", "$1-$2").toLowerCase + ":")
leading.getOrElse("") +
cls.getSimpleName.filterNot(_ == '$').replaceAll("([a-z])([A-Z])", "$1-$2").toLowerCase // kebabify
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ case class RouteRepresentation[Request, Response](
path: String,
parameters: List[RouteParameterRepresentation[_]] = Nil,
headers: List[RouteHeaderRepresentation] = Nil,
errorResponses: List[RouteErrorResponse[_]] = Nil,
requestPredefinedValue: Option[Request] = None,
responsePredefinedValue: Option[Response] = None,
authentication: List[RouteSecurityGroup] = List.empty,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,14 @@ class OpenApiFormatterWorker(jsonSchemaToSwaggerSchemaWorker: JsonSchemaToSwagge
val mt = routeDtoHandlerToMediaType(route.routeRepresentation.response)
apiResponse.setContent(new Content().addMediaType("application/json", mt))
}
route.routeRepresentation.errorResponses.find(er => er.status == code.intValue()).foreach { errorResponse =>
val mt = new MediaType

val example = new Example()
example.setValue(s"""{"type": "${errorResponse.jsonData.`type`}", "status": ${errorResponse.jsonData.status}}""")
mt.addExamples("const", example)
apiResponse.setContent(new Content().addMediaType("application/json", mt))
}

apiResponses.addApiResponse(code.intValue.toString, apiResponse)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ class SimpleDocsFormatter extends Formatter {
}
.mkString("<br/>")}</td></tr>"),
Some(s"<tr><td><b>STATUS CODES</b></td><td>${statusCodes.mkString("<br/>")}</td></tr>"),
Option.when(r.errorResponses.nonEmpty) {
s"<tr><td><b>ERROR RESPONSES</b></td><td>" +
s"${r.errorResponses.map(er => s"type: ${er.jsonData.`type`}, code: ${er.status}").mkString("<br/>")}</td></tr>"
},
Option.when(r.parameters.nonEmpty) {
s"<tr><td><b>PARAMETERS</b></td><td>" +
s"${r.parameters.map(h => s"${h.name} [${h.scalaType}] ${Option.when(h.required)(s"<b style='color: red'>*</b>").getOrElse("")}").mkString("<br/>")}" +
Expand Down

0 comments on commit 97f4f15

Please sign in to comment.