Skip to content

References to external schemas only work when the schema title matches the file name #275

@Jacoby6000

Description

@Jacoby6000

The RefParser currently does not check the schema title when producing DefIds. Because of this, references to the root object of an external schema will fail if the external schema's title does not match its file name.

For example, the json schemas below

Nested.json:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "nested.json",
  "type": "object",
  "title": "foo",
  "additionalProperties": false,
  "properties": {
    "id": {
      "type": "string"
    }
  }
}

wrapper.json:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "wrapper.json",
  "type": "object",
  "title": "wrapper",
  "additionalProperties": false,
  "properties": {
    "data": {
      "$ref": "nested.json"
    }
  }
}

Produce the smithy below:

nested.smithy:

$version: "2.0"

namespace nested

structure Foo {
    id: String,
}

wrapper.smithy

$version: "2.0"

namespace wrapper

use nested#Nested

structure Wrapper {
    data: Nested
}

The wrapper.smithy file should be importing nested#Foo and using data: Foo, rather than nested#Nested.

Sample test case:

test("multiple files - referenced file schema title does not match file name") {
    val dir = os.temp.dir()
    val nestedFile = dir / "nested.json"
    val wrapperFile = dir / "wrapper.json"
    val nested = s"""|{
                     |  "$$schema": "http://json-schema.org/draft-07/schema#",
                     |  "$$id": "file:///$nestedFile",
                     |  "type": "object",
                     |  "title": "nested",
                     |  "additionalProperties": false,
                     |  "properties": {
                     |    "id": {
                     |      "type": "string"
                     |    }
                     |  }
                     |}""".stripMargin
    val wrapper = s"""|{
                      |  "$$schema": "http://json-schema.org/draft-07/schema#",
                      |  "$$id": "file:///$wrapperFile",
                      |  "type": "object",
                      |  "title": "Foo",
                      |  "additionalProperties": false,
                      |  "properties": {
                      |    "data": {
                      |      "$$ref": "nested.json"
                      |    }
                      |  }
                      |}""".stripMargin

    os.write.over(nestedFile, nested)
    os.write.over(wrapperFile, wrapper)

    val expectedNested = """|namespace nested
                            |
                            |structure Foo {
                            |    id: String,
                            |}
                            |""".stripMargin

    val expectedWrapper = """|namespace wrapper
                             |
                             |use nested#Foo
                             |
                             |structure Wrapper {
                             |    data: Foo
                             |}
                             |""".stripMargin

    val inOne = TestUtils.ConversionTestInput(
      NonEmptyList.of("nested.json"),
      nested,
      expectedNested
    )
    val inTwo = TestUtils.ConversionTestInput(
      NonEmptyList.of("wrapper.json"),
      wrapper,
      expectedWrapper
    )
    TestUtils.runConversionTest(inOne, inTwo)
  }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions