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

use SString schema for LocalDateTime codec #989

Merged
merged 1 commit into from
Feb 8, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 10 additions & 13 deletions core/src/main/scala/sttp/tapir/Schema.scala
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
package sttp.tapir

import java.io.InputStream
import java.math.{BigDecimal => JBigDecimal}
import java.nio.ByteBuffer
import java.time._
import java.util.{Date, UUID}

import magnolia.Magnolia
import sttp.model.Part
import sttp.tapir.SchemaType._
import sttp.tapir.generic.Derived
import sttp.tapir.generic.internal.OneOfMacro.oneOfMacro
import sttp.tapir.generic.internal.SchemaMapMacro
import sttp.tapir.generic.internal.{SchemaMagnoliaDerivation, SchemaMapMacro}
import sttp.tapir.internal.ModifySchemaMacro

import scala.annotation.StaticAnnotation
import scala.annotation.implicitNotFound
import sttp.tapir.generic.Derived
import sttp.tapir.generic.internal.SchemaMagnoliaDerivation
import magnolia.Magnolia
import java.io.InputStream
import java.math.{BigDecimal => JBigDecimal}
import java.nio.ByteBuffer
import java.time._
import java.util.{Date, UUID}
import scala.annotation.{StaticAnnotation, implicitNotFound}

/** Describes the type `T`: its low-level representation, meta-data and validation rules.
* @param format The name of the format of the low-level representation of `T`.
Expand Down Expand Up @@ -142,7 +139,7 @@ object Schema extends SchemaExtensions with SchemaMagnoliaDerivation with LowPri
implicit val schemaForZonedDateTime: Schema[ZonedDateTime] = Schema(SDateTime)
implicit val schemaForOffsetDateTime: Schema[OffsetDateTime] = Schema(SDateTime)
implicit val schemaForDate: Schema[Date] = Schema(SDateTime)
implicit val schemaForLocalDateTime: Schema[LocalDateTime] = Schema(SDateTime)
implicit val schemaForLocalDateTime: Schema[LocalDateTime] = Schema(SString)
implicit val schemaForLocalDate: Schema[LocalDate] = Schema(SDate)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about LocalDate? :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would leave SDate schema for it, as swagger using it returns correct example, eg. "2017-07-21"

implicit val schemaForZoneOffset: Schema[ZoneOffset] = Schema(SString)
implicit val schemaForJavaDuration: Schema[Duration] = Schema(SString)
Expand Down
17 changes: 17 additions & 0 deletions docs/openapi-docs/src/test/resources/expected_localDateTime.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
openapi: 3.0.3
info:
title: Examples
version: '1.0'
paths:
/:
get:
operationId: getRoot
parameters:
- name: localDateTime
in: query
required: true
schema:
type: string
responses:
'200':
description: ''
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package sttp.tapir.docs.openapi

import java.time.Instant
import java.time.{Instant, LocalDateTime}
import io.circe.Json
import io.circe.generic.auto._
import sttp.model.{Method, StatusCode}
Expand Down Expand Up @@ -896,6 +896,18 @@ class VerifyYamlTest extends AnyFunSuite with Matchers {
actualYamlNoIndent shouldBe expectedYaml
}

test("should use string format for LocalDateTime fields") {

val expectedYaml = loadYaml("expected_localDateTime.yml")

val e = endpoint.in(query[LocalDateTime]("localDateTime"))

val actualYaml = OpenAPIDocsInterpreter.toOpenAPI(e, Info("Examples", "1.0")).toYaml
val actualYamlNoIndent = noIndentation(actualYaml)

actualYamlNoIndent shouldBe expectedYaml
}

test("exclusive bounds") {
val expectedYaml = loadYaml("expected_exclusive_bounds.yml")

Expand Down