Fix type: [A, B] + values: [...] raising IncompatibleOptionValues at definition time#2814
Open
ericproulx wants to merge 1 commit into
Open
Fix type: [A, B] + values: [...] raising IncompatibleOptionValues at definition time#2814ericproulx wants to merge 1 commit into
ericproulx wants to merge 1 commit into
Conversation
…definition time Sibling of #2812: parse_coerce stores a VariantCollectionCoercer in @coerce_type for a multiple-type declaration, and the values coherence check received the wrapper instead of the declared list — Object#=== matches nothing, so any type: [A, B] + values:/except_values: param raised at definition time (accepted in 3.2.1). Feed the check the declared member types; the wrapper stays in place for runtime coercion and documentation. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
ericproulx
force-pushed
the
fix-multiple-type-values-check
branch
from
July 25, 2026 18:11
9b43051 to
e5a88cc
Compare
Danger ReportNo issues found. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Sibling of #2812 (fixed by #2813): a multiple-type declaration combined with
values:orexcept_values:raisesGrape::Exceptions::IncompatibleOptionValuesat definition time on master, so the API class fails to load. All of these were accepted in 3.2.1 (verified against thev3.2.1tag):type: [Integer, String], values: [1, 2]type: [Integer, String], except_values: [3]type: Set[Integer, String], values: [1]types: [Integer, String], values: [1]The raised message even has a blank type name (
type: is incompatible with values: [1, 2]), which hints at the root cause below.Root cause
Same clobbering family as #2812.
ValidationsSpec#parse_coercestores aTypes::VariantCollectionCoercerinstance in@coerce_typewhenTypes.multiple?(type)is true — correct for runtime coercion and for documentation (to_s→"Array[Integer, String]"), but the definition-time coherence check also reads@coerce_type:VariantCollectionCoercerdoesn't define===, soObject#===(identity) matches nothing and the check can never pass.In 3.2.1 the two roles were separate:
infer_coercionreturned the raw declared type ([Integer, String]) forvalidate_value_coercion/guess_coerce_type/docs, while the wrapper went intovalidations[:coerce]for the runtime validator. TheValidationsSpecrefactor merged both roles into@coerce_type; #2813 un-merged the values-guessed-type leg but not this one.Fix
Mirrors #2813's isolation strategy:
ValidationsSpecgains a privatedeclared_coerce_typethat unwraps aVariantCollectionCoercerback to the declared member-type list, andguess_coerce_type/validate_value_coercionnow receive that.@coerce_typeis untouched, socoerce_options(runtime) andParamsDocumentation(renders"Array[Integer, String]") keep their current behavior.VariantCollectionCoercergains anattr_reader :typesso the declared list is recoverable from the wrapper.The restored check semantics are exactly 3.2.1's: the declared list is
Enumerable, so the existingcoerce_type = coerce_type.first if coerce_type.is_a?(Enumerable)line validates values against the first declared member type. That meanstype: [Integer, String], values: %w[active]still raises — quirky, but it is the historical contract, and this PR deliberately restores rather than redesigns it.Tests
spec/grape/validations/params_scope_spec.rb: six integration cases —values:,except_values:, andSet[...]accepted at definition time; array of allowed values accepted at request time; disallowed value rejected withdoes not have a valid value; incompatible values list still raisesIncompatibleOptionValues.spec/grape/validations/validations_spec_spec.rb: unit coverage that the wrapper stays incoerce_typewhile the values check passes, and that an incompatible list still raises.Full suite: 2375 examples, 0 failures. RuboCop clean.
🤖 Generated with Claude Code