Skip to content

Commit 007cbe9

Browse files
committed
fix some edge cases
1 parent a3d3f63 commit 007cbe9

File tree

4 files changed

+38
-29
lines changed

4 files changed

+38
-29
lines changed

shard.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: json-schema
2-
version: 1.0.1
2+
version: 1.0.2
33

44
development_dependencies:
55
ameba:

spec/json_schema_spec.cr

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ describe JSON::Schema do
1313

1414
TestGenericInheritance.json_schema.should eq({type: "object", additionalProperties: {type: "integer"}})
1515

16+
Array(Int32).json_schema.should eq({type: "array", items: {type: "integer"}})
17+
SuperArray.json_schema.should eq({type: "array", items: {type: "integer"}})
18+
19+
# empty named tuple
20+
NamedTuple.new.class.json_schema.should eq({type: "object", properties: {} of Symbol => Nil})
21+
1622
Array(String | Int32).json_schema.should eq({
1723
type: "array",
1824
items: {

spec/spec_helper.cr

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,6 @@ end
3131

3232
class TestGenericInheritance < Hash(String, Int32)
3333
end
34+
35+
class SuperArray < Array(Int32)
36+
end

src/json-schema.cr

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -61,50 +61,50 @@ module JSON
6161

6262
{% if klass <= Array || klass <= Set %}
6363
{% if klass.type_vars.size == 1 %}
64-
has_items = ::JSON::Schema.introspect {{klass.type_vars[0]}}
64+
%has_items = ::JSON::Schema.introspect({{klass.type_vars[0]}})
65+
{type: "array", items: %has_items}
6566
{% else %}
6667
# handle inheritance (no access to type_var / unknown value)
67-
has_items = NamedTuple.new
68-
{% end %}
69-
if has_items.empty?
70-
%klass = {{klass}}
68+
%klass = {{klass.ancestors[0]}}
7169
%klass.responds_to?(:json_schema) ? %klass.json_schema : {type: "array"}
72-
else
73-
{type: "array", items: has_items}
74-
end
70+
{% end %}
7571
{% elsif klass.union? %}
7672
{ anyOf: [
7773
{% for type in klass.union_types %}
7874
::JSON::Schema.introspect({{type}}),
7975
{% end %}
8076
]}
8177
{% elsif klass_name.starts_with? "Tuple(" %}
82-
has_items = [
78+
%has_items = [
8379
{% for generic in klass.type_vars %}
8480
::JSON::Schema.introspect({{generic}}),
8581
{% end %}
8682
]
87-
{type: "array", items: has_items}
83+
{type: "array", items: %has_items}
8884
{% elsif klass_name.starts_with? "NamedTuple(" %}
89-
{type: "object", properties: {
90-
{% for key in klass.keys %}
91-
{{key.id}}: ::JSON::Schema.introspect({{klass[key].resolve.name}}),
92-
{% end %}
93-
},
94-
{% required = [] of String %}
95-
{% for key in klass.keys %}
96-
{% if !klass[key].resolve.nilable? %}
97-
{% required << key.id.stringify %}
85+
{% if klass.keys.empty? %}
86+
{type: "object", properties: {} of Symbol => Nil}
87+
{% else %}
88+
{type: "object", properties: {
89+
{% for key in klass.keys %}
90+
{{key.id}}: ::JSON::Schema.introspect({{klass[key].resolve.name}}),
9891
{% end %}
99-
{% end %}
100-
{% if !required.empty? %}
101-
required: [
102-
{% for key in required %}
103-
{{key}},
92+
},
93+
{% required = [] of String %}
94+
{% for key in klass.keys %}
95+
{% if !klass[key].resolve.nilable? %}
96+
{% required << key.id.stringify %}
97+
{% end %}
10498
{% end %}
105-
]
106-
{% end %}
107-
}
99+
{% if !required.empty? %}
100+
required: [
101+
{% for key in required %}
102+
{{key}},
103+
{% end %}
104+
]
105+
{% end %}
106+
}
107+
{% end %}
108108
{% elsif klass < Enum %}
109109
{type: "string", enum: {{klass.constants.map(&.stringify.underscore)}} }
110110
{% elsif klass <= String || klass <= Symbol %}
@@ -133,7 +133,7 @@ module JSON
133133
%klass.json_schema
134134
else
135135
# anything will validate (JSON::Any)
136-
NamedTuple.new
136+
{ type: "object" }
137137
end
138138
{% end %}
139139
{% end %}

0 commit comments

Comments
 (0)