gitlab-grape-openapi 0.10.0 → 0.11.0

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: baf765aa19cd584ef90ebbeae24fcdf4027ae8d8e4f1bb108bc54b72bbf19a73
4
- data.tar.gz: 300cca6362ed4795b834ab123648a250ee4f514ba207edcb402caa1e9020c615
3
+ metadata.gz: a8fed43316726fbc9d90309ea12724004748b48cd037577400f105a1930c0293
4
+ data.tar.gz: 3d3f0fb427093db126b9ecf7b224e7dde36deda2b05972cd966338c65708c880
5
5
  SHA512:
6
- metadata.gz: 46046c44247a31ab9d7ed9a481999b9227ae13597da907796e0f2b9dc0be8ddb322ccb5e03c7baf214e710bac033ac60eb7f3d1ec6297d73edcddd5d97f9b614
7
- data.tar.gz: 56735aa0a8fb9b9a9f09d22557b3d08cf74035f2d3bf567d2c0782755c9d6cdb3aecd37f34ae9974e6059306daf7ca9ef163f2981c298ff62e2a521e88ccafad
6
+ metadata.gz: 5cb4f5e8fd688369cc985fe95e52d3fdc34580badc923fdec38f3885dd50053c5fc970a642f58eec50689267c1b97bfb14cbdd5a7262f499fce6311661fe334a
7
+ data.tar.gz: 7a40b746a603510daa57da1ba177f45d723706e5232ebb10ebdecab22bd0abe2211d244bb64d8dc65ebf829dc499081e48216bb9cf1494ea676adf20a0919ab1
data/README.md CHANGED
@@ -314,6 +314,24 @@ end
314
314
 
315
315
  An inline `is_array:` takes precedence over the standalone setting.
316
316
 
317
+ A route whose body can be one of several entities lists each of them, with the
318
+ same code, in an Array:
319
+
320
+ ```ruby
321
+ desc 'Search' do
322
+ success [
323
+ { code: 200, model: Entities::Issue, is_array: true },
324
+ { code: 200, model: Entities::Project, is_array: true }
325
+ ]
326
+ end
327
+ ```
328
+
329
+ The response schema then lists them under `anyOf`. When every entry is a
330
+ collection, the union moves to the items (`type: array` with `items: anyOf`),
331
+ so a list that mixes the entities is documented too. `anyOf` rather than
332
+ `oneOf`, because entity schemas list no required properties, so one body can
333
+ match several of them.
334
+
317
335
  Endpoints that return a file or plain text instead declare their media type
318
336
  with `produces` in the `desc` block:
319
337
 
@@ -5,6 +5,7 @@ module Gitlab
5
5
  module Converters
6
6
  class EntityConverter
7
7
  include Concerns::Serializable
8
+ include Concerns::Nullability
8
9
 
9
10
  attr_reader :entity_class, :schema_registry
10
11
 
@@ -119,11 +120,14 @@ module Gitlab
119
120
  nested_required = required_exposure_keys(exposure.nested_exposures)
120
121
  object_schema[:required] = nested_required if nested_required.any?
121
122
 
122
- if documentation[:is_array]
123
- { type: ARRAY_TYPE, description: documentation[:desc], items: object_schema }.compact
124
- else
125
- object_schema.merge(description: documentation[:desc]).compact
126
- end
123
+ property = if documentation[:is_array]
124
+ { type: ARRAY_TYPE, description: documentation[:desc], items: object_schema }
125
+ else
126
+ object_schema.merge(description: documentation[:desc])
127
+ end
128
+
129
+ apply_entity_nullability!(property, exposure)
130
+ property.compact
127
131
  end
128
132
 
129
133
  def inlineable_merge_exposure?(exposure)
@@ -156,9 +160,18 @@ module Gitlab
156
160
  def build_property(exposure)
157
161
  property = extract_basic_attributes(exposure)
158
162
  apply_type_specific_attributes!(property, exposure)
163
+ apply_entity_nullability!(property, exposure)
159
164
  property.compact
160
165
  end
161
166
 
167
+ def apply_entity_nullability!(property, exposure)
168
+ return unless exposure_documentation(exposure)[:nullable]
169
+
170
+ # OpenAPI 3.0 ignores keywords next to `$ref`, so wrap it in `allOf`.
171
+ property[:allOf] = [{ REF_KEY => property.delete(REF_KEY) }] if property.key?(REF_KEY)
172
+ mark_nullable!(property)
173
+ end
174
+
162
175
  def extract_basic_attributes(exposure)
163
176
  documentation = exposure_documentation(exposure)
164
177
  default_value = exposure_default(exposure)
@@ -322,12 +335,25 @@ module Gitlab
322
335
  end
323
336
 
324
337
  # Only `RepresentExposure` instances (i.e. exposures declared with
325
- # `using:`) expose a `using_class_name` reader; for any other exposure
326
- # type there is no nested entity to reference.
338
+ # `using:`) expose a `using_class_name` reader. Other exposures, such as
339
+ # a block that represents its value itself, reference an entity only
340
+ # when their documented `type` names one.
327
341
  def nested_entity_class(exposure)
328
- return unless exposure.respond_to?(:using_class_name)
342
+ return exposure.using_class_name if exposure.respond_to?(:using_class_name)
343
+
344
+ documented_entity_class(exposure) unless block_exposure?(exposure)
345
+ end
346
+
347
+ def documented_entity_class(exposure)
348
+ type = exposure_documentation(exposure)[:type]
349
+ klass = type.is_a?(String) ? constant_or_nil(type.delete_prefix('::')) : type
350
+ klass if self.class.grape_entity?(klass)
351
+ end
329
352
 
330
- exposure.using_class_name
353
+ def constant_or_nil(name)
354
+ Object.const_get(name)
355
+ rescue NameError
356
+ nil
331
357
  end
332
358
 
333
359
  def array_exposure?(exposure)
@@ -166,7 +166,29 @@ module Gitlab
166
166
  is_array: is_array
167
167
  )
168
168
 
169
- @responses[response.status_code] = response.to_h(@schema_registry)
169
+ hash = response.to_h(@schema_registry)
170
+ content = hash[:content][response.content_type]
171
+ existing = @responses.dig(response.status_code, :content, response.content_type, :schema)
172
+ content[:schema] = merge_schemas(existing, content[:schema]) if existing
173
+
174
+ @responses[response.status_code] = hash
175
+ end
176
+
177
+ # Several entities declared for one code (`success [{ code: 200, model: A }, { code: 200, model: B }]`)
178
+ # mean the body is one of them, so they are kept as `anyOf` instead of the last one winning.
179
+ # `anyOf`, not `oneOf`: entity schemas list no required properties, so a body can match several.
180
+ # When every entity is a collection, the union moves to the items: each item is one of them.
181
+ def merge_schemas(existing, added)
182
+ if existing[:type] == 'array' && added[:type] == 'array'
183
+ { type: 'array', items: union_schema(existing[:items], added[:items]) }
184
+ else
185
+ union_schema(existing, added)
186
+ end
187
+ end
188
+
189
+ def union_schema(first, second)
190
+ members = [first, second].flat_map { |schema| schema[:anyOf] || [schema] }.uniq
191
+ members.one? ? members.first : { anyOf: members }
170
192
  end
171
193
 
172
194
  # `content` is only ever passed for the success response, so failure
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Gitlab
4
4
  module GrapeOpenapi
5
- VERSION = "0.10.0"
5
+ VERSION = "0.11.0"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gitlab-grape-openapi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.10.0
4
+ version: 0.11.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - group::api
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2026-11-18 00:00:00.000000000 Z
11
+ date: 2026-11-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: grape