diff --git a/core/src/main/scala/pl/iterators/baklava/core/model/EnrichedRouteRepresentation.scala b/core/src/main/scala/pl/iterators/baklava/core/model/EnrichedRouteRepresentation.scala index e192551..e507320 100644 --- a/core/src/main/scala/pl/iterators/baklava/core/model/EnrichedRouteRepresentation.scala +++ b/core/src/main/scala/pl/iterators/baklava/core/model/EnrichedRouteRepresentation.scala @@ -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]) { @@ -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 } diff --git a/core/src/main/scala/pl/iterators/baklava/core/model/RouteErrorResponse.scala b/core/src/main/scala/pl/iterators/baklava/core/model/RouteErrorResponse.scala new file mode 100644 index 0000000..4aec9c1 --- /dev/null +++ b/core/src/main/scala/pl/iterators/baklava/core/model/RouteErrorResponse.scala @@ -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 + } +} diff --git a/core/src/main/scala/pl/iterators/baklava/core/model/RouteRepresentation.scala b/core/src/main/scala/pl/iterators/baklava/core/model/RouteRepresentation.scala index 990052c..3072b76 100644 --- a/core/src/main/scala/pl/iterators/baklava/core/model/RouteRepresentation.scala +++ b/core/src/main/scala/pl/iterators/baklava/core/model/RouteRepresentation.scala @@ -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, diff --git a/formatter-openapi/src/main/scala/pl/iterators/baklava/formatter/openapi/OpenApiFormatterWorker.scala b/formatter-openapi/src/main/scala/pl/iterators/baklava/formatter/openapi/OpenApiFormatterWorker.scala index d3b689c..8e7422a 100644 --- a/formatter-openapi/src/main/scala/pl/iterators/baklava/formatter/openapi/OpenApiFormatterWorker.scala +++ b/formatter-openapi/src/main/scala/pl/iterators/baklava/formatter/openapi/OpenApiFormatterWorker.scala @@ -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) } diff --git a/formatter/src/main/scala/pl/iterators/baklava/formatter/SimpleDocsFormatter.scala b/formatter/src/main/scala/pl/iterators/baklava/formatter/SimpleDocsFormatter.scala index bc3d3f8..3695ebe 100644 --- a/formatter/src/main/scala/pl/iterators/baklava/formatter/SimpleDocsFormatter.scala +++ b/formatter/src/main/scala/pl/iterators/baklava/formatter/SimpleDocsFormatter.scala @@ -127,6 +127,10 @@ class SimpleDocsFormatter extends Formatter { } .mkString("
")}"), Some(s"STATUS CODES${statusCodes.mkString("
")}"), + Option.when(r.errorResponses.nonEmpty) { + s"ERROR RESPONSES" + + s"${r.errorResponses.map(er => s"type: ${er.jsonData.`type`}, code: ${er.status}").mkString("
")}" + }, Option.when(r.parameters.nonEmpty) { s"PARAMETERS" + s"${r.parameters.map(h => s"${h.name} [${h.scalaType}] ${Option.when(h.required)(s"*").getOrElse("")}").mkString("
")}" +