-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
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
[ruby] Fix init/serialization/deserialization of map/array model aliases #7627
Conversation
👍 Thanks for opening this issue! The team will review the labels and make any necessary changes. |
Not sure I understand the concept, what problem is the whole "alias" feature solving? |
Well first of all, it's a feature that's supported in a bunch of different generators. Not sure exactly what was the origin of this feature, but more recently, it had to be used in the |
But what does the feature provide? Do you have an example where it's useful? All languages are different and will have different ways to deal with things, I don't think "language X and Y do this" should be a reason. |
Honestly I don't. We never used it until we needed it for oneOf in go. |
@autopp, some additional feedback on this from the technical committee would be awesome, thanks 🙇♂️ |
I think the Go client generator should be updated to support array/map as a primitive type instead of being a model. (same for the Ruby client generators) |
I don't think this is needed anymore since I implemented support for primitives, arrays and hashes for oneOf in #5706 Or maybe your use case here is yet different? |
Well IMO it's either we fix it completely, or we completely remove the feature for this generator. Right now it's kinda half supported, meaning the ruby generator has a different behavior based on the If you don't feel like supporting this feature at all, I could try to see if I can work on making this generator completely ignore the |
The problem is I just don't understand what the feature does. Examples of how the output would change with and without that flag set would help. |
or
from https://openapi-generator.tech/docs/globals/ Basically, when true, the generator would generate a model for the primitive types, and use that model in the code instead of directly using the type it aliases. For instance, let's say you have a schema defined like the following in your spec: ArrayAlias:
type: array
items:
type: string If you set the flag to false, nothing will get generated by the generator, and wherever this type is referenced in your spec, you'll just use a regular array of strings in the code. In Ruby currently, when true, it creates a class in class ArrayAlias < Array
...
end This class would be used as the type wherever it is ref'd in the spec. For instance if ref'd in another schema, the def self.openapi_types
{
:'refd_array_alias' => :'ArrayAlias'
}
end When false, it doesn't generate a new class, and the def self.openapi_types
{
:'refd_array_alias' => :'Array<String>'
}
end Does that make things clearer regarding what this feature does ? |
The feature (generateAliasAsModel) was introduced to make it backward compatible when we wanted to remove such behaviour. It was primarily used by the Java generators based on request from users to keep such behaviour. There are discussions suggesting "extending a model with array or map type" is an anti-pattern in Java but regardless we introduced the option to keep some users happy.
Only array, map will be generated as models when generateAliasAsModel is set to true. String, integer and other primitive types won't be generated as models.
I'm not a Ruby expert but this looks like an anti-pattern in Ruby to me. Is it correct to say the goal of this PR is to make primitive types work in oneOf in the Ruby client? |
The main goal of this PR was to make the feature work, because we already use in our spec for another language (go). Our problem is that since we use it in go (on a per-model basis thanks to #6937), it ends up being used in ruby too, which was the reason for me fixing it. Now if primitives work for oneOf in ruby, I'm happy to close this, and make the ruby client generator ignore this feature flag. |
First of all, thanks again for the PR. We truly appreciate your contributions. As @jfeltesse-mdsol has pointed out, #5706 will support primitive type as well so I suggest we close this one for the time being. |
Done ! |
Good idea. I'll try to come up with a PR this weekend. |
@zippolyte Thanks for the detailed explanation, it's much clearer now. As @wing328 pointed out, if you need a class to have those behaviors you wouldn't inherit but rather you'd include the Enumerable module, just like the Array and Hash classes do. Since overall the feature looks like it was a stopgap one I think we can move on now. |
Follow up to #7419
Currently, trying to instantiate such an object fails as the generic
initialize
method doesn't work at all when the parent is an Array or Hash.In the same way, serializing and deserializing doesn't work the generic way, so this is adding specific templates for the map/array aliases
PR checklist
./bin/generate-samples.sh
to update all Petstore samples related to your fix. This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master. These must match the expectations made by your contribution. You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example./bin/generate-samples.sh bin/configs/java*
. For Windows users, please run the script in Git BASH.master
cc @cliffano (2017/07) @zlx (2017/09) @autopp (2019/02)