-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
[BUG][Java] Model does not compile due to fluent-setters conflicting with set_*
field normal setter
#14770
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
Found that the builder pattern to the pojos was added on the template here: 54e78bd. Could be possible to add a flag to the tool to skip on the moustache template the builders ( |
name
and set_name
present on schema)set_*
field
set_*
fieldset_*
field
I was investigating a about the pattern that is causing the problem as I have never saw something like that in java, and it turns out that it is the fluent-setter pattern (see this article). This is similar to the builder pattern but within a POJO, and it is not an standard one as it is described in the article that advocates for it (see also some old discussion here), although there is an option on Lombok to generate such methods. Nevertheless, I believe that it should be either one or the other, an standard setter (e.g., So I would now suggest one of the following solutions for the problem:
I can propose a PR for (1) as I was investigating about the templates, identifying already several java-client generators where the |
set_*
fieldset_*
field
set_*
fieldset_*
field normal setter
I‘d opt for an option disabling the fluent setters only. That said, you can also pass a modified template when invoking the generator so that you can remove the offending fluent setters: https://openapi-generator.tech/docs/templating#retrieving-templates |
Fluent setters may conflict with ordinary setter, if model has 2 properties name '${X}' and 'set${X}'. Add skipFluentSetters additional property used to skip fluent setters. Set it to "true" to skip fluent setters in all models, or provide a comma separated list of models to exclude fluent setters. Add tests for new option. Fixes OpenAPITools#14770
Push So this might lead to usage of the wrong setters. This also might be a bug of the fluent setter generation. |
You are indirectly referring to i.e. generating a builder inner class or so instead? Well, changing generation like that is a breaking change. Could open a separate issue for that..
Bean validation is relevant to the getters only. I believe it’s correct not having them on (fluent or not) setters. |
I do not know what was the first reason to add these fluent setters (builder like setters) while you can generate builders by adding a config option generateBuilders to true. The fluent setters confuse also the serialization in jackson libraries, eg if by chance you have a property that is prefixed with set (eg. settlement) the fluent setter will drive the serialization that this as a normal java bean setter (even if the first letter after set is not capital letter). I understand that this is might be a bug in that lib but it drives me to wonder about having these fluent setter which are very useless and over engineer. Disabling these fluent setter would solve implicitly the issue. |
Springdoc-openapi also misinterprets fluent setters for fields like |
Bug Report Checklist
Description
When trying to generate a default java client model for an API that I described (and where I have no control over the serialized model returned by it), the class generated for one schema contains duplicated methods and thus does not compile. The schema itself containing a
name
and aset_name
fields. This results on 2 methods calledsetName
, one being the builder-like setter for theset_name
field and the other the setter for thename
field.I would like to know if this is something that can be fixed or there is already a workaround for this use-case. The only requirement is to be able to deserialize/serialize the object with the
name
andset_name
field, so it doesn't matter if the way to do so is to change the field-name/methods on the generated class as a workaround.openapi-generator version
6.3.0
OpenAPI declaration file content or url
Generation Details
java
npx
packageSteps to reproduce
org.openapitools.client.model.ConflictingSchema
Related issues/PRs
set on the prefix, but just
_(
_typeand
type` conflicting).Suggest a fix
The solution on the related issue won't work as only takes into account names starting with
_
.As I am kind of new with OpenAPI and its generator, the only suggestion that I have is to provide a way to override the name of the field/getter/setter somehow for those kind of conflicting names, or adding some extension for providing it on the openapi spec.
If there is a generator and/or option of using a different library than GSON for the model serialization/deserialization, it is also a solution from my side, as I don't need to stick to a specific library.
The text was updated successfully, but these errors were encountered: