Skip to content

Commit

Permalink
Fixes #3246 - swapped validation annotations minLength and maxLength …
Browse files Browse the repository at this point in the history
…in code generated from OpenAPI
  • Loading branch information
gregor-rayman committed Dec 10, 2024
1 parent 1f8ef1f commit 3728252
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package test.api.v1

import test.component._
import zio.schema._

object Entries {
import zio.http._
import zio.http.endpoint._
import zio.http.codec._
val post = Endpoint(Method.POST / "api" / "v1" / "entries")
.in[POST.RequestBody]
.out[POST.ResponseBody](status = Status.Ok)

object POST {
import zio.schema.annotation.validate
import zio.schema.validation.Validation

case class RequestBody(
id: Int,
@validate[String](Validation.maxLength(255) && Validation.minLength(1)) name: String,
)
object RequestBody {
implicit val codec: Schema[RequestBody] = DeriveSchema.gen[RequestBody]
}
case class ResponseBody(
id: Int,
@validate[String](Validation.maxLength(255) && Validation.minLength(1)) name: String,
)
object ResponseBody {
implicit val codec: Schema[ResponseBody] = DeriveSchema.gen[ResponseBody]
}
}
}
2 changes: 1 addition & 1 deletion zio-http-gen/src/test/resources/ValidatedData.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import zio.schema.annotation.validate
import zio.schema.validation.Validation

case class ValidatedData(
@validate[String](Validation.minLength(10)) name: String,
@validate[String](Validation.maxLength(10)) name: String,
@validate[Int](Validation.greaterThan(0) && Validation.lessThan(100)) age: Int,
)
object ValidatedData {
Expand Down
82 changes: 82 additions & 0 deletions zio-http-gen/src/test/resources/inline_schema_minmaxlength.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
{
"openapi" : "3.1.0",
"info" : {
"title" : "",
"version" : ""
},
"paths" : {
"/api/v1/entries" : {
"post" : {
"requestBody" :
{
"content" : {
"application/json" : {
"schema" :
{
"type" :
"object",
"properties" : {
"id" : {
"type" :
"integer",
"format" : "int32"
},
"name" : {
"type" :
"string",
"minLength" : 1,
"maxLength" : 255
}
},
"additionalProperties" :
true,
"required" : [
"id",
"name"
]
}

}
},
"required" : true
},
"responses" : {
"200" :
{
"description" : "",
"content" : {
"application/json" : {
"schema" :
{
"type" :
"object",
"properties" : {
"id" : {
"type" :
"integer",
"format" : "int32"
},
"name" : {
"type" :
"string",
"minLength" : 1,
"maxLength" : 255
}
},
"additionalProperties" :
true,
"required" : [
"id",
"name"
]
}

}
}
}
},
"deprecated" : false
}
}
}
}
10 changes: 10 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 @@ -198,6 +198,16 @@ object CodeGenSpec extends ZIOSpecDefault {
}
}
} @@ TestAspect.exceptScala3, // for some reason, the temp dir is empty in Scala 3
test("OpenAPI spec with inline schema request and response body with minLength and maxLength") {
val openAPIString = stringFromResource("/inline_schema_minmaxlength.json")

openApiFromJsonString(openAPIString) { openAPI =>
codeGenFromOpenAPI(openAPI) { testDir =>
fileShouldBe(testDir, "api/v1/Entries.scala", "/EndpointWithRequestResponseBodyInlineMinMaxLength.scala")
}
}
} @@ TestAspect.exceptScala3, // for some reason, the temp dir is empty in Scala 3

test("OpenAPI spec with inline schema request and response body, with nested object schema") {
val openAPIString = stringFromResource("/inline_schema_nested.json")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,8 +293,8 @@ object JsonSchema {
JsonSchema.String(
schema.format.map(StringFormat.fromString),
schema.pattern.map(Pattern.apply),
schema.minLength,
schema.maxLength,
schema.minLength,
)
case schema if schema.schemaType.contains(TypeOrTypes.Type("boolean")) =>
JsonSchema.Boolean
Expand Down

0 comments on commit 3728252

Please sign in to comment.