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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9d75f4e95d037fa1516504cff963dca3409cc1b45233fe6a46bab2155939e249
4
- data.tar.gz: 3e6626ac1b7796eb94cbbdabe57df7f9b286947018133ef4cdac93f241e8214a
3
+ metadata.gz: 1f68753b0fe40a759d233878a9ca2a96d227ba769d8e1d167255f48e538eb075
4
+ data.tar.gz: e6ba6a151038cf393c099784ce7b3bd358685de018308e3e437af0d54cfcf438
5
5
  SHA512:
6
- metadata.gz: 4fdf2b1bc207755bb94dc88f001d4d668e7b7506284358b8f08531f15869d1feb1b7eebcd5cced2277e2cba42ebdae907f254668f13f7d9295db4c42298a531a
7
- data.tar.gz: 3d6a20ef5a0218e2ac75233792dc844389853bfb24167d3f9ec0f4919f00a1e7623aa7a5801c10649cb02d8c30c0eb9e781901da2d488ebc4134b3492e1bdef5
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
- api_response = Response.new(
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
- json: api_response,
190
+ plain: result.to_s,
197
191
  status: status,
198
192
  content_type: media_type.to_s
199
193
  )
200
- elsif media_type == Media::Type::TEXT_PLAIN
201
- render(
202
- plain: result,
203
- status: status,
204
- content_type: media_type.to_s
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
- elsif media_type == Media::Type::APPLICATION_JSON_SEQ
207
- self.content_type = media_type.to_s
208
- response.status = status
209
-
210
- response.stream.tap do |stream|
211
- api_response.write_json_seq_to(stream)
212
- ensure
213
- stream.close
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
@@ -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
@@ -4,9 +4,9 @@ module Jsapi
4
4
  module JSON
5
5
  # Represents a JSON array.
6
6
  class Array < Value
7
- def initialize(array, schema, context: nil)
7
+ def initialize(value, schema, context: nil)
8
8
  super(schema)
9
- @json_values = Array(array).map do |item|
9
+ @json_values = Array(value).map do |item|
10
10
  JSON.wrap(item, schema.items, context: context)
11
11
  end
12
12
  end
@@ -8,7 +8,14 @@ module Jsapi
8
8
 
9
9
  def initialize(value, schema)
10
10
  super(schema)
11
- @value = schema.convert(value.to_i)
11
+ @value = schema.convert(
12
+ begin
13
+ value.to_i
14
+ rescue StandardError => _e
15
+ self.malformed = true
16
+ 0
17
+ end
18
+ )
12
19
  end
13
20
  end
14
21
  end
@@ -8,7 +8,14 @@ module Jsapi
8
8
 
9
9
  def initialize(value, schema)
10
10
  super(schema)
11
- @value = schema.convert(value.to_f)
11
+ @value = schema.convert(
12
+ begin
13
+ value.to_f
14
+ rescue StandardError => _e
15
+ self.malformed = true
16
+ 0.0
17
+ end
18
+ )
12
19
  end
13
20
  end
14
21
  end
@@ -10,7 +10,15 @@ module Jsapi
10
10
 
11
11
  attr_reader :raw_additional_attributes, :raw_attributes
12
12
 
13
- def initialize(hash, schema, context: nil)
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
 
@@ -21,23 +21,16 @@ module Jsapi
21
21
  else
22
22
  value.to_s
23
23
  end
24
- rescue StandardError => e
25
- @error = e
24
+ rescue StandardError => _e
25
+ self.malformed = true
26
26
  value
27
27
  end
28
- @value = schema.convert(@value) unless defined? @error
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
@@ -13,7 +13,7 @@ module Jsapi
13
13
  @schema = schema
14
14
  end
15
15
 
16
- # Used by #validate to test whether or not it is empty.
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
- # Used by #validate to test whether or not it is null.
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(object, schema, context: nil)
17
- object = schema.default_value(context: context) if object.nil?
18
- return Null.new(schema) if object.nil?
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(object, schema, context: context)
22
+ Array.new(value, schema, context: context)
23
23
  when 'boolean'
24
- Boolean.new(object, schema)
24
+ Boolean.new(value, schema)
25
25
  when 'integer'
26
- Integer.new(object, schema)
26
+ Integer.new(value, schema)
27
27
  when 'number'
28
- Number.new(object, schema)
28
+ Number.new(value, schema)
29
29
  when 'object'
30
- Object.new(object, schema, context: context)
30
+ Object.new(value, schema, context: context)
31
31
  when 'string'
32
- String.new(object, schema)
32
+ String.new(value, schema)
33
33
  else
34
34
  raise "invalid type: #{schema.type}"
35
35
  end
@@ -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.presence
133
+ end
133
134
  end
134
135
  end
135
136
 
data/lib/jsapi/version.rb CHANGED
@@ -5,6 +5,6 @@ module Jsapi
5
5
  # NOTE: See https://bundler.io/guides/creating_gem.html
6
6
 
7
7
  # The current GEM version.
8
- VERSION = '2.1'
8
+ VERSION = '2.1.2'
9
9
  end
10
10
  end
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: '2.1'
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-09-02 00:00:00.000000000 Z
11
+ date: 2026-10-21 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email: denis@dmgoeller.de