Skip to content

Commit

Permalink
Change param representation
Browse files Browse the repository at this point in the history
  • Loading branch information
Krzysztof Palcowski committed Mar 11, 2024
1 parent 95fd5da commit 7f6fb86
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,42 @@
package pl.iterators.baklava.core.model

import scala.reflect.runtime.universe.TypeTag
import scala.util.Random

case class RouteParameterRepresentation[T](
name: String,
required: Boolean,
sampleValue: T,
marshaller: T => String,
valueGenerator: () => T,
marshaller: T => String = RouteParameterRepresentation.marshaller[T] _,
required: Boolean = false,
seq: Boolean = false,
seqMin: Int = 2,
seqMax: Int = 5,
enumValues: Option[Seq[T]] = None
)(implicit typeTag: TypeTag[T]) {

lazy val marshall: String = marshaller(sampleValue)
lazy val sampleValue: T =
if (!seq) valueGenerator() else sys.error("sampleValue should not be called on seq parameter")

lazy val seqSampleValue: Seq[T] =
if (seq) (0 until Random.nextInt(1 + seqMax - seqMin) + seqMin).map(_ => valueGenerator())
else sys.error("seqSampleValue should not be called on seq parameter")

lazy val enums: Option[Seq[String]] = enumValues.map(values => values.map(marshaller))

lazy val scalaType: String = typeTag.tpe.toString
lazy val simpleType: String = typeTag.tpe.toString

lazy val queryString: String =
if (seq) {
seqSampleValue.map(v => s"$name=${marshaller(v)}").mkString("&")
} else
s"$name=${marshaller(sampleValue)}"

}

object RouteParameterRepresentation {
def marshaller[T](value: T): String =
value match {
case string: String => string
case other => other.toString
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ case class RouteRepresentation[Request, Response](
val parametersPath = Option
.when(requiredParams.nonEmpty) {
requiredParams
.map { param =>
s"${param.name}=${param.marshall}"
}
.map(_.queryString)
.mkString("?", "&", "")
}
.getOrElse("")
Expand All @@ -48,9 +46,7 @@ case class RouteRepresentation[Request, Response](
val parametersPath = Option
.when(parameters.nonEmpty) {
parameters
.map { param =>
s"${param.name}=${param.marshall}"
}
.map(_.queryString)
.mkString("?", "&", "")
}
.getOrElse("")
Expand All @@ -64,4 +60,7 @@ case class RouteRepresentation[Request, Response](
def getParamValue[T](name: String): Option[T] =
parameters.find(_.name == name).map(_.sampleValue.asInstanceOf[T])

def getSeqParamValue[T](name: String): Option[Seq[T]] =
parameters.find(_.name == name).map(_.seqSampleValue.asInstanceOf[Seq[T]])

}
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,21 @@ class OpenApiFormatterWorker(jsonSchemaToSwaggerSchemaWorker: JsonSchemaToSwagge

private def queryParamsToParams(parameters: List[RouteParameterRepresentation[_]]): List[Parameter] =
parameters.map { param =>
val schema = new StringSchema
schema.setExample(param.sampleValue)
val itemSchema = new StringSchema
itemSchema.setExample(param.valueGenerator())
param.enums.foreach { values =>
schema.setEnum(values.toList.asJava)
itemSchema.setEnum(values.toList.asJava)
}

val schema: Schema[_] =
if (param.seq) {
val arraySchema = new ArraySchema
arraySchema.setItems(itemSchema)
arraySchema
} else itemSchema

val p = new Parameter()
p.setName(param.name)
if (param.seq) p.setName(s"${param.name}[]") else p.setName(param.name)
p.setIn("query")
p.setRequired(param.required)
p.setSchema(schema)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import com.github.andyglow.json.JsonFormatter
import com.github.andyglow.jsonschema.AsValue
import json.schema.Version.Draft07
import pl.iterators.baklava.core.model.EnrichedRouteRepresentation
import pl.iterators.baklava.core.utils.option.RichOptionCompanion

import java.io.{File, FileWriter, PrintWriter}
import scala.util.Try
Expand Down Expand Up @@ -133,7 +132,13 @@ class SimpleDocsFormatter extends Formatter {
},
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/>")}" +
s"${r.parameters
.map(h =>
s"${h.name}${if (h.seq) "[]" else ""}${Option.when(h.required)(s"<b style='color: red'>*</b>").getOrElse("")}: ${h.simpleType}${if (h.seq) "[]"
else ""} " +
s"(${h.enums.map(enums => enums.mkString("possible values: ", ", ", "")).getOrElse(s"example: ${h.valueGenerator()}")})"
)
.mkString("<br/>")}" +
s"</td></tr>" +
s"<tr><td><b>ROUTE WITH MINIMAL PARAMS</b></td><td>${r.routePathWithRequiredParameters}</td></tr>" +
s"<tr><td><b>ROUTE WITH ALL PARAMS</b></td><td>${r.routePathWithAllParameters}</td></tr>"
Expand Down

0 comments on commit 7f6fb86

Please sign in to comment.