Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix handling of date fields with examples #215

Merged
merged 2 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()))
daddykotex marked this conversation as resolved.
Show resolved Hide resolved
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)
}
}
Loading