Skip to content

Commit

Permalink
Add error resposnes
Browse files Browse the repository at this point in the history
  • Loading branch information
Krzysztof Palcowski authored and kristerr committed Mar 4, 2024
1 parent f9f94a0 commit 95fd5da
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 3 deletions.
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,9 @@
package pl.iterators.baklava.core.model

final case class RouteErrorResponse[T](`result`: T, status: Int)(marshaller: T => String) {
lazy val jsonData: RouteErrorResponseJsonData =
RouteErrorResponseJsonData(marshaller(result), status)

def resultName: String = result.getClass.getSimpleName.replaceAll("\\$", "")
}
final case class RouteErrorResponseJsonData(`type`: String, status: Int)
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 @@ -10,6 +10,7 @@ import io.swagger.v3.oas.models.security.{SecurityRequirement, SecurityScheme}
import pl.iterators.baklava.core.model._
import pl.iterators.baklava.formatter.openapi.builders.{OpenApiBuilder, OperationBuilder, PathItemBuilder}
import pl.iterators.kebs.jsonschema.JsonSchemaWrapper

import scala.jdk.CollectionConverters._

class OpenApiFormatterWorker(jsonSchemaToSwaggerSchemaWorker: JsonSchemaToSwaggerSchemaWorker) {
Expand Down Expand Up @@ -130,14 +131,23 @@ class OpenApiFormatterWorker(jsonSchemaToSwaggerSchemaWorker: JsonSchemaToSwagge
.foreach { case (codeOpt, desc) =>
val code = codeOpt.get // get is safe here
val apiResponse = new ApiResponse()

apiResponse.setDescription(desc.map(_.description).mkString("\n"))

if (code.intValue >= 200 && code.intValue < 204) {

val mt = routeDtoHandlerToMediaType(route.routeRepresentation.response)
apiResponse.setContent(new Content().addMediaType("application/json", mt))
} else {
val mt = new MediaType
route.routeRepresentation.errorResponses.filter(er => er.status == code.intValue()).foreach { errorResponse =>
val example = new Example()
example.setValue(s"""{"type": "${errorResponse.jsonData.`type`}", "status": ${errorResponse.status}}""")
mt.addExamples(errorResponse.resultName, example)
}
if (Option(mt.getExamples).nonEmpty) {
apiResponse.setContent(new Content().addMediaType("application/json", mt))
}
}

apiResponses.addApiResponse(code.intValue.toString, apiResponse)
}
apiResponses
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 95fd5da

Please sign in to comment.