ScaCap / spring-auto-restdocs

Spring Auto REST Docs is an extension to Spring REST Docs
https://scacap.github.io/spring-auto-restdocs/
Apache License 2.0
311 stars 86 forks source link

java.util.Optional<EnumType> does not generate enum constraint message #362

Closed aklatt1194 closed 4 years ago

aklatt1194 commented 5 years ago

Many of the fields in my models use java.util.Optional, and it generally seems to be pretty well supported by auto-restdocs. However, I noticed that my Optional Enum fields do not get a constraint message generated the same way that raw Enum fields do. It seems like this behavior would be desirable for most users.

One possible solution would be to add a special check if the type is java.util.Optional in the constraint message generation code: https://github.com/ScaCap/spring-auto-restdocs/blob/46df17af26a3ae097d063439c5dbfb37ce5f685d/spring-auto-restdocs-core/src/main/java/capital/scalable/restdocs/constraints/ConstraintReaderImpl.java#L137, and, if so, use MethodParameter.getGenericParameterType() to get the underlying Enum type (as opposed to using MethodParameter.getParameterType()).

Note that it looks like this is possibly related to: https://github.com/ScaCap/spring-auto-restdocs/issues/288. Perhaps there should be a collection of known types (Collection, Optional etc.) where the generic parameter is used to determine whether to generate the enum constraint?

fbenz commented 4 years ago

This makes sense and also the suggestion sounds good. I'm not sure whether I can look into this soon but a PR is of course welcome.