play-swagger icon indicating copy to clipboard operation
play-swagger copied to clipboard

Support `Json.valueFormat`

Open lalnuo opened this issue 3 years ago • 3 comments

If class is defined like this:

case class TaskId(value: UUID) extends AnyVal

object TaskId {
  implicit val jsonFormat = Json.valueFormat[TaskId]
}

And play is used for serialization:

Json.toJson(TaskId(UUID.randomUUID()))

It will serialize to:

"482a4c0a-dc04-4c67-8821-b93096978a3d"

It would be nice if play-swagger handled this automatically. Currently i'm writing mappings like this:

- type:models\.ids\.taskid
  specAsParameter:
    - type: string
      format: uuid

lalnuo avatar Jun 18 '21 10:06 lalnuo

Hello. I'm writing (only one) mappings for UUID like this:

- type: uuid
  specAsParameter:
    - type: string
      format: uuid

in file conf/swagger-custom-mappings.yml

vnt-83 avatar Jul 28 '21 18:07 vnt-83

@vnt-83 This issue is about value classes.

For example, if you have following definition:

case class Foo(bar: String)

object Foo {
  implicit val jsonFormat = Json.valueFormat[Foo]
}

You can do this:

scala> Json.parse("\"foo\"").as[Foo]
val res1: Foo = Foo(foo)

However play-swagger plugin will generate api definition like this:

components:
  schemas:
    Foo:
      properties:
        bar:
          type: string

lalnuo avatar Aug 26 '21 20:08 lalnuo

@lalnuo We are currently reviewing it. It may take some time, but we plan to address it.

Javakky-pxv avatar Jul 21 '23 03:07 Javakky-pxv