|
| 1 | +# JSON Schema |
| 2 | + |
| 3 | +### CLI Usage |
| 4 | + |
| 5 | +``` |
| 6 | +> smithytranslate json-schema-to-smithy --help |
| 7 | +
|
| 8 | +Usage: smithytranslate json-schema-to-smithy --input <path> [--input <path>]... [--verboseNames] [--failOnValidationErrors] [--useEnumTraitSyntax] [--outputJson] <directory> |
| 9 | +
|
| 10 | +Take Json Schema specs as input and produce Smithy files as output. |
| 11 | +
|
| 12 | +Options and flags: |
| 13 | + --help |
| 14 | + Display this help text. |
| 15 | + --input <path>, -i <path> |
| 16 | + input source files |
| 17 | + --verbose-names |
| 18 | + If set, names of shapes not be simplified and will be as verbose as possible |
| 19 | + --validate-input |
| 20 | + If set, abort the conversion if any input specs contains a validation error |
| 21 | + --validate-output |
| 22 | + If set, abort the conversion if any produced smithy spec contains a validation error |
| 23 | + --enum-trait-syntax |
| 24 | + output enum types with the smithy v1 enum trait (deprecated) syntax |
| 25 | + --json-output |
| 26 | + changes output format to be json representations of the smithy models |
| 27 | +``` |
| 28 | + |
| 29 | +Run `smithytranslate json-schema-to-smithy --help` for all usage information. |
| 30 | + |
| 31 | +### Differences from OpenAPI |
| 32 | + |
| 33 | +Most of the functionality of the `OpenAPI => Smithy` conversion is the same for the `JSON Schema => Smithy` one. As such, here |
| 34 | +we will outline any differences that exist. Everything else is the same. See [OpenAPI docs](openapi.md) for more information. |
| 35 | + |
| 36 | +#### Default Values |
| 37 | + |
| 38 | +Default values from JSON Schema will be captured in the `smithy.api#default` trait. |
| 39 | + |
| 40 | +JSON Schema: |
| 41 | +```json |
| 42 | +{ |
| 43 | + "$id": "test.json", |
| 44 | + "$schema": "http://json-schema.org/draft-07/schema#", |
| 45 | + "title": "Person", |
| 46 | + "type": "object", |
| 47 | + "properties": { |
| 48 | + "firstName": { |
| 49 | + "type": "string", |
| 50 | + "default": "Sally" |
| 51 | + } |
| 52 | + } |
| 53 | +} |
| 54 | +``` |
| 55 | + |
| 56 | +Smithy: |
| 57 | +```smithy |
| 58 | +structure Person { |
| 59 | + @default("Sally") |
| 60 | + firstName: String |
| 61 | +} |
| 62 | +``` |
| 63 | + |
| 64 | +#### Null Values |
| 65 | + |
| 66 | +JSON Schemas allows for declaring types such as `["string", "null"]`. This type declaration |
| 67 | +on a required field means that the value cannot be omitted from the `JSON` payload entirely, |
| 68 | +but may be set to `null`. For example: |
| 69 | + |
| 70 | +JSON Schema: |
| 71 | +```json |
| 72 | +{ |
| 73 | + "$id": "test.json", |
| 74 | + "$schema": "http://json-schema.org/draft-07/schema#", |
| 75 | + "title": "Foo", |
| 76 | + "type": "object", |
| 77 | + "properties": { |
| 78 | + "bar": { |
| 79 | + "type": ["string", "null"] |
| 80 | + } |
| 81 | + }, |
| 82 | + "required": ["bar"] |
| 83 | +} |
| 84 | +``` |
| 85 | + |
| 86 | +Smithy: |
| 87 | +```smithy |
| 88 | +use alloy#nullable |
| 89 | +
|
| 90 | +structure Foo { |
| 91 | + @required |
| 92 | + @nullable |
| 93 | + bar: String |
| 94 | +} |
| 95 | +``` |
| 96 | + |
| 97 | +In most protocols, there is likely no difference between an optional field and a nullable optional field. |
| 98 | +Similarly, some protocols may not allow for required fields to be nullable. These considerations are left |
| 99 | +up to the protocol itself. |
| 100 | + |
| 101 | +#### Maps |
| 102 | + |
| 103 | +JSON Schema doesn't provide a first-class type for defining maps. As such, we translate a commonly-used |
| 104 | +convention into map types when encountered. When `patternProperties` is set to have a single entry, `.*`, |
| 105 | +we translate that to a smithy map type. |
| 106 | + |
| 107 | +JSON Schema: |
| 108 | +```json |
| 109 | +{ |
| 110 | + "$id": "test.json", |
| 111 | + "$schema": "http://json-schema.org/draft-07/schema#", |
| 112 | + "title": "TestMap", |
| 113 | + "type": "object", |
| 114 | + "patternProperties": { |
| 115 | + ".*": { |
| 116 | + "type": "string" |
| 117 | + } |
| 118 | + } |
| 119 | +} |
| 120 | +``` |
| 121 | + |
| 122 | +Smithy: |
| 123 | +```smithy |
| 124 | +map TestMap { |
| 125 | + key: String, |
| 126 | + value: String |
| 127 | +} |
| 128 | +``` |
0 commit comments