Skip to content

Commit

Permalink
Merge pull request #203 from disneystreaming/definitions-support
Browse files Browse the repository at this point in the history
Support definitions for older json schema versions
  • Loading branch information
daddykotex authored Oct 19, 2023
2 parents e357638 + 7fb56c1 commit a2c948c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
10 changes: 5 additions & 5 deletions modules/json-schema/src/internals/JsonSchemaToIModel.scala
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ private class JsonSchemaToIModel[F[_]: Parallel: TellShape: TellError](
val schemaName = Name(schemaNameSegment)

// Computing schemas under the $defs field, if it exists.
def $defSchemas = {
def $defSchemas(name: String): Vector[Local] = {
val defsObject = rawJson.asObject
.flatMap(_.apply("$defs"))
.flatMap(_.apply(name))
.flatMap(_.asObject)

val allDefs = defsObject.toVector
Expand All @@ -92,14 +92,14 @@ private class JsonSchemaToIModel[F[_]: Parallel: TellShape: TellError](
allDefs
.flatMap { case (key, value) =>
val topLevelJson = Json.fromJsonObject {
value.add("$defs", Json.fromJsonObject(defsObject.get))
value.add(name, Json.fromJsonObject(defsObject.get))
}

val defSchema = LoadSchema(new JSONObject(topLevelJson.noSpaces))

val defSchemaName =
Name(
Segment.Arbitrary(CIString("$defs")),
Segment.Arbitrary(CIString(name)),
Segment.Derived(CIString(key))
)
Vector(
Expand All @@ -113,7 +113,7 @@ private class JsonSchemaToIModel[F[_]: Parallel: TellShape: TellError](
val topLevelLocal =
Local(schemaName, jsonSchema, rawJson).addHints(List(Hint.TopLevel))

topLevelLocal +: $defSchemas
topLevelLocal +: ($defSchemas("$defs") ++ $defSchemas("definitions"))
}

/** Refolds the schema, aggregating found definitions in Tell.
Expand Down
34 changes: 34 additions & 0 deletions modules/json-schema/tests/src/RefSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -288,4 +288,38 @@ final class RefSpec extends munit.FunSuite {

TestUtils.runConversionTest(jsonSchString, expectedString)
}

test("schema with definitions rather than $defs") {
val jsonSchString = """|{
| "$schema": "http://json-schema.org/draft-07/schema#",
| "title": "Person",
| "type": "object",
| "properties": {
| "firstName": {
| "$ref": "#/definitions/name"
| },
| "lastName": {
| "$ref": "#/definitions/name"
| }
| },
| "definitions":{
| "name": {
| "type": "string"
| }
| }
|}
|""".stripMargin

val expectedString = """|namespace foo
|
|structure Person {
| firstName: Name,
| lastName: Name
|}
|
|string Name
|""".stripMargin

TestUtils.runConversionTest(jsonSchString, expectedString)
}
}

0 comments on commit a2c948c

Please sign in to comment.