# Automatic derivation of ZIO Prelude New Types

**URL:** <https://softwaremill.community/t/automatic-derivation-of-zio-prelude-new-types/62>\
**Category:** tapir\
**Created:** [January 2, 2023, 6:29am UTC](https://softwaremill.community/t/automatic-derivation-of-zio-prelude-new-types/62 "2023-01-02T06:29:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![boniface](https://dub1.discourse-cdn.com/flex005/user_avatar/softwaremill.community/boniface/32/56_2.png) [@boniface](https://softwaremill.community/u/boniface)\
**Post date:** [January 2, 2023, 6:29am UTC](https://softwaremill.community/t/automatic-derivation-of-zio-prelude-new-types/62/1 "2023-01-02T06:29:32Z")

</div>

Been struggling to do Automatic derivation of classes encoded with ZIO prelude’s newtypes. Here is a full code lists that can be reproduced in a scala worksheet

```auto
import sttp.tapir._
import sttp.tapir.generic.auto._
import sttp.tapir.json.zio.jsonBody
import sttp.tapir.ztapir.ZServerEndpoint
import zio.ZIO
import zio.json._
import zio.prelude.Newtype

object Name extends Newtype[String] {
  implicit val codec: JsonCodec[Type] = derive
}
type Name = Name.Type

case class Person(name: Name)

object Person {
  implicit val encoder: zio.json.JsonEncoder[Person] = DeriveJsonEncoder.gen[Person]
  implicit val decoder: zio.json.JsonDecoder[Person] = DeriveJsonDecoder.gen[Person]
}

object PersonRoute {

  val persons = List(Person(Name("John")),Person(Name("Jane")))

  private val personListEndPoint: PublicEndpoint[Unit, Unit, List[Person], Any] =
    endpoint.get
      .in("user" / "one" )
      .out(jsonBody[List[Person]])
  val personsListingServerEndpoint: ZServerEndpoint[Any, Any] =
    personListEndPoint.serverLogicSuccess(_ => ZIO.succeed(persons))
}

```

When run I get the error below with the suggestions, but I already implemented the suggestion

```auto
ould not find Schema for type List[A$A0.this.Person].
Automatic derivation requires the following import: `import sttp.tapir.generic.auto._`
You can find more details in the docs: https://tapir.softwaremill.com/en/latest/endpoint/schemas.html#schema-derivation
When using datatypes integration remember to import respective schemas/codecs as described in https://tapir.softwaremill.com/en/latest/endpoint/integrations.html
      .out(jsonBody[List[Person]])

```

What would be the workaround this? Though the error message is not helpful, it seems the encoding/decoding of the ZIO Prelude newtype is the real issue at play here and tried a number of combinations with no success.

---

<div class="post-metadata">

**Author:** ![Damian\_Karwacki](https://dub1.discourse-cdn.com/flex005/user_avatar/softwaremill.community/damian_karwacki/32/61_2.png) [@Damian\_Karwacki](https://softwaremill.community/u/Damian_Karwacki)\
**Post date:** [January 3, 2023, 8:24pm UTC](https://softwaremill.community/t/automatic-derivation-of-zio-prelude-new-types/62/2 "2023-01-03T20:24:02Z")

</div>

Hi,  
you can add `"com.softwaremill.sttp.tapir" %% "tapir-zio-prelude" % "VERSION"` dependency to your project.

Once added, you will be able to use TapirNewtypeSupport trait to provide tapir codec and schema for your new type.

Usage looks like that:

```auto
object Name extends Newtype[String] with TapirNewtypeSupport[String]

```

---

<div class="post-metadata">

**Author:** ![boniface](https://dub1.discourse-cdn.com/flex005/user_avatar/softwaremill.community/boniface/32/56_2.png) [@boniface](https://softwaremill.community/u/boniface)\
**Post date:** [January 4, 2023, 7:31pm UTC](https://softwaremill.community/t/automatic-derivation-of-zio-prelude-new-types/62/3 "2023-01-04T19:31:54Z")

</div>

Thanks, Damian, it works. I must say that Tapir is quite extensive.

Now I have to think about how I solve the other incidental issues that arise from the suggested approach. Do I want the large domain model to have tapir everywhere or is there a way I can abstract the solution away? I need to make the solution easy to change or replace.

Once again thanks

---

<div class="post-metadata">

**Author:** ![Damian\_Karwacki](https://dub1.discourse-cdn.com/flex005/user_avatar/softwaremill.community/damian_karwacki/32/61_2.png) [@Damian\_Karwacki](https://softwaremill.community/u/Damian_Karwacki)\
**Post date:** [January 4, 2023, 8:15pm UTC](https://softwaremill.community/t/automatic-derivation-of-zio-prelude-new-types/62/4 "2023-01-04T20:15:47Z")

</div>

As an alternative, you can define codecs and schemas by hand, like that:

```auto
implicit val codec = TapirNewtype[String](Name).tapirCodec[String, CodecFormat.TextPlain]
implicit val schema = TapirNewtype[String](Name).tapirSchema

```

and then place them in an object that’s separate from your domain models definitions
