Skip to content

Commit

Permalink
Codegen: handle other status codes than 200/default; add description …
Browse files Browse the repository at this point in the history
…to out/errorOut; prettier yaml decoding errors
  • Loading branch information
hughsimpson committed Aug 16, 2023
1 parent a2280d7 commit 3f0a81f
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,21 +127,34 @@ class EndpointGenerator {
openapiTags.map(_.distinct.mkString(".tags(List(\"", "\", \"", "\"))")).mkString
}

// treats redirects as ok
private val okStatus = """([23]\d\d)""".r
private val errorStatus = """([45]\d\d)""".r
private def outs(responses: Seq[OpenapiResponse]) = {
// .errorOut(stringBody)
// .out(jsonBody[List[Book]])
responses
.map { resp =>
val d = s""".description("${resp.description}")"""
resp.content match {
case Nil => ""
case Nil =>
resp.code match {
case "200" | "default" => ""
case okStatus(s) => s".out(statusCode(sttp.model.StatusCode($s))$d)"
case errorStatus(s) => s".errorOut(statusCode(sttp.model.StatusCode($s))$d)"
}
case content +: Nil =>
resp.code match {
case "200" =>
s".out(${contentTypeMapper(content.contentType, content.schema)})"
s".out(${contentTypeMapper(content.contentType, content.schema)}$d)"
case okStatus(s) =>
s".out(${contentTypeMapper(content.contentType, content.schema)}$d.and(statusCode(sttp.model.StatusCode($s))))"
case "default" =>
s".errorOut(${contentTypeMapper(content.contentType, content.schema)})"
case _ =>
throw new NotImplementedError("Statuscode mapping is incomplete!")
s".errorOut(${contentTypeMapper(content.contentType, content.schema)}$d)"
case errorStatus(s) =>
s".errorOut(${contentTypeMapper(content.contentType, content.schema)}$d.and(statusCode(sttp.model.StatusCode($s))))"
case x =>
throw new NotImplementedError(s"Statuscode mapping is incomplete! Cannot handle $x")
}
case _ => throw new NotImplementedError("We can handle only one return content!")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,52 @@ class EndpointGeneratorSpec extends CompileCheckTestBase {
BasicGenerator.imports ++
new EndpointGenerator().endpointDefs(doc) shouldCompile()
}

it should "handle status codes" in {
val doc = OpenapiDocument(
"",
null,
Seq(
OpenapiPath(
"find/{id}",
Seq(
OpenapiPathMethod(
methodType = "get",
parameters = Seq(Resolved(OpenapiParameter("id", "path", true, None, OpenapiSchemaString(true)))),
responses = Seq(
OpenapiResponse("202", "Processing", Seq(OpenapiResponseContent("text/plain", OpenapiSchemaString(false)))),
OpenapiResponse("404", "couldn't find thing", Seq(OpenapiResponseContent("text/plain", OpenapiSchemaString(false))))
),
requestBody = None,
summary = None,
tags = Some(Seq("Tag 1", "Tag 2", "Tag 1"))
)
)
),
OpenapiPath(
"find_v2/{id}",
Seq(
OpenapiPathMethod(
methodType = "get",
parameters = Seq(Resolved(OpenapiParameter("id", "path", true, None, OpenapiSchemaString(true)))),
responses = Seq(
OpenapiResponse("204", "No body", Nil),
OpenapiResponse("403", "Not authorised", Nil)
),
requestBody = None,
summary = None,
tags = Some(Seq("Tag 1", "Tag 2", "Tag 1"))
)
)
)
),
null
)
val generatedCode = BasicGenerator.imports ++ new EndpointGenerator().endpointDefs(doc)
generatedCode should include(""".out(stringBody.description("Processing").and(statusCode(sttp.model.StatusCode(202))))""") // status code with body
generatedCode should include(""".errorOut(stringBody.description("couldn't find thing").and(statusCode(sttp.model.StatusCode(404))))""") // error status code with body
generatedCode should include(""".errorOut(statusCode(sttp.model.StatusCode(403)).description("Not authorised"))""") // error status code, no body
generatedCode should include(""".out(statusCode(sttp.model.StatusCode(204)).description("No body"))""") // status code, no body
generatedCode shouldCompile()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ case class OpenapiCodegenTask(
def makeFile(file: File): Task[File] = {
task {
val parsed = YamlParser.parseFile(IO.readLines(inputYaml).mkString("\n"))
.left.map(d => new RuntimeException(_root_.io.circe.Error.showError.show(d)))
val lines = BasicGenerator.generateObjects(parsed.toTry.get, packageName, objectName, targetScala3).linesIterator.toSeq
IO.writeLines(file, lines, IO.utf8)
file
Expand Down

0 comments on commit 3f0a81f

Please sign in to comment.