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

@NotNull not supported on fields #395

Closed
vguna opened this issue May 18, 2016 · 10 comments
Closed

@NotNull not supported on fields #395

vguna opened this issue May 18, 2016 · 10 comments
Milestone

Comments

@vguna
Copy link

vguna commented May 18, 2016

Hi.

I'm using @NotNull to mark attributes as required. I tend to put these on the fields of a class. It seems that enunciate doesn't pick these up - unless they are specified on the getter. Could that be enhanced?

Another thing regarding this:

I've created my own annotation @NotBlank, that "inherits" @NotNull but also checks for empty strings using trim(). Would it make sense that enunciate also checks for any annotations on fields that "inherit" from @NotNull?

@stoicflame stoicflame added this to the 2.4.0 milestone May 18, 2016
@stoicflame
Copy link
Owner

What do you mean when you say @NotBlank "inherits" @NotNull?

@vguna
Copy link
Author

vguna commented May 26, 2016

If you take a look at hibernate's @NotBlank, it defines behavior, that inherits from @NotNull. That means, if the field is only decorated with @NotBlank, it automatically checks for not null AND additionally if the string is empty.

For the client side documentation it would be nice, if it would be set to required if an annotation already "inherits" @NotNull. Otherwise one would always have to add the two annotations separately for each field.

But since this is a custom annotation anyway, maybe it's good enough to handle that with the freemarker templates and check explicitly for @NotBlank on my own?

@Documented
@Constraint(validatedBy = { })
@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER })
@Retention(RUNTIME)
@ReportAsSingleViolation
@NotNull
public @interface NotBlank {
    String message() default "{org.hibernate.validator.constraints.NotBlank.message}";

    Class<?>[] groups() default { };

    Class<? extends Payload>[] payload() default { };

    /**
     * Defines several {@code @NotBlank} annotations on the same element.
     */
    @Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER })
    @Retention(RUNTIME)
    @Documented
    public @interface List {
        NotBlank[] value();
    }
}

@stoicflame
Copy link
Owner

Fixed at defdba0.

Added support for detecting "inherited" @NotNull annotations (i.e. annotations that are themselves annotated with @NotNull).

@stoicflame
Copy link
Owner

@ghost
Copy link

ghost commented Jun 6, 2016

Works, thanks! But this doesn't contain support for annotations on fields, as it seems, right :)?

@stoicflame
Copy link
Owner

The problem is that you're expecting Enunciate to use annotations that are on the fields for the properties (getters/setters).

If the fields were getting used as accessors instead of the properties, Enunciate would pick up the @NotNull annotations. So you can either annotate the properties, or use @JsonAutoDetect to specify that the fields should be used for (de)serialization instead of the properties.

@ghost
Copy link

ghost commented Jun 6, 2016

That means, that Enunciate is using jackson for reading annotations? Or does Enunciate evaluate the @JsonAutodetect by itself and acts accordingly?

@stoicflame
Copy link
Owner

Enunciate evaluates the annotation itself.

@ghost
Copy link

ghost commented Jun 6, 2016

Now I'm confused ;). Enunciate has its own mechanism for finding/reading annotations. Ok. And Enunciate evaluates @JsonAutodetect to decide where to expect the annotations - either on fields or properties - as specified by that annotation?

@stoicflame
Copy link
Owner

Correct.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants