Skip to content

Commit

Permalink
Merge pull request #202 from disneystreaming/allow-no-id
Browse files Browse the repository at this point in the history
JSON Schema Optional id Field
  • Loading branch information
lewisjkl committed Oct 18, 2023
2 parents 601ee71 + 97219a6 commit e357638
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 6 deletions.
13 changes: 8 additions & 5 deletions modules/json-schema/src/internals/Extractors.scala
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ object Extractors {
/*
* The most complicated thing
*/
abstract class JsonSchemaCaseRefBuilder(id: String, ns: Path)
abstract class JsonSchemaCaseRefBuilder(id: Option[String], ns: Path)
extends smithytranslate.openapi.internals.CaseRefBuilder(ns) {
def unapply(sch: Schema): Option[Either[ModelError, DefId]] = sch match {
case ref: ReferenceSchema =>
Expand All @@ -260,11 +260,14 @@ object Extractors {
// The number of `/` chars is not always consistent between the id and the refValue.
val fileRegex = "^file:\\/*"
val refValueNoPrefix = refValue.replaceFirst(fileRegex, "")
val idNoPrefix = id.replaceFirst(fileRegex, "")
val sanitisedRefValue =
if (refValueNoPrefix.startsWith(idNoPrefix))
refValueNoPrefix.drop(idNoPrefix.length())
else refValue
id.map(_.replaceFirst(fileRegex, ""))
.collectFirst {
case idNoPrefix if (refValueNoPrefix.startsWith(idNoPrefix)) =>
refValueNoPrefix.drop(idNoPrefix.length())
}
.getOrElse(refValue)

Option(sanitisedRefValue).map(this.apply)
case _ => None
}
Expand Down
5 changes: 4 additions & 1 deletion modules/json-schema/src/internals/JsonSchemaToIModel.scala
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,10 @@ private class JsonSchemaToIModel[F[_]: Parallel: TellShape: TellError](
implicit val F: Monad[F] = Parallel[F].monad

private val CaseRef =
new Extractors.JsonSchemaCaseRefBuilder(jsonSchema.getId(), namespace) {}
new Extractors.JsonSchemaCaseRefBuilder(
Option(jsonSchema.getId()),
namespace
) {}

private val allSchemas: Vector[Local] = {
val schemaNameSegment =
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 @@ -254,4 +254,38 @@ final class RefSpec extends munit.FunSuite {

TestUtils.runConversionTest(jsonSchString, expectedString)
}

test("schema with no id") {
val jsonSchString = """|{
| "$schema": "http://json-schema.org/draft-07/schema#",
| "title": "Person",
| "type": "object",
| "properties": {
| "firstName": {
| "$ref": "#/$defs/name"
| },
| "lastName": {
| "$ref": "#/$defs/name"
| }
| },
| "$defs":{
| "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 e357638

Please sign in to comment.