Fix: VariantCollectionCoercer#to_s returns "Array[Type, ...]" notation#2758
Open
bogdan wants to merge 1 commit into
Open
Fix: VariantCollectionCoercer#to_s returns "Array[Type, ...]" notation#2758bogdan wants to merge 1 commit into
bogdan wants to merge 1 commit into
Conversation
Danger ReportWarnings
MarkdownsHere's an example of a CHANGELOG.md entry: * [#2758](https://github.com/ruby-grape/grape/pull/2758): Fix: variantcollectioncoercer#to_s returns "array[type, ...]" notation - [@bogdan](https://github.com/bogdan). |
4cccebe to
cf5ef17
Compare
Without a custom to_s, type: [Integer, String] serialised to a garbage object string in route.params[:type], making it indistinguishable from the VariantCollectionCoercer instance address. Documentation tools (grape-swagger, grape-oas) could not tell it apart from the plain-array "[Integer, String]" produced by the types: keyword. Now type: [Integer, String] produces "Array[Integer, String]" and type: Set[Integer, String] produces "Set[Integer, String]", matching the Grape DSL notation and letting tooling generate the correct schema (array with variant-member items vs oneOf at the parameter level).
cf5ef17 to
9f06e49
Compare
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.
Problem
type: [Integer, String]andtypes: [Integer, String]have different semantics:types: [Integer, String]— the parameter itself can be Integer or String (variant types)type: [Integer, String]/type: Array[Integer, String]— the parameter is always an Array whose members can be Integer or String (variant-member-type collection)However,
VariantCollectionCoercerhad no customto_s, soroute.params[:type]for thetype:form serialised to a garbage object string ("#<Grape::Validations::Types::VariantCollectionCoercer:0x...>"). Documentation tools such as grape-swagger and grape-oas could not distinguish it from the plain"[Integer, String]"produced bytypes:, and could not generate a correct schema for either case.Fix
Add
VariantCollectionCoercer#to_sreturning the Grape DSL notation:type: [Integer, String]→"Array[Integer, String]"type: Set[Integer, String]→"Set[Integer, String]"types: [Integer, String]→"[Integer, String]"(unchanged)This matches the notation users write in the DSL and gives documentation tools an unambiguous signal to emit
{type: array, items: {oneOf: [...]}}rather than a plainoneOf.Compatibility
declared_params_handler.rbreadsroute.params[:type]but only matches'Array','Hash','Set', and the single-type[Type]pattern —"Array[Integer, String]"does not collide with any of those branches./\[(.*)\]$/,/\A\[.*\]\z/) are unaffected.