Skip to content

Commit

Permalink
better condition logic now properly handles correctly these cases:
Browse files Browse the repository at this point in the history
  extra_attributes:
    type: object
    additionalProperties: true

  extra_attributes:
    type: object
    additionalProperties: {}

  extra_attributes:
    additionalProperties: true

but fails for this case, which might not be an issue (or an issue with zio-schema):

  extra_attributes:
    additionalProperties: {}
  • Loading branch information
hochgi committed Nov 29, 2024
1 parent 6966af7 commit afe0bce
Showing 1 changed file with 20 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -255,14 +255,29 @@ object JsonSchema {

private[openapi] def fromSerializableSchema(schema: SerializableJsonSchema): JsonSchema = {

val definedAttributesCount = schema.productIterator.count(_.asInstanceOf[Option[_]].isDefined)

// if type: object with additionalProperties defined,
// but nothing else, we should assume a free form object
def anyObject: Boolean = schema.additionalProperties.collect { case BoolOrSchema.BooleanWrapper(true) =>
schema.schemaType.contains(TypeOrTypes.Type("object")) &&
schema.productIterator.count(_.asInstanceOf[Option[_]].isDefined) == 2
}.exists(identity)
// if type is not defined, but additionalProperties is,
// and nothing else, object is assumed again.
// if both type: object and additionalProperties are defined,
// and nothing else, object is assumed.
def anyObject: Boolean = {
val isObject = schema.schemaType.contains(TypeOrTypes.Type("object"))
val hasAttrs = schema.additionalProperties.collect { case BoolOrSchema.BooleanWrapper(b) =>
b
}.exists(identity)

// if definedAttributesCount == 0, this also yields true,
// but we check for it before calling this function,
// thus no need to check it here.
val isAnyObj = List(isObject, hasAttrs).count(identity) == definedAttributesCount

isAnyObj
}

if (schema.productIterator.forall(_.asInstanceOf[Option[_]].isEmpty)) JsonSchema.AnyJson
if (definedAttributesCount == 0) JsonSchema.AnyJson
else if (anyObject) JsonSchema.Object(Map.empty, Right(JsonSchema.AnyJson), Chunk.empty)
else {
val additionalProperties = schema.additionalProperties match {
Expand Down

0 comments on commit afe0bce

Please sign in to comment.