-
-
Notifications
You must be signed in to change notification settings - Fork 7k
[Ruby] Added validations resulting in a breaking changes #21029
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
@tomdracz thanks for reporting the issue. I merged it as bug fixes. Sorry that it breaks your use cases. @yujideveloper can you please review the feedback from @tomdracz when you've time? Thanks. |
does it mean you're assuming primitive type are nullable by default in your use cases? if that's the case, then there's a normalizer rule for that: SET_PRIMITIVE_TYPES_TO_NULLABLE |
Not quite. It's around any attribute. Consider schema entity like:
The generated model will create setter methods for all three fields and set them by default as nil, making them always required in initialisation. So FactoryBot configuration like:
Will immediately raise when calling something like It will blow up also even when customer is present like:
Unless For bigger objects, this creates lots of noise and necessitates defining all-encompassing factories, even when they are not required. Similarly, with Pact specs, when mocking some expectations it would not be unusual to only define partial objects to test for what's expected, but again, with the changes it necessitates creation of complete fixtures. Since this is squarely in the realms of testing (as deployed implementation works), it might be more of a "me" problem than "you" problem, but it did create quite a lot of havoc and we've rolled back to using v7.11.0 for now as we see no other workarounds available |
I don't like this approach that raises However, this approach wasn't introduced in #20672; it was first discussed in #15895 and implemented in #16114. #20672 is a follow-up to #16114 and implements missing validation patterns for required and non-nullable properties. P.S. |
For what it's worth, I've run into this as well! 🙇 While it's fantastic that the generator creates validation logic from the schema, it was surprising that it happens when initializing a new object, rather than "on demand". In my case, I'm using models as form objects in Rails. class AccountsController < ApplicationController
def new
@account_request = MyApi::CreateAccountRequest.new
end
def create
@account_request = MyApi::CreateAccountRequest.new(account_params)
MyApi::AccountsApi.new.create(@account_request)
redirect_to root_path
rescue MyApi::ApiError
render :new, status: :unprocessable_entity
end
private
def account_params
params.require(:account_request).permit(:email, :name, :password).to_h
end
end <%= form_with model: @account_request, url: accounts_path, method: :post, scope: "account_request" do |f| %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :email %>
<%= f.text_field :email %>
<%= f.label :password %>
<%= f.password_field :password %>
<%= f.submit %>
<% end %> This throws an error in the
It sounds like being able to manually call |
what about adding an option to skip validation in the constructor? |
@wing328 thanks for that idea! Although that might solve the problem to some extent, I worry it could turn into a gotcha since the accessors would behave differently, eg: request = CreateAccountRequest.new(email: "") # works
request.email = "" # ArgumentError I think I agree with @yujideveloper that it might be worth reconsidering the previous I'd be happy to take a stab at it as well if we're satisfied with that direction! |
Uh oh!
There was an error while loading. Please reload this page.
We're using API generator for Ruby to generate OpenAPI clients from the spec file.
We've just generated new client using version 7.12.0 but this resulted in numerous breaking changes.
Those were caused by additions in #20672 that weren't marked as breaking.
Is raising errors on initialization a sensible approach? This feels slightly off. Consider a example API entity of user with required name attribute.
Calling generated model initialization method without arguments like
Was working previously but will now raise an error as the
name
would be assigned to nil and the changes in setter methodname=
will mean it immediately fails.In our apps this has manifested mostly in two places:
initialize_with
declaration, otherwise I think default behaviour is to initialize empty model and then mass assign attributes laterKeen for any potential workarounds and other thoughts. Again the change was not noted as breaking but has got considerable knock on effect.
The text was updated successfully, but these errors were encountered: