Skip to content

Commit 717e33b

Browse files
Updates json_schema dependency to 0.2.0
Given that `json_schema` has breaking changes in the way it handles references, such that both local and global references are now handled with the `URI` type, this has required minor changes in all of the printer modules, and as a result I've decided to bump the minor version of json-schema-to-elm given the scope of affected files. However, this change should make things easier going forward when implementing new feature.
1 parent bb37100 commit 717e33b

25 files changed

+236
-365
lines changed

.tool-versions

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
erlang 21.0
2-
elixir 1.7.3-otp-21
2+
elixir 1.8.1

README.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,18 @@ JSON schema types are described in the `lib/types` folder.
3737
If we supply `js2e` with the following JSON schema file, `definitions.json`:
3838
``` javascript
3939
{
40-
"$schema": "http://json-schema.org/draft-04/schema#",
40+
"$schema": "http://json-schema.org/draft-07/schema#",
4141
"title": "Definitions",
42-
"id": "http://example.com/definitions.json",
42+
"$id": "http://example.com/definitions.json",
4343
"description": "Schema for common types",
4444
"definitions": {
4545
"color": {
46-
"id": "#color",
46+
"$id": "#color",
4747
"type": "string",
4848
"enum": [ "red", "yellow", "green", "blue" ]
4949
},
5050
"point": {
51-
"id": "#point",
51+
"$id": "#point",
5252
"type": "object",
5353
"properties": {
5454
"x": {
@@ -189,8 +189,8 @@ that have references across files, e.g.
189189
``` javascript
190190
{
191191
"$schema": "http://json-schema.org/draft-04/schema#",
192+
"$id": "http://example.com/circle.json",
192193
"title": "Circle",
193-
"id": "http://example.com/circle.json",
194194
"description": "Schema for a circle shape",
195195
"type": "object",
196196
"properties": {

examples/example-input-json-schemas/circle.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
2-
"$schema": "http://json-schema.org/draft-04/schema#",
2+
"$schema": "http://json-schema.org/draft-07/schema#",
3+
"$id": "http://example.com/circle.json",
34
"title": "Circle",
4-
"id": "http://example.com/circle.json",
55
"description": "Schema for a circle shape",
66
"type": "object",
77
"properties": {

examples/example-input-json-schemas/definitions.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
{
2-
"$schema": "http://json-schema.org/draft-04/schema#",
2+
"$schema": "http://json-schema.org/draft-07/schema#",
3+
"$id": "http://example.com/definitions.json",
34
"title": "Definitions",
4-
"id": "http://example.com/definitions.json",
55
"description": "Schema for common types",
66
"definitions": {
77
"color": {
8-
"id": "#color",
8+
"$id": "#color",
99
"type": "string",
1010
"enum": [ "red", "yellow", "green", "blue" ]
1111
},
1212
"point": {
13-
"id": "#point",
13+
"$id": "#point",
1414
"type": "object",
1515
"properties": {
1616
"x": {

lib/printer/all_of_printer.ex

+17-18
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ defmodule JS2E.Printer.AllOfPrinter do
66

77
require Elixir.{EEx, Logger}
88
alias JS2E.Printer
9-
alias JsonSchema.{TypePath, Types}
9+
alias JsonSchema.{Resolver, Types}
1010
alias Printer.{ErrorUtil, PrinterError, PrinterResult, Utils}
1111

1212
alias Types.{
@@ -25,8 +25,7 @@ defmodule JS2E.Printer.AllOfPrinter do
2525
ElmFuzzers,
2626
ElmTypes,
2727
Indentation,
28-
Naming,
29-
ResolveType
28+
Naming
3029
}
3130

3231
@templates_location Application.get_env(:js2e, :templates_location)
@@ -68,8 +67,8 @@ defmodule JS2E.Printer.AllOfPrinter do
6867
@type elm_type_field :: %{name: String.t(), type: String.t()}
6968

7069
@spec create_type_field(
71-
TypePath.t(),
72-
TypePath.t(),
70+
URI.t(),
71+
URI.t(),
7372
SchemaDefinition.t(),
7473
Types.schemaDictionary(),
7574
String.t()
@@ -83,7 +82,7 @@ defmodule JS2E.Printer.AllOfPrinter do
8382
) do
8483
field_type_result =
8584
type_path
86-
|> ResolveType.resolve_type(parent, schema_def, schema_dict)
85+
|> Resolver.resolve_type(parent, schema_def, schema_dict)
8786
|> ElmTypes.create_type_name(schema_def, module_name)
8887

8988
case field_type_result do
@@ -135,8 +134,8 @@ defmodule JS2E.Printer.AllOfPrinter do
135134
end
136135

137136
@spec create_decoder_property(
138-
TypePath.t(),
139-
TypePath.t(),
137+
URI.t(),
138+
URI.t(),
140139
SchemaDefinition.t(),
141140
Types.schemaDictionary(),
142141
String.t()
@@ -149,7 +148,7 @@ defmodule JS2E.Printer.AllOfPrinter do
149148
module_name
150149
) do
151150
with {:ok, {property_type, resolved_schema_def}} <-
152-
ResolveType.resolve_type(type_path, parent, schema_def, schema_dict),
151+
Resolver.resolve_type(type_path, parent, schema_def, schema_dict),
153152
{:ok, decoder_name} <-
154153
ElmDecoders.create_decoder_name(
155154
{:ok, {property_type, resolved_schema_def}},
@@ -254,8 +253,8 @@ defmodule JS2E.Printer.AllOfPrinter do
254253
end
255254

256255
@spec create_encoder_properties(
257-
[TypePath.t()],
258-
TypePath.t(),
256+
[URI.t()],
257+
URI.t(),
259258
SchemaDefinition.t(),
260259
Types.schemaDictionary(),
261260
String.t()
@@ -268,7 +267,7 @@ defmodule JS2E.Printer.AllOfPrinter do
268267
module_name
269268
) do
270269
type_paths
271-
|> Enum.map(&ResolveType.resolve_type(&1, parent, schema_def, schema_dict))
270+
|> Enum.map(&Resolver.resolve_type(&1, parent, schema_def, schema_dict))
272271
|> Enum.map(&to_encoder_property(&1, schema_def, module_name))
273272
|> Enum.concat()
274273
end
@@ -299,7 +298,7 @@ defmodule JS2E.Printer.AllOfPrinter do
299298
type_def.properties
300299
|> Enum.map(fn {child_name, child_path} ->
301300
with {:ok, {child_type_def, child_schema_def}} <-
302-
ResolveType.resolve_type(
301+
Resolver.resolve_type(
303302
child_path,
304303
type_def.path,
305304
schema_def,
@@ -388,8 +387,8 @@ defmodule JS2E.Printer.AllOfPrinter do
388387
end
389388

390389
@spec create_fuzzers(
391-
[TypePath.t()],
392-
TypePath.t(),
390+
[URI.t()],
391+
URI.t(),
393392
SchemaDefinition.t(),
394393
Types.schemaDictionary(),
395394
String.t()
@@ -414,8 +413,8 @@ defmodule JS2E.Printer.AllOfPrinter do
414413
end
415414

416415
@spec create_fuzzer(
417-
TypePath.t(),
418-
TypePath.t(),
416+
URI.t(),
417+
URI.t(),
419418
SchemaDefinition.t(),
420419
Types.schemaDictionary(),
421420
String.t()
@@ -428,7 +427,7 @@ defmodule JS2E.Printer.AllOfPrinter do
428427
module_name
429428
) do
430429
with {:ok, {resolved_type, resolved_schema}} <-
431-
ResolveType.resolve_type(
430+
Resolver.resolve_type(
432431
type_path,
433432
parent,
434433
schema_def,

lib/printer/any_of_printer.ex

+17-18
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ defmodule JS2E.Printer.AnyOfPrinter do
66

77
require Elixir.{EEx, Logger}
88
alias JS2E.Printer
9-
alias JsonSchema.{TypePath, Types}
9+
alias JsonSchema.{Resolver, Types}
1010
alias Printer.{ErrorUtil, PrinterError, PrinterResult, Utils}
1111

1212
alias Types.{
@@ -25,8 +25,7 @@ defmodule JS2E.Printer.AnyOfPrinter do
2525
ElmFuzzers,
2626
ElmTypes,
2727
Indentation,
28-
Naming,
29-
ResolveType
28+
Naming
3029
}
3130

3231
@templates_location Application.get_env(:js2e, :templates_location)
@@ -67,8 +66,8 @@ defmodule JS2E.Printer.AnyOfPrinter do
6766
@type elm_type_field :: %{name: String.t(), type: String.t()}
6867

6968
@spec create_type_field(
70-
TypePath.t(),
71-
TypePath.t(),
69+
URI.t(),
70+
URI.t(),
7271
SchemaDefinition.t(),
7372
Types.schemaDictionary(),
7473
String.t()
@@ -82,7 +81,7 @@ defmodule JS2E.Printer.AnyOfPrinter do
8281
) do
8382
field_type_result =
8483
type_path
85-
|> ResolveType.resolve_type(parent, schema_def, schema_dict)
84+
|> Resolver.resolve_type(parent, schema_def, schema_dict)
8685
|> ElmTypes.create_type_name(schema_def, module_name)
8786

8887
case field_type_result do
@@ -134,8 +133,8 @@ defmodule JS2E.Printer.AnyOfPrinter do
134133
end
135134

136135
@spec create_decoder_property(
137-
TypePath.t(),
138-
TypePath.t(),
136+
URI.t(),
137+
URI.t(),
139138
SchemaDefinition.t(),
140139
Types.schemaDictionary(),
141140
String.t()
@@ -148,7 +147,7 @@ defmodule JS2E.Printer.AnyOfPrinter do
148147
module_name
149148
) do
150149
with {:ok, {property_type, resolved_schema_def}} <-
151-
ResolveType.resolve_type(type_path, parent, schema_def, schema_dict),
150+
Resolver.resolve_type(type_path, parent, schema_def, schema_dict),
152151
{:ok, decoder_name} <-
153152
ElmDecoders.create_decoder_name(
154153
{:ok, {property_type, resolved_schema_def}},
@@ -249,8 +248,8 @@ defmodule JS2E.Printer.AnyOfPrinter do
249248
end
250249

251250
@spec create_encoder_properties(
252-
[TypePath.t()],
253-
TypePath.t(),
251+
[URI.t()],
252+
URI.t(),
254253
SchemaDefinition.t(),
255254
Types.schemaDictionary(),
256255
String.t()
@@ -263,7 +262,7 @@ defmodule JS2E.Printer.AnyOfPrinter do
263262
module_name
264263
) do
265264
type_paths
266-
|> Enum.map(&ResolveType.resolve_type(&1, parent, schema_def, schema_dict))
265+
|> Enum.map(&Resolver.resolve_type(&1, parent, schema_def, schema_dict))
267266
|> Enum.map(&to_encoder_property(&1, schema_def, module_name))
268267
|> Enum.concat()
269268
end
@@ -293,7 +292,7 @@ defmodule JS2E.Printer.AnyOfPrinter do
293292
type_def.properties
294293
|> Enum.map(fn {child_name, child_path} ->
295294
with {:ok, {child_type_def, child_schema_def}} <-
296-
ResolveType.resolve_type(
295+
Resolver.resolve_type(
297296
child_path,
298297
type_def.path,
299298
schema_def,
@@ -382,8 +381,8 @@ defmodule JS2E.Printer.AnyOfPrinter do
382381
end
383382

384383
@spec create_fuzzer_properties(
385-
[TypePath.t()],
386-
TypePath.t(),
384+
[URI.t()],
385+
URI.t(),
387386
SchemaDefinition.t(),
388387
Types.schemaDictionary(),
389388
String.t()
@@ -408,8 +407,8 @@ defmodule JS2E.Printer.AnyOfPrinter do
408407
end
409408

410409
@spec create_fuzzer_property(
411-
TypePath.t(),
412-
TypePath.t(),
410+
URI.t(),
411+
URI.t(),
413412
SchemaDefinition.t(),
414413
Types.schemaDictionary(),
415414
String.t()
@@ -422,7 +421,7 @@ defmodule JS2E.Printer.AnyOfPrinter do
422421
module_name
423422
) do
424423
with {:ok, {resolved_type, resolved_schema}} <-
425-
ResolveType.resolve_type(
424+
Resolver.resolve_type(
426425
type_path,
427426
parent,
428427
schema_def,

lib/printer/array_printer.ex

+21-12
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ defmodule JS2E.Printer.ArrayPrinter do
66

77
require Elixir.{EEx, Logger}
88
alias JS2E.Printer
9-
alias JsonSchema.Types
9+
alias JsonSchema.{Parser, Resolver, Types}
10+
alias Parser.ParserError
1011
alias Printer.{PrinterError, PrinterResult, Utils}
1112

1213
alias Types.{ArrayType, PrimitiveType, SchemaDefinition}
@@ -16,8 +17,7 @@ defmodule JS2E.Printer.ArrayPrinter do
1617
ElmEncoders,
1718
ElmFuzzers,
1819
ElmTypes,
19-
Naming,
20-
ResolveType
20+
Naming
2121
}
2222

2323
@templates_location Application.get_env(:js2e, :templates_location)
@@ -63,15 +63,18 @@ defmodule JS2E.Printer.ArrayPrinter do
6363
_module_name
6464
) do
6565
with {:ok, {items_type, _resolved_schema_def}} <-
66-
ResolveType.resolve_type(items_path, path, schema_def, schema_dict),
66+
Resolver.resolve_type(items_path, path, schema_def, schema_dict),
6767
{:ok, items_type_name} <- determine_type_name(items_type),
6868
{:ok, items_decoder_name} <- determine_decoder_name(items_type) do
6969
"#{Naming.normalize_identifier(name, :downcase)}Decoder"
7070
|> decoder_template(items_type_name, items_decoder_name)
7171
|> PrinterResult.new()
7272
else
73-
{:error, error} ->
74-
PrinterResult.new("", [error])
73+
{:error, %ParserError{identifier: id, error_type: atom, message: str}} ->
74+
PrinterResult.new("", [PrinterError.new(id, atom, str)])
75+
76+
{:error, printer_error} ->
77+
PrinterResult.new("", [printer_error])
7578
end
7679
end
7780

@@ -135,15 +138,18 @@ defmodule JS2E.Printer.ArrayPrinter do
135138
_module_name
136139
) do
137140
with {:ok, {items_type, _resolved_schema_def}} <-
138-
ResolveType.resolve_type(items_path, path, schema_def, schema_dict),
141+
Resolver.resolve_type(items_path, path, schema_def, schema_dict),
139142
{:ok, items_type_name} <- determine_type_name(items_type),
140143
{:ok, items_encoder_name} <- determine_encoder_name(items_type) do
141144
"encode#{Naming.normalize_identifier(items_type_name, :upcase)}s"
142145
|> encoder_template(name, items_type_name, items_encoder_name)
143146
|> PrinterResult.new()
144147
else
145-
{:error, error} ->
146-
PrinterResult.new("", [error])
148+
{:error, %ParserError{identifier: id, error_type: atom, message: str}} ->
149+
PrinterResult.new("", [PrinterError.new(id, atom, str)])
150+
151+
{:error, printer_error} ->
152+
PrinterResult.new("", [printer_error])
147153
end
148154
end
149155

@@ -192,7 +198,7 @@ defmodule JS2E.Printer.ArrayPrinter do
192198
_module_name
193199
) do
194200
with {:ok, {items_type, _resolved_schema_def}} <-
195-
ResolveType.resolve_type(items_path, path, schema_def, schema_dict),
201+
Resolver.resolve_type(items_path, path, schema_def, schema_dict),
196202
{:ok, items_type_name} <- determine_type_name(items_type),
197203
{:ok, items_fuzzer_name} <- determine_fuzzer_name(items_type) do
198204
array_name = Naming.normalize_identifier(name, :upcase)
@@ -213,8 +219,11 @@ defmodule JS2E.Printer.ArrayPrinter do
213219
)
214220
|> PrinterResult.new()
215221
else
216-
{:error, error} ->
217-
PrinterResult.new("", [error])
222+
{:error, %ParserError{identifier: id, error_type: atom, message: str}} ->
223+
PrinterResult.new("", [PrinterError.new(id, atom, str)])
224+
225+
{:error, printer_error} ->
226+
PrinterResult.new("", [printer_error])
218227
end
219228
end
220229

0 commit comments

Comments
 (0)