Skip to content

Commit

Permalink
Fix handling of date format examples
Browse files Browse the repository at this point in the history
  • Loading branch information
lewisjkl committed Nov 30, 2023
1 parent d48ee26 commit 2e8dcfd
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 9 deletions.
25 changes: 16 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 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 2e8dcfd

Please sign in to comment.