Skip to content

Commit

Permalink
Fix some anomaly checking with additionalProperties
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelmior committed Mar 6, 2024
1 parent 9595637 commit a8eb619
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
### Fixed
- Fix some anomaly checking with `patternProperties`
- Fix some anomaly checking with `additionalProperties`

## [0.20.1]
### Added
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package io.github.dataunitylab.jsonoid.discovery
package schemas

import scala.collection.mutable.HashMap
import scala.language.existentials
import scala.reflect._
import scala.util.matching.Regex

Expand Down Expand Up @@ -33,16 +34,6 @@ object ObjectSchema {
implicit val formats: Formats = DefaultFormats
val props = SchemaProperties.empty[Map[String, JsonSchema[_]]]

(obj \ "additionalProperties") match {
case JNothing =>
case JBool(additionalProperties) =>
props.add(AdditionalPropertiesProperty(Some(additionalProperties)))
case _ =>
throw new UnsupportedOperationException(
"additionalProperties with non-Boolean schema not supported"
)
}

if ((obj \ "not") =/= JNothing) {
throw new UnsupportedOperationException("not isn't supported")
}
Expand Down Expand Up @@ -119,7 +110,22 @@ object ObjectSchema {
}
val reqProp = RequiredProperty(Some(required))

props.add(ObjectTypesProperty(objTypes))
(obj \ "additionalProperties") match {
case JNothing =>
case JBool(additionalProperties) =>
props.add(AdditionalPropertiesProperty(Some(additionalProperties)))
case JObject(obj) if obj.isEmpty && patternTypes.isEmpty =>
val patternTypes = Map(".*".r -> JsonSchema.fromJson(obj))
props.add(PatternTypesProperty(patternTypes))
case _ =>
throw new UnsupportedOperationException(
"additionalProperties with non-Boolean schema not supported"
)
}

if (!objTypes.isEmpty) {
props.add(ObjectTypesProperty(objTypes))
}
if (!patternTypes.isEmpty) {
props.add(PatternTypesProperty(patternTypes))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ class JsonSchemaSpec extends UnitSpec {

val convertedSchema = JsonSchema.fromJson(objSchema)

convertedSchema.properties should contain(ObjectTypesProperty(Map()))
convertedSchema shouldBe an[ObjectSchema]
}

it should "convert an object schema with missing type" in {
Expand Down

0 comments on commit a8eb619

Please sign in to comment.