spring-hateoas
spring-hateoas copied to clipboard
When @EnableHypermediaSupport has no types, only enable user-defined media types.
If an empty list of hypermedia types is provided, activate all of them.
Also consider making an empty list the default value for this annotation.
When using an empty list, the old code had this:
if (types.isEmpty()) {
return configurationProviders.toArray(new String[0]);
}
This was incorrect, and would produce an ArrayStoreException.
Instead, the purpose is to enable ALL the MediaTypeConfigurationProvider found in the application context, like this:
if (types.isEmpty()) {
return configurationProviders.stream() //
.map(MediaTypeConfigurationProvider::getConfiguration) //
.map(Class::getName) //
.toArray(String[]::new);
}
Since a Stream must be created in either situation, this may be preferable:
return configurationProviders.stream() //
.filter(it -> {
// If there are no types, then let them all through
if (types.isEmpty()) {
return true;
}
// Filter the ones supporting the given media types
return it.supportsAny(types);
}) //
.map(MediaTypeConfigurationProvider::getConfiguration) //
.map(Class::getName) //
.toArray(String[]::new);
Duplicates #1015 .
UPDATE: If enabled but empty, then don't register the pre-built ones. BUT activate custom ones.
Issue a warning if NO hypermedia types are found.
As discussed in a video call, we are switching it up so that enabling hypermedia support with no specified types, Spring HATEOAS will attempt to ONLY enable user-defined types.
If NO media types are found, issue a warning that you've enabled hypermedia support but not provided a media type.
This will avoid enabling newly added types from the team.
May want to include a custom annotation where the user can define a mediatype, but shield it from being picked up.
Because @EnableHypermediaSupport kicks in early in the lifecycle, we can't look for HypermediaMappingInformation types at that stage.
Only alternative to ANY sort of check sounds like some lifecycle event handler that validates right before the app goes active.