Skip to content

Commit

Permalink
Merge pull request #215 from disneystreaming/issue-214
Browse files Browse the repository at this point in the history
Fix handling of date fields with examples
  • Loading branch information
daddykotex committed Nov 30, 2023
2 parents d48ee26 + 94d1d3c commit 99ef6c5
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 9 deletions.
26 changes: 17 additions & 9 deletions modules/openapi/src/internals/GetExtensions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import smithytranslate.openapi.internals.OpenApiPattern
import scala.language.reflectiveCalls
import com.fasterxml.jackson.databind.JsonNode
import com.fasterxml.jackson.databind.{node => jackson}
import java.time.format.DateTimeFormatter
import java.time.ZoneId

object GetExtensions {

Expand Down Expand Up @@ -49,16 +51,21 @@ object GetExtensions {
}
.toList

private val formatter =
DateTimeFormatter.ofPattern("yyyy-MM-dd").withZone(ZoneId.of("UTC"))

protected[smithytranslate] def anyToNode(input: Any): Node = input match {
case null => Node.nullNode()
case b: Boolean => Node.from(b)
case s: String => Node.from(s)
case i: Int => Node.from(i)
case d: Double => Node.from(d)
case s: Short => Node.from(s)
case l: Long => Node.from(l)
case f: Float => Node.from(f)
case n: Number => Node.from(n)
case null => Node.nullNode()
case b: Boolean => Node.from(b)
case s: String => Node.from(s)
case i: Int => Node.from(i)
case d: Double => Node.from(d)
case s: Short => Node.from(s)
case l: Long => Node.from(l)
case f: Float => Node.from(f)
case n: Number => Node.from(n)
case d: java.time.OffsetDateTime => Node.from(d.toString())
case d: java.util.Date => Node.from(formatter.format(d.toInstant()))
case u: java.util.UUID =>
Node.from(u.toString)
case m: java.util.Map[_, _] =>
Expand All @@ -70,6 +77,7 @@ object GetExtensions {
case c: java.util.Collection[_] =>
Node.fromNodes(c.asScala.map(anyToNode).toList.asJava)
case j: JsonNode => jacksonToSmithy(j)
case _ => Node.nullNode() // if nothing is found, to prevent match errors
}

private def jacksonToSmithy(jn: JsonNode): Node = jn match {
Expand Down
62 changes: 62 additions & 0 deletions modules/openapi/tests/src/TimestampSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,66 @@ final class TimestampSpec extends munit.FunSuite {

TestUtils.runConversionTest(openapiString, expectedString)
}

test("date-time with example") {
val openapiString = """|openapi: '3.0.'
|info:
| title: test
| version: '1.0'
|paths: {}
|components:
| schemas:
| MyTimestamp:
| type: string
| format: date-time
| example: '2017-07-21T17:32:28Z'
|""".stripMargin

val expectedString = """|namespace foo
|
|use alloy#dataExamples
|
|@dataExamples([
| {
| json: "2017-07-21T17:32:28Z"
| }
|])
|
|@timestampFormat("date-time")
|timestamp MyTimestamp
|""".stripMargin

TestUtils.runConversionTest(openapiString, expectedString)
}

test("simple date with example") {
val openapiString = """|openapi: '3.0.'
|info:
| title: test
| version: '1.0'
|paths: {}
|components:
| schemas:
| MyDate:
| type: string
| format: date
| example: '2017-07-21'
|""".stripMargin

val expectedString = """|namespace foo
|
|use alloy#dateFormat
|use alloy#dataExamples
|
|@dataExamples([
| {
| json: "2017-07-21"
| }
|])
|@dateFormat
|string MyDate
|""".stripMargin

TestUtils.runConversionTest(openapiString, expectedString)
}
}

0 comments on commit 99ef6c5

Please sign in to comment.