Skip to content

Commit

Permalink
Fix OpenAPI example extraction (#2635)
Browse files Browse the repository at this point in the history
* Deconstruct tupled examples (#2609)

* Deconstruct either examples (#2609)

* Handle path examples and add/fix tests (#2609)
  • Loading branch information
987Nabil authored Jan 21, 2024
1 parent 49d4aa4 commit 6930757
Show file tree
Hide file tree
Showing 13 changed files with 554 additions and 266 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
package zio.http.endpoint.cli

import scala.util.Try

import zio.cli._

import zio.schema._

import zio.http._
import zio.http.codec.HttpCodec.Metadata
import zio.http.codec._
import zio.http.codec.internal._
import zio.http.endpoint._

/**
Expand Down Expand Up @@ -90,36 +82,34 @@ private[cli] object CliEndpoint {

def fromCodec[Input](input: HttpCodec[_, Input]): CliEndpoint = {
input match {
case atom: HttpCodec.Atom[_, _] => fromAtom(atom)
case HttpCodec.TransformOrFail(api, _, _) => fromCodec(api)
case HttpCodec.Annotated(in, metadata) =>
case atom: HttpCodec.Atom[_, _] => fromAtom(atom)
case HttpCodec.TransformOrFail(api, _, _) => fromCodec(api)
case HttpCodec.Annotated(in, metadata) =>
metadata match {
case HttpCodec.Metadata.Documented(doc) => fromCodec(in) describeOptions doc
case _ => fromCodec(in)
}
case HttpCodec.Fallback(left, right, _) => fromCodec(left) ++ fromCodec(right)
case HttpCodec.Combine(left, right, _) => fromCodec(left) ++ fromCodec(right)
case _ => CliEndpoint.empty
case HttpCodec.Fallback(left, right, _, _) => fromCodec(left) ++ fromCodec(right)
case HttpCodec.Combine(left, right, _) => fromCodec(left) ++ fromCodec(right)
case _ => CliEndpoint.empty
}
}

private def fromAtom[Input](input: HttpCodec.Atom[_, Input]): CliEndpoint = {
input match {
case HttpCodec.Content(schema, mediaType, nameOption, _) => {
case HttpCodec.Content(schema, mediaType, nameOption, _) =>
val name = nameOption match {
case Some(x) => x
case None => ""
}
CliEndpoint(body = HttpOptions.Body(name, mediaType, schema) :: List())
}

case HttpCodec.ContentStream(schema, mediaType, nameOption, _) => {
case HttpCodec.ContentStream(schema, mediaType, nameOption, _) =>
val name = nameOption match {
case Some(x) => x
case None => ""
}
CliEndpoint(body = HttpOptions.Body(name, mediaType, schema) :: List())
}

case HttpCodec.Header(name, textCodec, _) =>
CliEndpoint(headers = HttpOptions.Header(name, textCodec) :: List())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -312,10 +312,9 @@ private[cli] object HttpOptions {
}

private[cli] def optionsFromSegment(segment: SegmentCodec[_]): Options[String] = {
@tailrec
def fromSegment[A](segment: SegmentCodec[A]): Options[String] =
segment match {
case SegmentCodec.UUID(name) =>
case SegmentCodec.UUID(name) =>
Options
.text(name)
.mapOrFail(str =>
Expand All @@ -327,14 +326,13 @@ private[cli] object HttpOptions {
},
)
.map(_.toString)
case SegmentCodec.Text(name) => Options.text(name)
case SegmentCodec.IntSeg(name) => Options.integer(name).map(_.toInt).map(_.toString)
case SegmentCodec.LongSeg(name) => Options.integer(name).map(_.toInt).map(_.toString)
case SegmentCodec.BoolSeg(name) => Options.boolean(name).map(_.toString)
case SegmentCodec.Literal(value) => Options.Empty.map(_ => value)
case SegmentCodec.Trailing => Options.none.map(_.toString)
case SegmentCodec.Empty => Options.none.map(_.toString)
case SegmentCodec.Annotated(codec, _) => fromSegment(codec)
case SegmentCodec.Text(name) => Options.text(name)
case SegmentCodec.IntSeg(name) => Options.integer(name).map(_.toInt).map(_.toString)
case SegmentCodec.LongSeg(name) => Options.integer(name).map(_.toInt).map(_.toString)
case SegmentCodec.BoolSeg(name) => Options.boolean(name).map(_.toString)
case SegmentCodec.Literal(value) => Options.Empty.map(_ => value)
case SegmentCodec.Trailing => Options.none.map(_.toString)
case SegmentCodec.Empty => Options.none.map(_.toString)
}

fromSegment(segment)
Expand Down
18 changes: 8 additions & 10 deletions zio-http-cli/src/test/scala/zio/http/endpoint/cli/CommandGen.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,16 @@ import zio.http.endpoint.cli.EndpointGen._
object CommandGen {

def getSegment(segment: SegmentCodec[_]): (String, String) = {
@tailrec
def fromSegment[A](segment: SegmentCodec[A]): (String, String) =
segment match {
case SegmentCodec.UUID(name) => (name, "text")
case SegmentCodec.Text(name) => (name, "text")
case SegmentCodec.IntSeg(name) => (name, "integer")
case SegmentCodec.LongSeg(name) => (name, "integer")
case SegmentCodec.BoolSeg(name) => (name, "boolean")
case SegmentCodec.Literal(_) => ("", "")
case SegmentCodec.Trailing => ("", "")
case SegmentCodec.Empty => ("", "")
case SegmentCodec.Annotated(codec, _) => fromSegment(codec)
case SegmentCodec.UUID(name) => (name, "text")
case SegmentCodec.Text(name) => (name, "text")
case SegmentCodec.IntSeg(name) => (name, "integer")
case SegmentCodec.LongSeg(name) => (name, "integer")
case SegmentCodec.BoolSeg(name) => (name, "boolean")
case SegmentCodec.Literal(_) => ("", "")
case SegmentCodec.Trailing => ("", "")
case SegmentCodec.Empty => ("", "")
}

fromSegment(segment)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
},
"responses": {
"200": {
"description": "",
"content": {
"text/plain": {
"schema": {
Expand All @@ -44,7 +43,6 @@
},
"responses": {
"201": {
"description": "",
"content": {
"text/plain": {
"schema": {
Expand Down
Loading

0 comments on commit 6930757

Please sign in to comment.