Skip to content

Commit

Permalink
Review: Remove optional fields from the required ones
Browse files Browse the repository at this point in the history
  • Loading branch information
guizmaii committed Aug 26, 2024
1 parent 751bd12 commit 335492a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2839,7 +2839,6 @@ object OpenAPIGenSpec extends ZIOSpecDefault {
| }
| },
| "required" : [
| "nestedOption",
| "nestedList",
| "nestedMap",
| "nestedSet",
Expand Down Expand Up @@ -3067,10 +3066,7 @@ object OpenAPIGenSpec extends ZIOSpecDefault {
| { "$ref": "#/components/schemas/SealedTraitCustomDiscriminator" }
| ]
| }
| },
| "required" : [
| "optionalAdtField"
| ]
| }
| },
| "Two" :
| {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
package zio.http.endpoint.openapi

import scala.annotation.{nowarn, tailrec}

import zio._
import zio.http.codec.{SegmentCodec, TextCodec}
import zio.http.endpoint.openapi.JsonSchema.MetaData
import zio.json.ast.Json

import zio.schema.Schema.CaseClass0
import zio.schema._
import zio.schema.annotation._
import zio.schema.codec._
import zio.schema.codec.json._
import zio.schema.validation._

import zio.http.codec.{SegmentCodec, TextCodec}
import scala.annotation.{nowarn, tailrec}

@nowarn("msg=possible missing interpolator")
private[openapi] case class SerializableJsonSchema(
Expand Down Expand Up @@ -161,6 +160,12 @@ sealed trait JsonSchema extends Product with Serializable { self =>
case _ => Chunk.empty
}

final def isNullable: Boolean =
annotations.exists {
case MetaData.Nullable(nullable) => nullable
case _ => false
}

def withoutAnnotations: JsonSchema = self match {
case JsonSchema.AnnotatedSchema(schema, _) => schema.withoutAnnotations
case _ => self
Expand Down Expand Up @@ -874,9 +879,7 @@ object JsonSchema {
obj <- objects
otherObj <- objects
notNullableSchemas = obj.withoutAnnotations.asInstanceOf[JsonSchema.Object].properties.collect {
case (name, schema)
if !schema.annotations.exists { case MetaData.Nullable(nullable) => nullable; case _ => false } =>
name -> schema
case (name, schema) if !schema.isNullable => name -> schema
}
if notNullableSchemas == otherObj.withoutAnnotations.asInstanceOf[JsonSchema.Object].properties
} yield otherObj).distinct
Expand All @@ -885,9 +888,7 @@ object JsonSchema {
val annotations = obj.annotations
val asObject = obj.withoutAnnotations.asInstanceOf[JsonSchema.Object]
val notNullableSchemas = asObject.properties.collect {
case (name, schema)
if !schema.annotations.exists { case MetaData.Nullable(nullable) => nullable; case _ => false } =>
name -> schema
case (name, schema) if !schema.isNullable => name -> schema
}
asObject.required(asObject.required.filter(notNullableSchemas.contains)).annotate(annotations)
}
Expand Down Expand Up @@ -1278,11 +1279,20 @@ object JsonSchema {
case Left(false) => Some(BoolOrSchema.BooleanWrapper(false))
case Right(schema) => Some(BoolOrSchema.SchemaWrapper(schema.toSerializableSchema))
}

val nullableFields = properties.collect { case (name, schema) if schema.isNullable => name }.toSet

SerializableJsonSchema(
schemaType = Some(TypeOrTypes.Type("object")),
properties = Some(properties.map { case (name, schema) => name -> schema.toSerializableSchema }),
additionalProperties = additionalProperties,
required = if (required.isEmpty) None else Some(required),
required =
if (required.isEmpty) None
else if (nullableFields.isEmpty) Some(required)
else {
val newRequired = required.filterNot(nullableFields.contains)
if (newRequired.isEmpty) None else Some(newRequired)
},
)
}
}
Expand Down

2 comments on commit 335492a

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀 : Performance Benchmarks (SimpleEffectBenchmarkServer)

concurrency: 256
requests/sec: 335172

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀 : Performance Benchmarks (PlainTextBenchmarkServer)

concurrency: 256
requests/sec: 329216

Please sign in to comment.