Skip to content

Conversation

@eriklovmo
Copy link
Contributor

@eriklovmo eriklovmo commented Dec 4, 2025

The deleted example was incorrect: you can, in fact, not supply a blank value to the state parameter with such a params block. I don't know the background, maybe it was correct at one point in time.

The example is incorrect: you can, in fact, not supply a blank
value to `state` with that `params` block.
@eriklovmo
Copy link
Contributor Author

I did not update the changelog because it seemed like an insignificant patch. Is that OK?

@eriklovmo eriklovmo marked this pull request as ready for review December 4, 2025 13:11
@ericproulx
Copy link
Contributor

@eriklovmo I do confirm the behavior

# frozen_string_literal: true

require 'bundler/inline'

gemfile(true) do
  source 'https://rubygems.org'
  gem 'grape'
  gem 'rack'
  gem 'minitest'
  gem 'rack-test'
end

require 'minitest/autorun'
require 'rack/test'
require 'grape'

class GrapeAPIBugTest < Minitest::Test
  include Rack::Test::Methods

  def setup
    @api = Class.new(Grape::API) do
      prefix :api
      version 'v1', using: :path
  
      params do
        requires :state, type: Symbol, values: [:active, :inactive]
      end
  
      post '/' do
        'hello'
      end
    end.new
  end

  def test_v1_users_via_api
    options = {
      method: Rack::POST,
      params: {
        state: nil
      }
    }
    env = Rack::MockRequest.env_for('/api/v1', options)
    response = Rack::MockResponse[*@api.call(env)]

    assert_equal 'hello', response.body
    assert_equal 201, response.status
  end
end

It fails

@ericproulx
Copy link
Contributor

But #2185, is mentionning something about optional vs requires. Test is passing when updating the test with optional.

@eriklovmo
Copy link
Contributor Author

@eriklovmo I do confirm the behavior

Thanks!

But #2185, is mentionning something about optional vs requires. Test is passing when updating the test with optional.

OK, this might warrant a clarifying note in the docs. When using optional, validation also passes when given an empty string "", which appears to be coerced into nil. I haven’t looked into the code mechanics yet, but do you think the underlying issue is the ambiguity around "blankness", or is it that we’re conflating optional keys with optional values?

@ericproulx

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants