lutaml-model 0.7.2 → 0.7.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (187) hide show
  1. checksums.yaml +4 -4
  2. data/.envrc +1 -0
  3. data/.github/workflows/release.yml +3 -0
  4. data/.gitignore +6 -1
  5. data/.irbrc +1 -0
  6. data/.pryrc +1 -0
  7. data/.rubocop_todo.yml +25 -52
  8. data/README.adoc +2177 -192
  9. data/docs/custom_registers.adoc +228 -0
  10. data/docs/schema_generation.adoc +898 -0
  11. data/docs/schema_import.adoc +364 -0
  12. data/flake.lock +114 -0
  13. data/flake.nix +103 -0
  14. data/lib/lutaml/model/attribute.rb +230 -94
  15. data/lib/lutaml/model/choice.rb +30 -0
  16. data/lib/lutaml/model/collection.rb +194 -0
  17. data/lib/lutaml/model/comparable_model.rb +3 -3
  18. data/lib/lutaml/model/config.rb +26 -3
  19. data/lib/lutaml/model/constants.rb +2 -0
  20. data/lib/lutaml/model/error/element_count_out_of_range_error.rb +29 -0
  21. data/lib/lutaml/model/error/invalid_attribute_name_error.rb +15 -0
  22. data/lib/lutaml/model/error/invalid_attribute_options_error.rb +16 -0
  23. data/lib/lutaml/model/error/invalid_choice_range_error.rb +3 -5
  24. data/lib/lutaml/model/error/register/not_registrable_class_error.rb +11 -0
  25. data/lib/lutaml/model/error/type/invalid_value_error.rb +5 -3
  26. data/lib/lutaml/model/error/type/max_bound_error.rb +20 -0
  27. data/lib/lutaml/model/error/type/max_length_error.rb +20 -0
  28. data/lib/lutaml/model/error/type/min_bound_error.rb +20 -0
  29. data/lib/lutaml/model/error/type/min_length_error.rb +20 -0
  30. data/lib/lutaml/model/error/type/pattern_not_matched_error.rb +18 -0
  31. data/lib/lutaml/model/error/validation_failed_error.rb +9 -0
  32. data/lib/lutaml/model/error.rb +10 -0
  33. data/lib/lutaml/model/errors.rb +36 -0
  34. data/lib/lutaml/model/format_registry.rb +5 -2
  35. data/lib/lutaml/model/global_register.rb +41 -0
  36. data/lib/lutaml/model/{hash.rb → hash_adapter.rb} +5 -5
  37. data/lib/lutaml/model/jsonl/document.rb +14 -0
  38. data/lib/lutaml/model/jsonl/mapping.rb +19 -0
  39. data/lib/lutaml/model/jsonl/mapping_rule.rb +9 -0
  40. data/lib/lutaml/model/jsonl/standard_adapter.rb +33 -0
  41. data/lib/lutaml/model/jsonl/transform.rb +19 -0
  42. data/lib/lutaml/model/jsonl.rb +21 -0
  43. data/lib/lutaml/model/key_value_document.rb +3 -2
  44. data/lib/lutaml/model/mapping/key_value_mapping.rb +64 -4
  45. data/lib/lutaml/model/mapping/key_value_mapping_rule.rb +4 -0
  46. data/lib/lutaml/model/mapping/mapping_rule.rb +8 -3
  47. data/lib/lutaml/model/register.rb +105 -0
  48. data/lib/lutaml/model/registrable.rb +6 -0
  49. data/lib/lutaml/model/schema/base_schema.rb +64 -0
  50. data/lib/lutaml/model/schema/decorators/attribute.rb +114 -0
  51. data/lib/lutaml/model/schema/decorators/choices.rb +31 -0
  52. data/lib/lutaml/model/schema/decorators/class_definition.rb +85 -0
  53. data/lib/lutaml/model/schema/decorators/definition_collection.rb +97 -0
  54. data/lib/lutaml/model/schema/generator/definition.rb +53 -0
  55. data/lib/lutaml/model/schema/generator/definitions_collection.rb +81 -0
  56. data/lib/lutaml/model/schema/generator/properties_collection.rb +63 -0
  57. data/lib/lutaml/model/schema/generator/property.rb +110 -0
  58. data/lib/lutaml/model/schema/generator/ref.rb +24 -0
  59. data/lib/lutaml/model/schema/helpers/template_helper.rb +49 -0
  60. data/lib/lutaml/model/schema/json_schema.rb +42 -49
  61. data/lib/lutaml/model/schema/relaxng_schema.rb +14 -10
  62. data/lib/lutaml/model/schema/renderer.rb +36 -0
  63. data/lib/lutaml/model/schema/shared_methods.rb +24 -0
  64. data/lib/lutaml/model/schema/templates/model.erb +9 -0
  65. data/lib/lutaml/model/schema/xml_compiler/attribute.rb +85 -0
  66. data/lib/lutaml/model/schema/xml_compiler/attribute_group.rb +45 -0
  67. data/lib/lutaml/model/schema/xml_compiler/choice.rb +65 -0
  68. data/lib/lutaml/model/schema/xml_compiler/complex_content.rb +27 -0
  69. data/lib/lutaml/model/schema/xml_compiler/complex_content_restriction.rb +34 -0
  70. data/lib/lutaml/model/schema/xml_compiler/complex_type.rb +136 -0
  71. data/lib/lutaml/model/schema/xml_compiler/element.rb +104 -0
  72. data/lib/lutaml/model/schema/xml_compiler/group.rb +97 -0
  73. data/lib/lutaml/model/schema/xml_compiler/restriction.rb +101 -0
  74. data/lib/lutaml/model/schema/xml_compiler/sequence.rb +50 -0
  75. data/lib/lutaml/model/schema/xml_compiler/simple_content.rb +36 -0
  76. data/lib/lutaml/model/schema/xml_compiler/simple_type.rb +189 -0
  77. data/lib/lutaml/model/schema/xml_compiler.rb +231 -587
  78. data/lib/lutaml/model/schema/xsd_schema.rb +12 -8
  79. data/lib/lutaml/model/schema/yaml_schema.rb +41 -35
  80. data/lib/lutaml/model/schema.rb +1 -0
  81. data/lib/lutaml/model/sequence.rb +60 -30
  82. data/lib/lutaml/model/serialize.rb +175 -53
  83. data/lib/lutaml/model/services/base.rb +11 -0
  84. data/lib/lutaml/model/services/logger.rb +2 -2
  85. data/lib/lutaml/model/services/rule_value_extractor.rb +92 -0
  86. data/lib/lutaml/model/services/type/validator/number.rb +25 -0
  87. data/lib/lutaml/model/services/type/validator/string.rb +52 -0
  88. data/lib/lutaml/model/services/type/validator.rb +43 -0
  89. data/lib/lutaml/model/services/validator.rb +145 -0
  90. data/lib/lutaml/model/services.rb +3 -0
  91. data/lib/lutaml/model/transform/key_value_transform.rb +60 -50
  92. data/lib/lutaml/model/transform/xml_transform.rb +46 -57
  93. data/lib/lutaml/model/transform.rb +22 -8
  94. data/lib/lutaml/model/type/boolean.rb +1 -1
  95. data/lib/lutaml/model/type/date.rb +1 -1
  96. data/lib/lutaml/model/type/date_time.rb +1 -1
  97. data/lib/lutaml/model/type/decimal.rb +11 -9
  98. data/lib/lutaml/model/type/float.rb +2 -1
  99. data/lib/lutaml/model/type/integer.rb +24 -21
  100. data/lib/lutaml/model/type/string.rb +4 -2
  101. data/lib/lutaml/model/type/time.rb +1 -1
  102. data/lib/lutaml/model/type/time_without_date.rb +1 -1
  103. data/lib/lutaml/model/type/value.rb +5 -1
  104. data/lib/lutaml/model/type.rb +5 -2
  105. data/lib/lutaml/model/utils.rb +30 -8
  106. data/lib/lutaml/model/validation.rb +6 -4
  107. data/lib/lutaml/model/version.rb +1 -1
  108. data/lib/lutaml/model/xml/document.rb +37 -19
  109. data/lib/lutaml/model/xml/mapping.rb +74 -13
  110. data/lib/lutaml/model/xml/mapping_rule.rb +10 -2
  111. data/lib/lutaml/model/xml/nokogiri_adapter.rb +5 -3
  112. data/lib/lutaml/model/xml/oga/element.rb +4 -1
  113. data/lib/lutaml/model/xml/oga_adapter.rb +4 -3
  114. data/lib/lutaml/model/xml/ox_adapter.rb +20 -6
  115. data/lib/lutaml/model/xml/xml_element.rb +3 -28
  116. data/lib/lutaml/model/xml.rb +1 -1
  117. data/lib/lutaml/model/xml_adapter/element.rb +1 -1
  118. data/lib/lutaml/model/xml_adapter/nokogiri_adapter.rb +1 -1
  119. data/lib/lutaml/model/xml_adapter/oga_adapter.rb +1 -1
  120. data/lib/lutaml/model/xml_adapter/ox_adapter.rb +1 -1
  121. data/lib/lutaml/model/yamls/document.rb +14 -0
  122. data/lib/lutaml/model/yamls/mapping.rb +19 -0
  123. data/lib/lutaml/model/yamls/mapping_rule.rb +9 -0
  124. data/lib/lutaml/model/yamls/standard_adapter.rb +34 -0
  125. data/lib/lutaml/model/yamls/transform.rb +19 -0
  126. data/lib/lutaml/model/yamls.rb +21 -0
  127. data/lib/lutaml/model.rb +7 -31
  128. data/spec/benchmarks/xml_parsing_benchmark_spec.rb +4 -5
  129. data/spec/fixtures/xml/advanced_test_schema.xsd +134 -0
  130. data/spec/fixtures/xml/examples/nested_categories.xml +55 -0
  131. data/spec/fixtures/xml/examples/valid_catalog.xml +43 -0
  132. data/spec/fixtures/xml/product_catalog.xsd +151 -0
  133. data/spec/fixtures/xml/specifications_schema.xsd +38 -0
  134. data/spec/lutaml/model/attribute_collection_spec.rb +101 -0
  135. data/spec/lutaml/model/attribute_spec.rb +41 -44
  136. data/spec/lutaml/model/choice_spec.rb +44 -0
  137. data/spec/lutaml/model/custom_collection_spec.rb +830 -0
  138. data/spec/lutaml/model/custom_model_spec.rb +15 -3
  139. data/spec/lutaml/model/custom_vobject_adapter_spec.rb +6 -6
  140. data/spec/lutaml/model/defaults_spec.rb +5 -1
  141. data/spec/lutaml/model/global_register_spec.rb +108 -0
  142. data/spec/lutaml/model/group_spec.rb +9 -3
  143. data/spec/lutaml/model/jsonl/standard_adapter_spec.rb +91 -0
  144. data/spec/lutaml/model/jsonl_spec.rb +229 -0
  145. data/spec/lutaml/model/multiple_mapping_spec.rb +1 -1
  146. data/spec/lutaml/model/register/key_value_spec.rb +275 -0
  147. data/spec/lutaml/model/register/xml_spec.rb +187 -0
  148. data/spec/lutaml/model/register_spec.rb +147 -0
  149. data/spec/lutaml/model/rule_value_extractor_spec.rb +162 -0
  150. data/spec/lutaml/model/schema/generator/definitions_collection_spec.rb +120 -0
  151. data/spec/lutaml/model/schema/json_schema_spec.rb +412 -51
  152. data/spec/lutaml/model/schema/json_schema_to_models_spec.rb +383 -0
  153. data/spec/lutaml/model/schema/xml_compiler/attribute_group_spec.rb +65 -0
  154. data/spec/lutaml/model/schema/xml_compiler/attribute_spec.rb +63 -0
  155. data/spec/lutaml/model/schema/xml_compiler/choice_spec.rb +71 -0
  156. data/spec/lutaml/model/schema/xml_compiler/complex_content_restriction_spec.rb +55 -0
  157. data/spec/lutaml/model/schema/xml_compiler/complex_content_spec.rb +37 -0
  158. data/spec/lutaml/model/schema/xml_compiler/complex_type_spec.rb +173 -0
  159. data/spec/lutaml/model/schema/xml_compiler/element_spec.rb +63 -0
  160. data/spec/lutaml/model/schema/xml_compiler/group_spec.rb +86 -0
  161. data/spec/lutaml/model/schema/xml_compiler/restriction_spec.rb +76 -0
  162. data/spec/lutaml/model/schema/xml_compiler/sequence_spec.rb +59 -0
  163. data/spec/lutaml/model/schema/xml_compiler/simple_content_spec.rb +55 -0
  164. data/spec/lutaml/model/schema/xml_compiler/simple_type_spec.rb +181 -0
  165. data/spec/lutaml/model/schema/xml_compiler_spec.rb +503 -1804
  166. data/spec/lutaml/model/schema/yaml_schema_spec.rb +249 -26
  167. data/spec/lutaml/model/sequence_spec.rb +36 -0
  168. data/spec/lutaml/model/serializable_spec.rb +31 -0
  169. data/spec/lutaml/model/type_spec.rb +8 -4
  170. data/spec/lutaml/model/utils_spec.rb +3 -3
  171. data/spec/lutaml/model/xml/derived_attributes_spec.rb +1 -1
  172. data/spec/lutaml/model/xml/xml_element_spec.rb +7 -1
  173. data/spec/lutaml/model/xml_adapter/xml_namespace_spec.rb +6 -6
  174. data/spec/lutaml/model/xml_adapter_spec.rb +24 -0
  175. data/spec/lutaml/model/xml_mapping_rule_spec.rb +11 -4
  176. data/spec/lutaml/model/xml_mapping_spec.rb +1 -1
  177. data/spec/lutaml/model/xml_spec.rb +6 -6
  178. data/spec/lutaml/model/yamls/standard_adapter_spec.rb +183 -0
  179. data/spec/lutaml/model/yamls_spec.rb +294 -0
  180. data/spec/spec_helper.rb +1 -0
  181. metadata +105 -9
  182. data/lib/lutaml/model/schema/templates/simple_type.rb +0 -247
  183. /data/lib/lutaml/model/{hash → hash_adapter}/document.rb +0 -0
  184. /data/lib/lutaml/model/{hash → hash_adapter}/mapping.rb +0 -0
  185. /data/lib/lutaml/model/{hash → hash_adapter}/mapping_rule.rb +0 -0
  186. /data/lib/lutaml/model/{hash → hash_adapter}/standard_adapter.rb +0 -0
  187. /data/lib/lutaml/model/{hash → hash_adapter}/transform.rb +0 -0
@@ -1,7 +1,13 @@
1
1
  require "spec_helper"
2
2
  require "lutaml/model/schema"
3
3
 
4
- module SchemaGeneration
4
+ module JsonSchemaSpec
5
+ # Class for register testing
6
+ class RegisterGlaze < Lutaml::Model::Serializable
7
+ attribute :color, :string
8
+ attribute :finish, :integer
9
+ end
10
+
5
11
  class Glaze < Lutaml::Model::Serializable
6
12
  attribute :color, Lutaml::Model::Type::String
7
13
  attribute :finish, Lutaml::Model::Type::String
@@ -13,67 +19,422 @@ module SchemaGeneration
13
19
  attribute :glaze, Glaze
14
20
  attribute :materials, Lutaml::Model::Type::String, collection: true
15
21
  end
22
+
23
+ class ChoiceModel < Lutaml::Model::Serializable
24
+ attribute :name, Lutaml::Model::Type::String
25
+
26
+ choice(min: 1, max: 2) do
27
+ attribute :email, Lutaml::Model::Type::String
28
+ attribute :phone, Lutaml::Model::Type::String
29
+ end
30
+ end
31
+
32
+ class ValidationModel < Lutaml::Model::Serializable
33
+ attribute :name, Lutaml::Model::Type::String, values: %w[Alice Bob Charlie]
34
+ attribute :email, Lutaml::Model::Type::String, pattern: /.*?\S+@.+\.\S+/
35
+ attribute :age, Lutaml::Model::Type::Integer, collection: 1..3
36
+ attribute :score, Lutaml::Model::Type::Float, default: 0.0
37
+ end
38
+
39
+ class Shape < Lutaml::Model::Serializable
40
+ attribute :area, :float
41
+ end
42
+
43
+ class Circle < Shape
44
+ attribute :radius, Lutaml::Model::Type::Float
45
+ end
46
+
47
+ class Square < Shape
48
+ attribute :side, Lutaml::Model::Type::Float
49
+ end
50
+
51
+ class PolymorphicModel < Lutaml::Model::Serializable
52
+ attribute :shape, Shape, polymorphic: [Circle, Square]
53
+ end
54
+
55
+ # For deeply nested classes
56
+ class Detail < Lutaml::Model::Serializable
57
+ attribute :weight, Lutaml::Model::Type::Float
58
+ attribute :color, Lutaml::Model::Type::String
59
+ end
60
+
61
+ class Item < Lutaml::Model::Serializable
62
+ attribute :name, Lutaml::Model::Type::String
63
+ attribute :detail, Detail
64
+ end
65
+
66
+ class Box < Lutaml::Model::Serializable
67
+ attribute :size, Lutaml::Model::Type::String
68
+ attribute :items, Item, collection: true
69
+ end
70
+
71
+ class Container < Lutaml::Model::Serializable
72
+ attribute :id, Lutaml::Model::Type::String
73
+ attribute :box, Box
74
+ end
16
75
  end
17
76
 
18
77
  RSpec.describe Lutaml::Model::Schema::JsonSchema do
19
78
  describe ".generate" do
20
- it "generates a JSON schema for nested Serialize objects" do
21
- schema = described_class.generate(SchemaGeneration::Vase,
22
- id: "https://example.com/vase.schema.json", description: "A vase schema", pretty: true)
79
+ let(:parsed_schema) { JSON.parse(schema) }
80
+
81
+ context "with basic model" do
82
+ let(:schema) do
83
+ described_class.generate(
84
+ JsonSchemaSpec::Vase,
85
+ id: "https://example.com/vase.schema.json",
86
+ description: "A vase schema",
87
+ pretty: true,
88
+ )
89
+ end
90
+
91
+ let(:expected_schema) do
92
+ {
93
+ "$schema" => "https://json-schema.org/draft/2020-12/schema",
94
+ "$id" => "https://example.com/vase.schema.json",
95
+ "description" => "A vase schema",
96
+ "$ref" => "#/$defs/JsonSchemaSpec_Vase",
97
+ "$defs" => {
98
+ "JsonSchemaSpec_Vase" => {
99
+ "type" => "object",
100
+ "additionalProperties" => false,
101
+ "properties" => {
102
+ "height" => {
103
+ "type" => ["number", "null"],
104
+ },
105
+ "diameter" => {
106
+ "type" => ["number", "null"],
107
+ },
108
+ "glaze" => {
109
+ "$ref" => "#/$defs/JsonSchemaSpec_Glaze",
110
+ },
111
+ "materials" => {
112
+ "type" => "array",
113
+ "items" => {
114
+ "type" => "string",
115
+ },
116
+ },
117
+ },
118
+ },
119
+ "JsonSchemaSpec_Glaze" => {
120
+ "type" => "object",
121
+ "additionalProperties" => false,
122
+ "properties" => {
123
+ "color" => {
124
+ "type" => ["string", "null"],
125
+ },
126
+ "finish" => {
127
+ "type" => ["string", "null"],
128
+ },
129
+ },
130
+ },
131
+ },
132
+ }
133
+ end
134
+
135
+ it "generates a JSON schema for nested Serialize objects" do
136
+ expect(parsed_schema).to eq(expected_schema)
137
+ end
138
+ end
139
+
140
+ context "with choice validation" do
141
+ let(:schema) do
142
+ described_class.generate(JsonSchemaSpec::ChoiceModel, pretty: true)
143
+ end
144
+
145
+ let(:expected_schema) do
146
+ {
147
+ "$schema" => "https://json-schema.org/draft/2020-12/schema",
148
+ "$ref" => "#/$defs/JsonSchemaSpec_ChoiceModel",
149
+ "$defs" => {
150
+ "JsonSchemaSpec_ChoiceModel" => {
151
+ "type" => "object",
152
+ "additionalProperties" => false,
153
+ "properties" => {
154
+ "name" => {
155
+ "type" => ["string", "null"],
156
+ },
157
+ "email" => {
158
+ "type" => ["string", "null"],
159
+ },
160
+ "phone" => {
161
+ "type" => ["string", "null"],
162
+ },
163
+ },
164
+ "oneOf" => [
165
+ {
166
+ "type" => "object",
167
+ "properties" => {
168
+ "email" => {
169
+ "type" => ["string", "null"],
170
+ },
171
+ "phone" => {
172
+ "type" => ["string", "null"],
173
+ },
174
+ },
175
+ },
176
+ ],
177
+ },
178
+ },
179
+ }
180
+ end
181
+
182
+ it "generates a JSON schema with choice constraints" do
183
+ expect(parsed_schema).to eq(expected_schema)
184
+ end
185
+ end
186
+
187
+ context "with validation constraints" do
188
+ let(:schema) do
189
+ described_class.generate(
190
+ JsonSchemaSpec::ValidationModel,
191
+ pretty: true,
192
+ )
193
+ end
194
+
195
+ let(:expected_schema) do
196
+ {
197
+ "$schema" => "https://json-schema.org/draft/2020-12/schema",
198
+ "$ref" => "#/$defs/JsonSchemaSpec_ValidationModel",
199
+ "$defs" => {
200
+ "JsonSchemaSpec_ValidationModel" => {
201
+ "type" => "object",
202
+ "additionalProperties" => false,
203
+ "properties" => {
204
+ "name" => {
205
+ "type" => ["string", "null"],
206
+ "enum" => ["Alice", "Bob", "Charlie"],
207
+ },
208
+ "email" => {
209
+ "type" => ["string", "null"],
210
+ "pattern" => ".*?\\S+@.+\\.\\S+",
211
+ },
212
+ "age" => {
213
+ "type" => "array",
214
+ "items" => {
215
+ "type" => "integer",
216
+ },
217
+ "minItems" => 1,
218
+ "maxItems" => 3,
219
+ },
220
+ "score" => {
221
+ "type" => ["number", "null"],
222
+ "default" => 0.0,
223
+ },
224
+ },
225
+ },
226
+ },
227
+ }
228
+ end
229
+
230
+ it "generates a JSON schema with validation constraints" do
231
+ expect(parsed_schema).to eq(expected_schema)
232
+ end
233
+ end
23
234
 
