Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
* [#2678](https://github.com/ruby-grape/grape/pull/2678): Update rubocop to 1.86.0 and autocorrect offenses - [@ericproulx](https://github.com/ericproulx).
* [#2682](https://github.com/ruby-grape/grape/pull/2682): Fix `Style/OptionalBooleanParameter` offenses - [@ericproulx](https://github.com/ericproulx).
* [#2699](https://github.com/ruby-grape/grape/pull/2699): Fix `Grape::Validations::Types::CustomTypeCoercer` dropping symbolized hash keys for `Array`/`Set` types; refactor the class for readability - [@ericproulx](https://github.com/ericproulx).
* [#2758](https://github.com/ruby-grape/grape/pull/2758): Fix `VariantCollectionCoercer#to_s` to return `"Array[Type, ...]"` notation instead of a raw object string - [@bogdan](https://github.com/bogdan).
* [#2700](https://github.com/ruby-grape/grape/pull/2700): Fix README typos, remove obsolete Ruby 2.4 / Fixnum section, and replace incorrect `requires + values + allow_blank` note with a correct one covering `optional + values` semantics (closes #2631) - [@ericproulx](https://github.com/ericproulx).
* [#2703](https://github.com/ruby-grape/grape/pull/2703): Catch exceptions raised inside `rescue_from` blocks; new `rescue_from :internal_grape_exceptions` opt-in for unrecognised internal errors (resolves [#2482](https://github.com/ruby-grape/grape/issues/2482)) - [@ericproulx](https://github.com/ericproulx).
* [#2706](https://github.com/ruby-grape/grape/pull/2706): Fix `optional :foo, message: 'oops'` raising `UnknownValidator` - [@ericproulx](https://github.com/ericproulx).
Expand Down
8 changes: 8 additions & 0 deletions lib/grape/validations/types/variant_collection_coercer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ def initialize(types, method = nil)
@member_coercer = MultipleTypeCoercer.new types, method
end

# Returns the Grape DSL notation for this coercer, e.g. "Array[Integer, String]".
# Distinct from the plain-array string "[Integer, String]" produced by the
# +types:+ keyword, which lets documentation tools tell the two apart.
def to_s
container = @types.is_a?(Set) ? 'Set' : 'Array'
"#{container}[#{@types.join(', ')}]"
end

# Coerce the given value.
#
# @param value [Array<String>] collection of values to be coerced
Expand Down
2 changes: 2 additions & 0 deletions spec/grape/validations/validations_spec_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,14 @@
it 'wraps a multiple type from :type in a VariantCollectionCoercer' do
multi_spec = described_class.from(type: [Integer, String])
expect(multi_spec.coerce_type).to be_a(Grape::Validations::Types::VariantCollectionCoercer)
expect(multi_spec.coerce_type.to_s).to eq('Array[Integer, String]')
expect(multi_spec.coerce_method).to be_nil
end

it 'extracts :types as a plain coerce list' do
spec = described_class.from(types: [Integer, String])
expect(spec.coerce_type).to eq([Integer, String])
expect(spec.coerce_type.to_s).to eq('[Integer, String]')
end
end

Expand Down