Skip to content

Commit

Permalink
Merge pull request #141 from benoitlouy/json-schema-additional-proper…
Browse files Browse the repository at this point in the history
…ty-freeform

JSON schema object with only additional properties should translate to freeform primitive
  • Loading branch information
daddykotex committed Jun 2, 2023
2 parents 7ecdd62 + 5f094bc commit 9168ae6
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 1 deletion.
10 changes: 10 additions & 0 deletions modules/json-schema/src/internals/Extractors.scala
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,16 @@ object Extractors {
val hints = desc.toList
Some(hints -> PBoolean)

// M:
// type: object
// additionalProperties: true
case (obj: ObjectSchema)
if obj.getPropertySchemas().size() == 0 &&
obj.permitsAdditionalProperties() &&
CaseMap.unapply(obj).isEmpty =>
val genericHints = getGenericHints(obj)
Some(genericHints -> PFreeForm)

case _ => None
}
specific.map(_.leftMap(_ ++ genericHints))
Expand Down
61 changes: 61 additions & 0 deletions modules/json-schema/tests/src/PrimitiveSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/* Copyright 2022 Disney Streaming
*
* Licensed under the Tomorrow Open Source Technology License, Version 1.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://disneystreaming.github.io/TOST-1.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package smithytranslate.json_schema

final class PrimitiveSpec extends munit.FunSuite {

test("freeform document") {
val jsonSchString = """|{
| "$id": "test.json",
| "$schema": "http://json-schema.org/draft-07/schema#",
| "title": "FreeForm",
| "type": "object",
| "additionalProperties": true
|}
|""".stripMargin

val expectedString = """|namespace foo
|
|document FreeForm
|""".stripMargin

TestUtils.runConversionTest(jsonSchString, expectedString)
}
test("freeform document nested") {
val jsonSchString = """|{
| "$id": "test.json",
| "$schema": "http://json-schema.org/draft-07/schema#",
| "title": "FreeFormWrapper",
| "type": "object",
| "properties": {
| "freeform": {
| "type": "object",
| "additionalProperties": true
| }
| }
|}
|""".stripMargin

val expectedString = """|namespace foo
|
|structure FreeFormWrapper {
| freeform: Document
|}
|""".stripMargin

TestUtils.runConversionTest(jsonSchString, expectedString)
}
}
8 changes: 7 additions & 1 deletion modules/json-schema/tests/src/RefSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,12 @@ final class RefSpec extends munit.FunSuite {
| },
| "$defs":{
| "bar": {
| "type": "object"
| "type": "object",
| "properties": {
| "baz": {
| "type": "string"
| }
| }
| }
| }
|}
Expand All @@ -115,6 +120,7 @@ final class RefSpec extends munit.FunSuite {
|}
|
|structure Bar {
| baz: String
|}
|""".stripMargin

Expand Down
20 changes: 20 additions & 0 deletions modules/json-schema/tests/src/StructureSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,26 @@ final class StructureSpec extends munit.FunSuite {
TestUtils.runConversionTest(jsonSchString, expectedString)
}

test("structures - constant") {
val jsonSchString = """|{
| "$id": "test.json",
| "$schema": "http://json-schema.org/draft-07/schema#",
| "title": "Constant",
| "type": "object",
| "additionalProperties": false
|}
|""".stripMargin

val expectedString = """|namespace foo
|
|structure Constant {
|}
|""".stripMargin

TestUtils.runConversionTest(jsonSchString, expectedString)

}

test("structures - nested") {
val jsonSchString = """|{
| "$id": "test.json",
Expand Down

0 comments on commit 9168ae6

Please sign in to comment.