Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #3103 Only last response is generated into Endpoint code #3151

Merged
merged 2 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
}
Loading