@@ -57,6 +57,7 @@ module JSON
57
57
{% format_hint = (args && args[:format ]) % }
58
58
{% type_override = (args && args[:type ]) % }
59
59
{% pattern = (args && args[:pattern ]) % }
60
+ {% description = (args && args[:description ]) % }
60
61
61
62
{% arg_name = klass.stringify % }
62
63
{% if ! arg_name.starts_with?(" Union" ) && arg_name.includes?(" |" ) % }
@@ -69,16 +70,16 @@ module JSON
69
70
{% if klass <= Array || klass <= Set % }
70
71
{% if klass.type_vars.size == 1 % }
71
72
% has_items = ::JSON ::Schema .introspect({{klass.type_vars[0 ]}}, nil , {{openapi}})
72
- {type: " array" , items: % has_items}
73
+ {type: " array" { % if description % }, description: {{description}}{ % end % } , items: % has_items}
73
74
{% else % }
74
75
# handle inheritance (no access to type_var / unknown value)
75
76
% klass = {{klass.ancestors[0 ]}}
76
- % klass.responds_to?(:json_schema ) ? % klass.json_schema : {type: " array" }
77
+ % klass.responds_to?(:json_schema ) ? % klass.json_schema({{openapi}}) : { type: " array" { % if description % }, description: {{description}}{ % end % } }
77
78
{% end % }
78
79
{% elsif klass.union? && ! openapi.nil? && nillable && klass.union_types.size == 2 % }
79
80
{% for type in klass.union_types % }
80
81
{% if type .stringify != " Nil" % }
81
- ::JSON ::Schema .introspect({{type }}, nil , {{openapi}}).merge({
82
+ ::JSON ::Schema .introspect({{type }}, {{args}} , {{openapi}}).merge({
82
83
nullable: true
83
84
})
84
85
{% end % }
@@ -90,19 +91,19 @@ module JSON
90
91
::JSON ::Schema .introspect({{type }}, nil , {{openapi}}),
91
92
{% end % }
92
93
{% end % }
93
- ]{% if ! openapi.nil? && nillable % }, nullable: true {% end % } }
94
+ ]{% if ! openapi.nil? && nillable % }, nullable: true {% end % }{ % if description % }, description: {{description}}{ % end % } }
94
95
{% elsif klass_name.starts_with? " Tuple(" % }
95
96
% has_items = [
96
97
{% for generic in klass.type_vars % }
97
98
::JSON ::Schema .introspect({{generic}}, nil , {{openapi}}),
98
99
{% end % }
99
100
]
100
- {type: " array" , items: % has_items}
101
+ {type: " array" { % if description % }, description: {{description}}{ % end % } , items: % has_items}
101
102
{% elsif klass_name.starts_with? " NamedTuple(" % }
102
103
{% if klass.keys.empty? % }
103
- {type: " object" , properties: {} of Symbol => Nil }
104
+ {type: " object" { % if description % }, description: {{description}}{ % end % } , properties: {} of Symbol => Nil }
104
105
{% else % }
105
- {type: " object" , properties: {
106
+ {type: " object" { % if description % }, description: {{description}}{ % end % } , properties: {
106
107
{% for key in klass.keys % }
107
108
{{key.id}}: ::JSON ::Schema .introspect({{klass[key].resolve.name}}, nil , {{openapi}}),
108
109
{% end % }
@@ -123,37 +124,37 @@ module JSON
123
124
}
124
125
{% end % }
125
126
{% elsif klass < Enum % }
126
- {type: " string" , enum: {{klass.constants.map(& .stringify.underscore)}} }
127
+ {type: " string" , enum: {{klass.constants.map(& .stringify.underscore)}}{ % if description % }, description: {{description}}{ % end % } }
127
128
{% elsif klass <= String || klass <= Symbol % }
128
129
{% min_length = (args && args[:min_length ]) % }
129
130
{% max_length = (args && args[:max_length ]) % }
130
- { type: {{type_override || " string" }}{% if format_hint % }, format: {{format_hint}}{% end % }{% if pattern % }, pattern: {{pattern}}{% end % }{% if min_length % }, minLength: {{min_length}}{% end % }{% if max_length % }, maxLength: {{max_length}}{% end % } }
131
+ { type: {{type_override || " string" }}{% if format_hint % }, format: {{format_hint}}{% end % }{% if pattern % }, pattern: {{pattern}}{% end % }{% if min_length % }, minLength: {{min_length}}{% end % }{% if max_length % }, maxLength: {{max_length}}{% end % }{ % if description % }, description: {{description}}{ % end % } }
131
132
{% elsif klass <= Bool % }
132
- { type: {{type_override || " boolean" }}{% if format_hint % }, format: {{format_hint}}{% end % } }
133
+ { type: {{type_override || " boolean" }}{% if format_hint % }, format: {{format_hint}}{% end % }{ % if description % }, description: {{description}}{ % end % } }
133
134
{% elsif klass <= Int || klass <= Float % }
134
135
{% multiple_of = (args && args[:multiple_of ]) % }
135
136
{% minimum = (args && args[:minimum ]) % }
136
137
{% exclusive_minimum = (args && args[:exclusive_minimum ]) % }
137
138
{% maximum = (args && args[:maximum ]) % }
138
139
{% exclusive_maximum = (args && args[:exclusive_maximum ]) % }
139
140
{% if klass <= Int % }
140
- { type: {{type_override || " integer" }}, format: {{format_hint || klass.stringify}}{% if multiple_of % }, multipleOf: {{multiple_of}}{% end % }{% if minimum % }, minimum: {{minimum}}{% end % }{% if exclusive_minimum % }, exclusiveMinimum: {{exclusive_minimum}}{% end % }{% if maximum % }, maximum: {{maximum}}{% end % }{% if exclusive_maximum % }, exclusiveMaximum: {{exclusive_maximum}}{% end % } }
141
+ { type: {{type_override || " integer" }}, format: {{format_hint || klass.stringify}}{% if multiple_of % }, multipleOf: {{multiple_of}}{% end % }{% if minimum % }, minimum: {{minimum}}{% end % }{% if exclusive_minimum % }, exclusiveMinimum: {{exclusive_minimum}}{% end % }{% if maximum % }, maximum: {{maximum}}{% end % }{% if exclusive_maximum % }, exclusiveMaximum: {{exclusive_maximum}}{% end % }{ % if description % }, description: {{description}}{ % end % } }
141
142
{% elsif klass <= Float % }
142
- { type: {{type_override || " number" }}, format: {{format_hint || klass.stringify}}{% if multiple_of % }, multipleOf: {{multiple_of}}{% end % }{% if minimum % }, minimum: {{minimum}}{% end % }{% if exclusive_minimum % }, exclusiveMinimum: {{exclusive_minimum}}{% end % }{% if maximum % }, maximum: {{maximum}}{% end % }{% if exclusive_maximum % }, exclusiveMaximum: {{exclusive_maximum}}{% end % } }
143
+ { type: {{type_override || " number" }}, format: {{format_hint || klass.stringify}}{% if multiple_of % }, multipleOf: {{multiple_of}}{% end % }{% if minimum % }, minimum: {{minimum}}{% end % }{% if exclusive_minimum % }, exclusiveMinimum: {{exclusive_minimum}}{% end % }{% if maximum % }, maximum: {{maximum}}{% end % }{% if exclusive_maximum % }, exclusiveMaximum: {{exclusive_maximum}}{% end % }{ % if description % }, description: {{description}}{ % end % } }
143
144
{% end % }
144
145
{% elsif klass <= Nil % }
145
- { type: {{type_override || " null" }}{% if format_hint % }, format: {{format_hint}}{% end % } }
146
+ { type: {{type_override || " null" }}{% if format_hint % }, format: {{format_hint}}{% end % }{ % if description % }, description: {{description}}{ % end % } }
146
147
{% elsif klass <= Time % }
147
- { type: {{type_override || " string" }}, format: {{format_hint || " date-time" }}{% if pattern % }, pattern: {{pattern}}{% end % } }
148
+ { type: {{type_override || " string" }}, format: {{format_hint || " date-time" }}{% if pattern % }, pattern: {{pattern}}{% end % }{ % if description % }, description: {{description}}{ % end % } }
148
149
{% elsif klass <= UUID % }
149
- { type: {{type_override || " string" }}, format: {{format_hint || " uuid" }}{% if pattern % }, pattern: {{pattern}}{% end % } }
150
+ { type: {{type_override || " string" }}, format: {{format_hint || " uuid" }}{% if pattern % }, pattern: {{pattern}}{% end % }{ % if description % }, description: {{description}}{ % end % } }
150
151
{% elsif klass <= Hash % }
151
152
{% if klass.type_vars.size == 2 % }
152
153
{ type: " object" , additionalProperties: ::JSON ::Schema .introspect({{klass.type_vars[1 ]}}, nil , {{openapi}}) }
153
154
{% else % }
154
155
# As inheritance might include the type_vars it's hard to work them out
155
156
% klass = {{klass.ancestors[0 ]}}
156
- % klass.responds_to?(:json_schema ) ? % klass.json_schema({{openapi}}) : { type: " object" }
157
+ % klass.responds_to?(:json_schema ) ? % klass.json_schema({{openapi}}) : { type: " object" { % if description % }, description: {{description}}{ % end % } }
157
158
{% end % }
158
159
{% elsif klass.ancestors.includes? JSON ::Serializable % }
159
160
{{klass}}.json_schema({{openapi}})
@@ -163,7 +164,7 @@ module JSON
163
164
% klass.json_schema({{openapi}})
164
165
else
165
166
# anything will validate (JSON::Any)
166
- { type: " object" }
167
+ { type: " object" { % if description % }, description: {{description}}{ % end % } }
167
168
end
168
169
{% end % }
169
170
{% end % }
0 commit comments