Skip to content

Conversation

1075461847
Copy link
Contributor

Description

Fixes #92

@1075461847 1075461847 changed the title Fix transform with default value #92 Fix transform with default value Mar 23, 2023
@1075461847
Copy link
Contributor Author

#92

@iMacTia
Copy link
Collaborator

iMacTia commented Mar 27, 2023

@1075461847 thank you for opening this PR.
I'm investigating the issues with CI and will address them separately.
In the meantime, I suspect this change will break when an optional parameter is not provided, but there's no spec to test this scenario. Would you mind adding one and ensure that is passing as expected?
Something like this:

context "when param is optional & not present" do
        it "doesn't transform the value" do
          allow(controller).to receive(:params).and_return({ })
          expect { controller.param! :foo, String, required: true, transform: :upcase }.not_to raise_error
        end
      end

Under the current implementation, I would expect this test to fail with NoMethodError

@1075461847
Copy link
Contributor Author

@iMacTia I think it will throw an InvalidParameterError instead of an NoMethodError when an optional parameter is not provided, the param! method has validate presence before apply transformation

      # validate presence
      if params[name].nil? && options[:required]
        raise InvalidParameterError.new(
          "Parameter #{parameter_name} is required",
          param: parameter_name,
          options: options
        )
      end
    ```

@1075461847
Copy link
Contributor Author

@iMacTia I've added a spec to test it

Comment on lines 70 to 73
context "when param is optional & not present" do
it "doesn't transform the value" do
allow(controller).to receive(:params).and_return({ })
expect { controller.param! :foo, String, required: true, transform: :upcase }.to raise_error(RailsParam::InvalidParameterError, "Parameter foo is required")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this testing the same thing as the spec above it? The context says the param is optional but has required: true, which is confusing me.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you're right, I didn't see the above case, now I've modified it to fit the context.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really sorry, I copy-pasted from the test above and forgot to make the parameter optional 🙈

@iMacTia
Copy link
Collaborator

iMacTia commented Mar 28, 2023

@1075461847 I've just merged #94 in master, which should hopefully fix the CI issue with the setup-ruby action 🤞.
Could you please rebase your PR to pull those changes 🙏 ?

@1075461847 1075461847 force-pushed the fix/transform-with-default-value branch from d273dac to 4c1bf6c Compare March 28, 2023 10:07
@1075461847
Copy link
Contributor Author

@1075461847 I've just merged #94 in master, which should hopefully fix the CI issue with the setup-ruby action 🤞. Could you please rebase your PR to pull those changes 🙏 ?

@iMacTia I've rebase master.

Copy link
Collaborator

@iMacTia iMacTia left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @1075461847, to be honest I'm quite surprised the test we added is passing, but there might be something else going on here.
Regardless, you made a good addition and all tests are green, so I'll merge your PR as-is and investigate further before shipping in a new release 👍

@iMacTia iMacTia merged commit 12bc859 into nicolasblanco:master Mar 28, 2023
@iMacTia
Copy link
Collaborator

iMacTia commented Mar 28, 2023

As it turns out, we have the following guard at the beginning of the param! method:

return unless params.include?(name) || check_param_presence?(options[:default]) || options[:required]

Since the parameter is not present, optional (required: false) and no default is provided, the method exits immediately.
No further changes needed 🙌 !

@1075461847 1075461847 deleted the fix/transform-with-default-value branch March 29, 2023 09:02
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.

when the param use default value, transform will not take effect
3 participants