Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support definitions for older json schema versions #203

Merged
merged 2 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: String => Vector[Local] = { name =>
lewisjkl marked this conversation as resolved.
Show resolved Hide resolved
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)
}
}
Loading