24
- expected_schema = <<~JSON.chomp
235
+ context "with polymorphic types" do
236
+ let(:schema) do
237
+ described_class.generate(
238
+ JsonSchemaSpec::PolymorphicModel,
239
+ pretty: true,
240
+ )
241
+ end
242
+
243
+ let(:expected_schema) do
25
244
  {
26
- "$schema": "https://json-schema.org/draft/2020-12/schema",
27
- "$id": "https://example.com/vase.schema.json",
28
- "description": "A vase schema",
29
- "$ref": "#/$defs/SchemaGeneration::Vase",
30
- "$defs": {
31
- "SchemaGeneration::Vase": {
32
- "type": "object",
33
- "properties": {
34
- "height": {
35
- "type": "number"
36
- },
37
- "diameter": {
38
- "type": "number"
39
- },
40
- "glaze": {
41
- "$ref": "#/$defs/SchemaGeneration::Glaze"
42
- },
43
- "materials": {
44
- "type": "array",
45
- "items": {
46
- "type": "string"
47
- }
48
- }
245
+ "$schema" => "https://json-schema.org/draft/2020-12/schema",
246
+ "$ref" => "#/$defs/JsonSchemaSpec_PolymorphicModel",
247
+ "$defs" => {
248
+ "JsonSchemaSpec_PolymorphicModel" => {
249
+ "type" => "object",
250
+ "additionalProperties" => false,
251
+ "properties" => {
252
+ "shape" => {
253
+ "type" => ["object", "null"],
254
+ "oneOf" => [
255
+ {
256
+ "$ref" => "#/$defs/JsonSchemaSpec_Circle",
257
+ },
258
+ {
259
+ "$ref" => "#/$defs/JsonSchemaSpec_Square",
260
+ },
261
+ {
262
+ "$ref" => "#/$defs/JsonSchemaSpec_Shape",
263
+ },
264
+ ],
265
+ },
49
266
  },
50
- "required": [
51
- "height",
52
- "diameter",
53
- "glaze",
54
- "materials"
55
- ]
56
267
  },
57
- "SchemaGeneration::Glaze": {
58
- "type": "object",
59
- "properties": {
60
- "color": {
61
- "type": "string"
62
- },
63
- "finish": {
64
- "type": "string"
65
- }
268
+ "JsonSchemaSpec_Circle" => {
269
+ "type" => "object",
270
+ "additionalProperties" => false,
271
+ "properties" => {
272
+ "area" => {
273
+ "type" => ["number", "null"],
274
+ },
275
+ "radius" => {
276
+ "type" => ["number", "null"],
277
+ },
66
278
  },
67
- "required": [
68
- "color",
69
- "finish"
70
- ]
71
- }
72
- }
279
+ },
280
+ "JsonSchemaSpec_Square" => {
281
+ "type" => "object",
282
+ "additionalProperties" => false,
283
+ "properties" => {
284
+ "area" => {
285
+ "type" => ["number", "null"],
286
+ },
287
+ "side" => {
288
+ "type" => ["number", "null"],
289
+ },
290
+ },
291
+ },
292
+ "JsonSchemaSpec_Shape" => {
293
+ "type" => "object",
294
+ "additionalProperties" => false,
295
+ "properties" => {
296
+ "area" => { "type" => ["number", "null"] },
297
+ },
298
+ },
299
+ },
73
300
  }
74
- JSON
301
+ end
302
+
303
+ it "generates a JSON schema with polymorphic type constraints" do
304
+ expect(parsed_schema).to eq(expected_schema)
305
+ end
306
+ end
307
+
308
+ context "with deeply nested classes" do
309
+ let(:schema) do
310
+ described_class.generate(
311
+ JsonSchemaSpec::Container,
312
+ pretty: true,
313
+ )
314
+ end
315
+
316
+ let(:expected_schema) do
317
+ {
318
+ "$schema" => "https://json-schema.org/draft/2020-12/schema",
319
+ "$ref" => "#/$defs/JsonSchemaSpec_Container",
320
+ "$defs" => {
321
+ "JsonSchemaSpec_Container" => {
322
+ "type" => "object",
323
+ "additionalProperties" => false,
324
+ "properties" => {
325
+ "id" => {
326
+ "type" => ["string", "null"],
327
+ },
328
+ "box" => {
329
+ "$ref" => "#/$defs/JsonSchemaSpec_Box",
330
+ },
331
+ },
332
+ },
333
+ "JsonSchemaSpec_Box" => {
334
+ "type" => "object",
335
+ "additionalProperties" => false,
336
+ "properties" => {
337
+ "size" => {
338
+ "type" => ["string", "null"],
339
+ },
340
+ "items" => {
341
+ "type" => "array",
342
+ "items" => {
343
+ "$ref" => "#/$defs/JsonSchemaSpec_Item",
344
+ },
345
+ },
346
+ },
347
+ },
348
+ "JsonSchemaSpec_Item" => {
349
+ "type" => "object",
350
+ "additionalProperties" => false,
351
+ "properties" => {
352
+ "name" => {
353
+ "type" => ["string", "null"],
354
+ },
355
+ "detail" => {
356
+ "$ref" => "#/$defs/JsonSchemaSpec_Detail",
357
+ },
358
+ },
359
+ },
360
+ "JsonSchemaSpec_Detail" => {
361
+ "type" => "object",
362
+ "additionalProperties" => false,
363
+ "properties" => {
364
+ "weight" => {
365
+ "type" => ["number", "null"],
366
+ },
367
+ "color" => {
368
+ "type" => ["string", "null"],
369
+ },
370
+ },
371
+ },
372
+ },
373
+ }
374
+ end
375
+
376
+ it "generates a JSON schema for deeply nested classes" do
377
+ expect(parsed_schema).to eq(expected_schema)
378
+ end
379
+ end
380
+
381
+ context "with register class" do
382
+ let(:register) { Lutaml::Model::Register.new(:json_schema) }
383
+ let(:schema) { described_class.generate(register.get_class(:vase), pretty: true) }
384
+ let(:expected_schema) do
385
+ {
386
+ "$schema" => "https://json-schema.org/draft/2020-12/schema",
387
+ "$ref" => "#/$defs/JsonSchemaSpec_Vase",
388
+ "$defs" => {
389
+ "JsonSchemaSpec_Vase" => {
390
+ "type" => "object",
391
+ "additionalProperties" => false,
392
+ "properties" => {
393
+ "height" => {
394
+ "type" => ["number", "null"],
395
+ },
396
+ "diameter" => {
397
+ "type" => ["number", "null"],
398
+ },
399
+ "glaze" => {
400
+ "$ref" => "#/$defs/JsonSchemaSpec_RegisterGlaze",
401
+ },
402
+ "materials" => {
403
+ "type" => "array",
404
+ "items" => {
405
+ "type" => "string",
406
+ },
407
+ },
408
+ },
409
+ },
410
+ "JsonSchemaSpec_RegisterGlaze" => {
411
+ "type" => "object",
412
+ "additionalProperties" => false,
413
+ "properties" => {
414
+ "color" => {
415
+ "type" => ["string", "null"],
416
+ },
417
+ "finish" => {
418
+ "type" => ["integer", "null"],
419
+ },
420
+ },
421
+ },
422
+ },
423
+ }
424
+ end
425
+
426
+ before do
427
+ Lutaml::Model::GlobalRegister.register(register)
428
+ register.register_model_tree(JsonSchemaSpec::Vase)
429
+ register.register_global_type_substitution(
430
+ from_type: JsonSchemaSpec::Glaze,
431
+ to_type: JsonSchemaSpec::RegisterGlaze,
432
+ )
433
+ end
75
434
 
76
- expect(schema).to eq(expected_schema)
435
+ it "generates a JSON schema with substituted registered class" do
436
+ expect(parsed_schema).to eq(expected_schema)
437
+ end
77
438
  end
78
439
  end
79
440
  end