Skip to content

Commit

Permalink
Fix zio#3103 Only last response is generated into Endpoint code (zio#…
Browse files Browse the repository at this point in the history
…3151)

* gen: failing test: code is only generated for last response

* Fix zio#3103 Only last response is generated into Endpoint code
  • Loading branch information
nafg authored Sep 19, 2024
1 parent 087ee94 commit 635603e
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ final case class EndpointGen(config: Config) {

val (outImports: Iterable[List[Code.Import]], outCodes: Iterable[Code.OutCode]) =
// TODO: ignore default for now. Not sure how to handle it
op.responses.collect {
op.responses.toSeq.collect {
case (OpenAPI.StatusOrDefault.StatusValue(status), OpenAPI.ReferenceOr.Reference(ResponseRef(key), _, _)) =>
val response = resolveResponseRef(openAPI, key)
val (imports, code) =
Expand Down
68 changes: 68 additions & 0 deletions zio-http-gen/src/test/scala/zio/http/gen/scala/CodeGenSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package zio.http.gen.scala
import java.nio.file._

import scala.annotation.nowarn
import scala.collection.immutable.ListMap
import scala.jdk.CollectionConverters._
import scala.meta._
import scala.meta.parsers._
Expand Down Expand Up @@ -975,5 +976,72 @@ object CodeGenSpec extends ZIOSpecDefault {
}
}
} @@ TestAspect.exceptScala3,
test("Generate all responses") {
val oapi =
OpenAPI(
openapi = "3.0.0",
info = OpenAPI.Info(
title = "XXX",
description = None,
termsOfService = None,
contact = None,
license = None,
version = "1.0.0",
),
paths = ListMap(
OpenAPI.Path
.fromString(name = "/api/a/b")
.map { path =>
path -> OpenAPI.PathItem(
ref = None,
summary = None,
description = None,
get = None,
put = None,
post = Some(
OpenAPI.Operation(
summary = None,
description = None,
externalDocs = None,
operationId = None,
requestBody = None,
responses = Map(
OpenAPI.StatusOrDefault.StatusValue(status = Status.Ok) ->
OpenAPI.ReferenceOr.Or(value = OpenAPI.Response()),
OpenAPI.StatusOrDefault.StatusValue(Status.BadRequest) ->
OpenAPI.ReferenceOr.Or(OpenAPI.Response()),
OpenAPI.StatusOrDefault.StatusValue(Status.Unauthorized) ->
OpenAPI.ReferenceOr.Or(OpenAPI.Response()),
),
),
),
delete = None,
options = None,
head = None,
patch = None,
trace = None,
)
}
.toSeq: _*,
),
components = None,
externalDocs = None,
)

val maybeEndpointCode =
EndpointGen
.fromOpenAPI(oapi, Config.default)
.files
.flatMap(_.objects)
.flatMap(_.endpoints)
.collectFirst {
case (field, code) if field.name == "post" => code
}

assertTrue(
maybeEndpointCode.is(_.some).outCodes.length == 1 &&
maybeEndpointCode.is(_.some).errorsCode.length == 2,
)
},
) @@ java11OrNewer @@ flaky @@ blocking // Downloading scalafmt on CI is flaky
}

0 comments on commit 635603e

Please sign in to comment.