Skip to content
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

[FEATURE] Add configuration key lombok.copyableAnnotations.onMethod and/or add swagger's @Hidden to the list of COPY_TO_SETTER_ANNOTATIONS #3662

Open
dstango opened this issue May 2, 2024 · 0 comments

Comments

@dstango
Copy link

dstango commented May 2, 2024

Describe the feature
It would be nice to have an option to not only copy attribute annotations to setter-parameters via config, but also be able to copy them on the setter-method.

Here's my use case:
While defining an openapi-spec with swagger (io.swagger.core.v3:swagger-jaxrs2) I wanted to use the magic of @Data (I boiled it down to @Setter for the issue) and copyable annotations:

Example class:

@Setter
public class Foo {
	@Hidden
	private int bar;
}

lombok.config:

lombok.copyableAnnotations += io.swagger.v3.oas.annotations.Hidden

I got the (for me) mysterious error message The annotation @Hidden is disallowed for this location.

After lots of head scratching I found out lombok produced something like this out of it:

public class Foo {
	@Hidden
	private int bar;

        public void setBar(@Hidden int bar) {
            this.bar = bar;
        }
}

Turned out annotations on setters are always copied to the corresponding parameter, not on the setter-method.

Yet @Hidden specifies the following target:

@Target({METHOD, TYPE, FIELD, ANNOTATION_TYPE})

Reviewing the documentation of lombok.copyableAnnotations I find:

Lombok will copy any of these annotations from the field to the setter parameter, ...

So the behavior is as intended, yet kept me somewhat helpless.

I found a workaround, it's putting

@Setter(onMethod_ = {@Hidden})

on every field, which makes my original @Data annotation on the class redundant for setters.

I found an older issue that addresses this behavior in regards to builders: #1961.
This issue was resolved by hardcoding the special case @JsonProperty.

While I'd favor a general configuration solution like having a setting:

lombok.copyableAnnotations.onMethod += io.swagger.v3.oas.annotations.Hidden

which would result in:

public class Foo {
	@Hidden
	private int bar;

        @Hidden
        public void setBar(int bar) {
            this.bar = bar;
        }
}

an easier solution would be to add swagger's @io.swagger.v3.oas.annotations.Hidden to the list HandlerUtil.COPY_TO_SETTER_ANNOTATIONS, that already contains Json-properties, that seem to behave similarly.

Describe the target audience
Everyone that wants to use @Data or @Setter a lot with annotations that can only be put on a method level.

Additional context
This issue might be related: #3595

If the team lombok would choose to put the annotation to the COPY_TO_SETTER_ANNOTATIONS-list, I could provide a list of all swagger annotations that might fall into the same category (and of course provide a PR with this list).

Thanks for considering!

@dstango dstango changed the title [FEATURE] Add configuration key lombok.copyableAnnotations.onMethod add swagger's @Hidden to the list of COPY_TO_SETTER_ANNOTATIONS [FEATURE] Add configuration key lombok.copyableAnnotations.onMethod and/or add swagger's @Hidden to the list of COPY_TO_SETTER_ANNOTATIONS May 2, 2024
@dstango dstango changed the title [FEATURE] Add configuration key lombok.copyableAnnotations.onMethod and/or add swagger's @Hidden to the list of COPY_TO_SETTER_ANNOTATIONS [FEATURE] Add configuration key lombok.copyableAnnotations.onMethod and/or add swagger's @Hidden to the list of COPY_TO_SETTER_ANNOTATIONS May 2, 2024
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

No branches or pull requests

1 participant