jsapi 2.1 → 2.1.2
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.
- checksums.yaml +4 -4
- data/lib/jsapi/controller/methods.rb +23 -21
- data/lib/jsapi/dsl/base.rb +5 -0
- data/lib/jsapi/dsl/shared_operation_methods.rb +15 -0
- data/lib/jsapi/json/array.rb +2 -2
- data/lib/jsapi/json/integer.rb +8 -1
- data/lib/jsapi/json/number.rb +8 -1
- data/lib/jsapi/json/object.rb +9 -1
- data/lib/jsapi/json/string.rb +3 -10
- data/lib/jsapi/json/value.rb +14 -2
- data/lib/jsapi/json.rb +9 -9
- data/lib/jsapi/media/type.rb +5 -0
- data/lib/jsapi/meta/schema/object.rb +10 -9
- data/lib/jsapi/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 1f68753b0fe40a759d233878a9ca2a96d227ba769d8e1d167255f48e538eb075
|
|
4
|
+
data.tar.gz: e6ba6a151038cf393c099784ce7b3bd358685de018308e3e437af0d54cfcf438
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: ef47ff95a63d481af9b7eb169487ad7e05d6a653a5a6d2bdefb8499802a43531e2d729bce9a74701006b59c66f24241d9a9dae7b2a7e0f95e5788e81cc73dacb
|
|
7
|
+
data.tar.gz: 9cf392df9e3d8bd4913faa8c3594c3e10399c9600a165e96f8f8ae91c2c240f60e7e77518ac018d5d327d6c3bde4aa9e9119babda7ef0959e285a0c5365741ce
|
|
@@ -185,32 +185,34 @@ module Jsapi
|
|
|
185
185
|
|
|
186
186
|
result = _api_before_rendering(operation_name, result, api_params)
|
|
187
187
|
|
|
188
|
-
|
|
189
|
-
result,
|
|
190
|
-
content_model,
|
|
191
|
-
omit: omit,
|
|
192
|
-
locale: response_model.locale
|
|
193
|
-
)
|
|
194
|
-
if media_type.json?
|
|
188
|
+
if media_type == Media::Type::TEXT_PLAIN
|
|
195
189
|
render(
|
|
196
|
-
|
|
190
|
+
plain: result.to_s,
|
|
197
191
|
status: status,
|
|
198
192
|
content_type: media_type.to_s
|
|
199
193
|
)
|
|
200
|
-
elsif media_type
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
194
|
+
elsif media_type.json? || media_type.json_seq?
|
|
195
|
+
api_response = Response.new(
|
|
196
|
+
result,
|
|
197
|
+
content_model,
|
|
198
|
+
omit: omit,
|
|
199
|
+
locale: response_model.locale
|
|
205
200
|
)
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
201
|
+
if media_type.json?
|
|
202
|
+
render(
|
|
203
|
+
json: api_response,
|
|
204
|
+
status: status,
|
|
205
|
+
content_type: media_type.to_s
|
|
206
|
+
)
|
|
207
|
+
else
|
|
208
|
+
self.content_type = media_type.to_s
|
|
209
|
+
response.status = status
|
|
210
|
+
|
|
211
|
+
response.stream.tap do |stream|
|
|
212
|
+
api_response.write_json_seq_to(stream)
|
|
213
|
+
ensure
|
|
214
|
+
stream.close
|
|
215
|
+
end
|
|
214
216
|
end
|
|
215
217
|
end
|
|
216
218
|
end
|
data/lib/jsapi/dsl/base.rb
CHANGED
|
@@ -89,6 +89,11 @@ module Jsapi
|
|
|
89
89
|
|
|
90
90
|
private
|
|
91
91
|
|
|
92
|
+
def deprecation_warning(message)
|
|
93
|
+
caller_location = caller.find { |line| line.exclude?('lib/jsapi') }
|
|
94
|
+
warn("[DEPRECATION WARNING] #{message}\nDefined at: #{caller_location}")
|
|
95
|
+
end
|
|
96
|
+
|
|
92
97
|
def define(*args, &block)
|
|
93
98
|
result = block.call
|
|
94
99
|
result.freeze_attributes if result.respond_to?(:freeze_attributes)
|
|
@@ -81,6 +81,14 @@ module Jsapi
|
|
|
81
81
|
if name && (keywords.any? || block)
|
|
82
82
|
|
|
83
83
|
keywords = { ref: name } if name
|
|
84
|
+
|
|
85
|
+
if (%i[type ref schema contents].none? { |key| keywords.key?(key) }) && !block
|
|
86
|
+
deprecation_warning(
|
|
87
|
+
'Starting with Jsapi 3.0, a `request_body` without :type will no longer ' \
|
|
88
|
+
'define an :object schema. Please specify `type: :object` explicitly.'
|
|
89
|
+
)
|
|
90
|
+
end
|
|
91
|
+
|
|
84
92
|
@meta_model.request_body = keywords
|
|
85
93
|
|
|
86
94
|
@meta_model.request_body.tap do |request_body|
|
|
@@ -123,6 +131,13 @@ module Jsapi
|
|
|
123
131
|
raise Error, "name can't be specified together with keywords or a block"
|
|
124
132
|
end
|
|
125
133
|
|
|
134
|
+
if (%i[type ref schema contents].none? { |key| keywords.key?(key) }) && !block
|
|
135
|
+
deprecation_warning(
|
|
136
|
+
'Starting with Jsapi 3.0, a `response` without :type will no longer ' \
|
|
137
|
+
'define an :object schema. Please specify `type: :object` explicitly.'
|
|
138
|
+
)
|
|
139
|
+
end
|
|
140
|
+
|
|
126
141
|
@meta_model.add_response(status, keywords).tap do |response_model|
|
|
127
142
|
Response.new(response_model, &block) if block
|
|
128
143
|
end
|
data/lib/jsapi/json/array.rb
CHANGED
|
@@ -4,9 +4,9 @@ module Jsapi
|
|
|
4
4
|
module JSON
|
|
5
5
|
# Represents a JSON array.
|
|
6
6
|
class Array < Value
|
|
7
|
-
def initialize(
|
|
7
|
+
def initialize(value, schema, context: nil)
|
|
8
8
|
super(schema)
|
|
9
|
-
@json_values = Array(
|
|
9
|
+
@json_values = Array(value).map do |item|
|
|
10
10
|
JSON.wrap(item, schema.items, context: context)
|
|
11
11
|
end
|
|
12
12
|
end
|
data/lib/jsapi/json/integer.rb
CHANGED
data/lib/jsapi/json/number.rb
CHANGED
data/lib/jsapi/json/object.rb
CHANGED
|
@@ -10,7 +10,15 @@ module Jsapi
|
|
|
10
10
|
|
|
11
11
|
attr_reader :raw_additional_attributes, :raw_attributes
|
|
12
12
|
|
|
13
|
-
def initialize(
|
|
13
|
+
def initialize(value, schema, context: nil)
|
|
14
|
+
hash =
|
|
15
|
+
begin
|
|
16
|
+
value.to_h
|
|
17
|
+
rescue StandardError => _e
|
|
18
|
+
self.malformed = true
|
|
19
|
+
{}
|
|
20
|
+
end
|
|
21
|
+
|
|
14
22
|
schema = schema.resolve_schema(hash, context: context)
|
|
15
23
|
properties = schema.resolve_properties(context: context)
|
|
16
24
|
|
data/lib/jsapi/json/string.rb
CHANGED
|
@@ -21,23 +21,16 @@ module Jsapi
|
|
|
21
21
|
else
|
|
22
22
|
value.to_s
|
|
23
23
|
end
|
|
24
|
-
rescue StandardError =>
|
|
25
|
-
|
|
24
|
+
rescue StandardError => _e
|
|
25
|
+
self.malformed = true
|
|
26
26
|
value
|
|
27
27
|
end
|
|
28
|
-
@value = schema.convert(@value) unless
|
|
28
|
+
@value = schema.convert(@value) unless malformed?
|
|
29
29
|
end
|
|
30
30
|
|
|
31
31
|
def empty? # :nodoc:
|
|
32
32
|
value.blank?
|
|
33
33
|
end
|
|
34
|
-
|
|
35
|
-
def validate(errors) # :nodoc:
|
|
36
|
-
return super unless defined? @error
|
|
37
|
-
|
|
38
|
-
errors.add(:base, :invalid)
|
|
39
|
-
false
|
|
40
|
-
end
|
|
41
34
|
end
|
|
42
35
|
end
|
|
43
36
|
end
|
data/lib/jsapi/json/value.rb
CHANGED
|
@@ -13,7 +13,7 @@ module Jsapi
|
|
|
13
13
|
@schema = schema
|
|
14
14
|
end
|
|
15
15
|
|
|
16
|
-
#
|
|
16
|
+
# Called by #validate to test whether or not it is empty.
|
|
17
17
|
# Returns false by default.
|
|
18
18
|
def empty?
|
|
19
19
|
false
|
|
@@ -23,7 +23,7 @@ module Jsapi
|
|
|
23
23
|
"#<#{self.class} #{value.inspect}>"
|
|
24
24
|
end
|
|
25
25
|
|
|
26
|
-
#
|
|
26
|
+
# Called by #validate to test whether or not it is null.
|
|
27
27
|
# Returns false by default.
|
|
28
28
|
def null?
|
|
29
29
|
false
|
|
@@ -37,6 +37,10 @@ module Jsapi
|
|
|
37
37
|
# Validates it against #schema. Returns true if it is valid, false otherwise.
|
|
38
38
|
# Detected errors are added to +errors+.
|
|
39
39
|
def validate(errors)
|
|
40
|
+
if malformed?
|
|
41
|
+
errors.add(:base, :invalid)
|
|
42
|
+
return false
|
|
43
|
+
end
|
|
40
44
|
unless schema.existence.reach?(self)
|
|
41
45
|
errors.add(:base, :blank)
|
|
42
46
|
return false
|
|
@@ -47,6 +51,14 @@ module Jsapi
|
|
|
47
51
|
validation.validate(value, errors)
|
|
48
52
|
end.all?
|
|
49
53
|
end
|
|
54
|
+
|
|
55
|
+
protected
|
|
56
|
+
|
|
57
|
+
attr_writer :malformed
|
|
58
|
+
|
|
59
|
+
def malformed?
|
|
60
|
+
@malformed == true
|
|
61
|
+
end
|
|
50
62
|
end
|
|
51
63
|
end
|
|
52
64
|
end
|
data/lib/jsapi/json.rb
CHANGED
|
@@ -13,23 +13,23 @@ module Jsapi
|
|
|
13
13
|
# Provides a DOM for JSON values.
|
|
14
14
|
module JSON
|
|
15
15
|
class << self
|
|
16
|
-
def wrap(
|
|
17
|
-
|
|
18
|
-
return Null.new(schema) if
|
|
16
|
+
def wrap(value, schema, context: nil)
|
|
17
|
+
value = schema.default_value(context: context) if value.nil?
|
|
18
|
+
return Null.new(schema) if value.nil?
|
|
19
19
|
|
|
20
20
|
case schema.type
|
|
21
21
|
when 'array'
|
|
22
|
-
Array.new(
|
|
22
|
+
Array.new(value, schema, context: context)
|
|
23
23
|
when 'boolean'
|
|
24
|
-
Boolean.new(
|
|
24
|
+
Boolean.new(value, schema)
|
|
25
25
|
when 'integer'
|
|
26
|
-
Integer.new(
|
|
26
|
+
Integer.new(value, schema)
|
|
27
27
|
when 'number'
|
|
28
|
-
Number.new(
|
|
28
|
+
Number.new(value, schema)
|
|
29
29
|
when 'object'
|
|
30
|
-
Object.new(
|
|
30
|
+
Object.new(value, schema, context: context)
|
|
31
31
|
when 'string'
|
|
32
|
-
String.new(
|
|
32
|
+
String.new(value, schema)
|
|
33
33
|
else
|
|
34
34
|
raise "invalid type: #{schema.type}"
|
|
35
35
|
end
|
data/lib/jsapi/media/type.rb
CHANGED
|
@@ -65,6 +65,11 @@ module Jsapi
|
|
|
65
65
|
(type.in?(%w[application text]) && subtype == 'json') ||
|
|
66
66
|
subtype.end_with?('+json')
|
|
67
67
|
end
|
|
68
|
+
|
|
69
|
+
# Returns true if it is <code>"application/json-seq"</code>.
|
|
70
|
+
def json_seq?
|
|
71
|
+
type == 'application' && subtype == 'json-seq'
|
|
72
|
+
end
|
|
68
73
|
end
|
|
69
74
|
end
|
|
70
75
|
end
|
|
@@ -100,15 +100,16 @@ module Jsapi
|
|
|
100
100
|
property_value = property.reader.call(value)
|
|
101
101
|
property_value = property_schema.default if property_value.nil?
|
|
102
102
|
|
|
103
|
+
property_value = property_schema.jsonify(
|
|
104
|
+
property_value,
|
|
105
|
+
context: context,
|
|
106
|
+
omit: omit
|
|
107
|
+
)
|
|
103
108
|
if property_schema.omittable?
|
|
104
109
|
next if omit == :nil && property_value.nil?
|
|
105
110
|
next if omit == :empty && (property_value.nil? || property_value.try(:empty?))
|
|
106
111
|
end
|
|
107
|
-
|
|
108
|
-
properties[property.name] = property_schema.jsonify(
|
|
109
|
-
property_value,
|
|
110
|
-
context: context
|
|
111
|
-
)
|
|
112
|
+
properties[property.name] = property_value
|
|
112
113
|
rescue JsonifyError => e
|
|
113
114
|
raise e.prepend(".#{property.name}")
|
|
114
115
|
end
|
|
@@ -116,20 +117,20 @@ module Jsapi
|
|
|
116
117
|
if additional_properties
|
|
117
118
|
additional_properties_schema = additional_properties.schema
|
|
118
119
|
|
|
119
|
-
additional_properties.source.call(value)&.each
|
|
120
|
-
do |property_name, property_value|
|
|
120
|
+
additional_properties.source.call(value)&.each do |property_name, property_value|
|
|
121
121
|
property_name = property_name.to_s
|
|
122
122
|
next if properties.key?(property_name)
|
|
123
123
|
|
|
124
124
|
properties[property_name] = additional_properties_schema.jsonify(
|
|
125
125
|
property_value,
|
|
126
|
-
context: context
|
|
126
|
+
context: context,
|
|
127
|
+
omit: omit
|
|
127
128
|
)
|
|
128
129
|
rescue JsonifyError => e
|
|
129
130
|
raise e.prepend(".#{property_name}")
|
|
130
131
|
end
|
|
131
132
|
end
|
|
132
|
-
end
|
|
133
|
+
end
|
|
133
134
|
end
|
|
134
135
|
end
|
|
135
136
|
|
data/lib/jsapi/version.rb
CHANGED
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: jsapi
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version:
|
|
4
|
+
version: 2.1.2
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Denis Göller
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2026-
|
|
11
|
+
date: 2026-10-21 00:00:00.000000000 Z
|
|
12
12
|
dependencies: []
|
|
13
13
|
description:
|
|
14
14
|
email: denis@dmgoeller.de
|