Skip to content

Commit

Permalink
Fix Scala 3 build
Browse files Browse the repository at this point in the history
  • Loading branch information
987Nabil committed Oct 14, 2023
1 parent 690ad90 commit f8581d1
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -421,9 +421,9 @@ object JsonSchema {

private def nominal(schema: Schema[_], referenceType: SchemaStyle = SchemaStyle.Reference): Option[String] =
schema match {
case enum: Schema.Enum[_] => refForTypeId(enum.id, referenceType)
case record: Schema.Record[_] => refForTypeId(record.id, referenceType)
case _ => None
case enumSchema: Schema.Enum[_] => refForTypeId(enumSchema.id, referenceType)
case record: Schema.Record[_] => refForTypeId(record.id, referenceType)
case _ => None
}

private def refForTypeId(id: TypeId, referenceType: SchemaStyle): Option[String] =
Expand Down
96 changes: 48 additions & 48 deletions zio-http/src/main/scala/zio/http/endpoint/openapi/OpenAPI.scala
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ object OpenAPI {
externalDocs = None,
)

implicit val statusSchema =
implicit val statusSchema: Schema[Status] =
zio.schema
.Schema[String]
.transformOrFail[Status](
Expand Down Expand Up @@ -160,8 +160,8 @@ object OpenAPI {
implicit def keyMapSchema[T](implicit
schema: Schema[T],
): Schema[Map[Key, T]] =
DeriveSchema
.gen[Map[String, T]]
Schema
.map[String, T]
.transformOrFail(
m => {
val it = m.iterator
Expand All @@ -183,8 +183,8 @@ object OpenAPI {
implicit def statusMapSchema[T](implicit
schema: Schema[T],
): Schema[Map[StatusOrDefault, T]] =
DeriveSchema
.gen[Map[String, T]]
Schema
.map[String, T]
.transformOrFail(
m => {
val it = m.iterator
Expand Down Expand Up @@ -415,7 +415,7 @@ object OpenAPI {
* @param name
* The field name of the relative path MUST begin with a forward slash (/).
*/
case class Path private (name: String) extends AnyVal
case class Path private (name: String)

object Path {
implicit val schema: Schema[Path] = DeriveSchema.gen[Path]
Expand Down Expand Up @@ -445,21 +445,21 @@ object OpenAPI {
* path.
* @param description
* A description, intended to apply to all operations in this path.
* @param get
* @param getOp
* A definition of a GET operation on this path.
* @param put
* @param putOp
* A definition of a PUT operation on this path.
* @param post
* @param postOp
* A definition of a POST operation on this path.
* @param delete
* @param deleteOp
* A definition of a DELETE operation on this path.
* @param options
* @param optionsOp
* A definition of a OPTIONS operation on this path.
* @param head
* @param headOp
* A definition of a HEAD operation on this path.
* @param patch
* @param patchOp
* A definition of a PATCH operation on this path.
* @param trace
* @param traceOp
* A definition of a TRACE operation on this path.
* @param servers
* An alternative server List to service all operations in this path.
Expand All @@ -474,35 +474,35 @@ object OpenAPI {
@fieldName("$ref") ref: Option[String],
summary: Option[String],
description: Option[Doc],
get: Option[Operation],
put: Option[Operation],
post: Option[Operation],
delete: Option[Operation],
options: Option[Operation],
head: Option[Operation],
patch: Option[Operation],
trace: Option[Operation],
getOp: Option[Operation],
putOp: Option[Operation],
postOp: Option[Operation],
deleteOp: Option[Operation],
optionsOp: Option[Operation],
headOp: Option[Operation],
patchOp: Option[Operation],
traceOp: Option[Operation],
servers: List[Server] = List.empty,
parameters: Set[ReferenceOr[Parameter]] = Set.empty,
) {
def get(operation: Operation): PathItem = copy(get = Some(operation))
def put(operation: Operation): PathItem = copy(put = Some(operation))
def post(operation: Operation): PathItem = copy(post = Some(operation))
def delete(operation: Operation): PathItem = copy(delete = Some(operation))
def options(operation: Operation): PathItem = copy(options = Some(operation))
def head(operation: Operation): PathItem = copy(head = Some(operation))
def patch(operation: Operation): PathItem = copy(patch = Some(operation))
def trace(operation: Operation): PathItem = copy(trace = Some(operation))
def get(operation: Operation): PathItem = copy(getOp = Some(operation))
def put(operation: Operation): PathItem = copy(putOp = Some(operation))
def post(operation: Operation): PathItem = copy(postOp = Some(operation))
def delete(operation: Operation): PathItem = copy(deleteOp = Some(operation))
def options(operation: Operation): PathItem = copy(optionsOp = Some(operation))
def head(operation: Operation): PathItem = copy(headOp = Some(operation))
def patch(operation: Operation): PathItem = copy(patchOp = Some(operation))
def trace(operation: Operation): PathItem = copy(traceOp = Some(operation))
def any(operation: Operation): PathItem =
copy(
get = Some(operation),
put = Some(operation),
post = Some(operation),
delete = Some(operation),
options = Some(operation),
head = Some(operation),
patch = Some(operation),
trace = Some(operation),
getOp = Some(operation),
putOp = Some(operation),
postOp = Some(operation),
deleteOp = Some(operation),
optionsOp = Some(operation),
headOp = Some(operation),
patchOp = Some(operation),
traceOp = Some(operation),
)
}

Expand All @@ -514,14 +514,14 @@ object OpenAPI {
ref = None,
summary = None,
description = None,
get = None,
put = None,
post = None,
delete = None,
options = None,
head = None,
patch = None,
trace = None,
getOp = None,
putOp = None,
postOp = None,
deleteOp = None,
optionsOp = None,
headOp = None,
patchOp = None,
traceOp = None,
servers = List.empty,
parameters = Set.empty,
)
Expand Down Expand Up @@ -1094,13 +1094,13 @@ object OpenAPI {
implicit val schema: Schema[BooleanLiteral] =
Schema[Boolean].transform[BooleanLiteral](s => BooleanLiteral(s), p => p.value)
}
sealed abstract case class Expression private (value: String) extends LiteralOrExpression
case class Expression(value: String) extends LiteralOrExpression

object Expression {
implicit val schema: Schema[Expression] =
Schema[String].transform[Expression](s => Expression.create(s), p => p.value)

private[openapi] def create(value: String): Expression = new Expression(value) {}
private[openapi] def create(value: String): Expression = Expression(value)
}

// TODO: maybe one could make a regex to validate the expression. For now just accept anything
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -599,11 +599,11 @@ object OpenAPIGen {

def genDiscriminator(schema: Schema[_]): Option[OpenAPI.Discriminator] = {
schema match {
case enum: Schema.Enum[_] =>
case enumSchema: Schema.Enum[_] =>
val discriminatorName =
enum.annotations.collectFirst { case zio.schema.annotation.discriminatorName(name) => name }
val noDiscriminator = enum.annotations.contains(zio.schema.annotation.noDiscriminator())
val typeMapping = enum.cases.map { case_ =>
enumSchema.annotations.collectFirst { case zio.schema.annotation.discriminatorName(name) => name }
val noDiscriminator = enumSchema.annotations.contains(zio.schema.annotation.noDiscriminator())
val typeMapping = enumSchema.cases.map { case_ =>
val caseName =
case_.annotations.collectFirst { case zio.schema.annotation.caseName(name) => name }.getOrElse(case_.id)
// There should be no enums with cases that are not records with a nominal id
Expand Down Expand Up @@ -739,16 +739,16 @@ object OpenAPIGen {

def nominal(schema: Schema[_], referenceType: SchemaStyle): Option[String] =
schema match {
case enum: Schema.Enum[_] =>
enum.id match {
case enumSchema: Schema.Enum[_] =>
enumSchema.id match {
case TypeId.Structural =>
None
case nominal: TypeId.Nominal if referenceType == SchemaStyle.Compact =>
Some(nominal.typeName)
case nominal: TypeId.Nominal =>
Some(nominal.fullyQualified.replace(".", "_"))
}
case record: Record[_] =>
case record: Record[_] =>
record.id match {
case TypeId.Structural =>
None
Expand All @@ -757,7 +757,7 @@ object OpenAPIGen {
case nominal: TypeId.Nominal =>
Some(nominal.fullyQualified.replace(".", "_"))
}
case _ => None
case _ => None
}

private def responsesForAlternatives(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import zio.Scope
import zio.json.ast.Json
import zio.test._

import zio.schema.DeriveSchema.gen
import zio.schema.{DeriveSchema, Schema}

import zio.http.Method.GET
import zio.http._
Expand All @@ -14,10 +14,20 @@ import zio.http.endpoint._
object OpenAPIGenSpec extends ZIOSpecDefault {

final case class SimpleInputBody(name: String, age: Int)
implicit val simpleInputBodySchema: Schema[SimpleInputBody] =
DeriveSchema.gen[SimpleInputBody]
final case class OtherSimpleInputBody(fullName: String, shoeSize: Int)
implicit val otherSimpleInputBodySchema: Schema[OtherSimpleInputBody] =
DeriveSchema.gen[OtherSimpleInputBody]
final case class SimpleOutputBody(userName: String, score: Int)
implicit val simpleOutputBodySchema: Schema[SimpleOutputBody] =
DeriveSchema.gen[SimpleOutputBody]
final case class NotFoundError(message: String)
implicit val notFoundErrorSchema: Schema[NotFoundError] =
DeriveSchema.gen[NotFoundError]
final case class ImageMetadata(name: String, size: Int)
implicit val imageMetadataSchema: Schema[ImageMetadata] =
DeriveSchema.gen[ImageMetadata]

def minify(str: String): String =
Json.encoder.encodeJson(Json.decoder.decodeJson(str).toOption.get, None).toString
Expand Down

0 comments on commit f8581d1

Please sign in to comment.